首页 > 解决方案 > Node.js + Prometheus - 目标连接被拒绝

问题描述

我在本地运行节点应用程序。它http://localhost:3002使用prom-client运行,我可以在以下端点http://localhost:3002/metrics看到指标。

我已经在 docker 容器中设置了 prometheus 并运行了它。

Dockerfile

FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/

普罗米修斯.yml

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:3002']
        labels:
          service: 'my-service'
          group: 'production'
rule_files:
  - 'alert.rules'

docker build -t my-prometheus .
docker run -p 9090:9090 my-prometheus

当我导航到http://localhost:9090/targets它显示

获取http://localhost:3002/metrics : 拨打 tcp 127.0.0.1:3002: connect: 连接被拒绝

在此处输入图像描述

你能告诉我我在这里做错了什么吗?节点应用程序正在该端口的本地主机上运行,​​因为当我去时http://localhost:3002/metrics我可以看到指标。

标签: node.jsprometheus

解决方案


当您在容器内时,您无法直接访问 localhost。您需要将 docker.for.mac.localhost 添加到您的 prometheus.yml 文件中。见下文:

prometheus.yml 文件中的工作。- 工作名称:'普罗米修斯'

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ['localhost:9090']
- targets: ['docker.for.mac.localhost:3002']  

推荐阅读