Kay
Kay

Reputation: 19660

Node.js + Prometheus - Target Down Connection Refused

I am running a node applications locally. It runs on http://localhost:3002 using prom-client i can see the metrics at the following endpoint http://localhost:3002/metrics.

I've setup prometheus in a docker container and ran it.

Dockerfile

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

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

When i navigate to http://localhost:9090/targets it shows

Get http://localhost:3002/metrics: dial tcp 127.0.0.1:3002: connect: connection refused

enter image description here

Can you please tell me what im doing wrong here. node app is running on localhost at that port becasue when i go to http://localhost:3002/metrics i can see the metrics.

Upvotes: 3

Views: 3225

Answers (4)

Raphael Vitor
Raphael Vitor

Reputation: 652

I do this to localhost...success

global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 1s
scrape_configs:

  • job_name: prometheus honor_timestamps: true scrape_interval: 15s scrape_timeout: 10s metrics_path: /metrics scheme: http follow_redirects: true static_configs:
    • targets: ['127.0.0.1:9090']
  • job_name: class honor_timestamps: true scrape_interval: 5s scrape_timeout: 5s metrics_path: /metrics scheme: http follow_redirects: true static_configs:
    • targets: ['host.docker.internal:8080']

Upvotes: 0

burak isik
burak isik

Reputation: 571

The applications are not in the same network. Firstly, you can create docker image from your node application too. When running docker images, network( --net) parameter should be passed to both images.

Run prometheus app:

docker run --net basic -p 9090:9090 my-prometheus

Run nodejs app:

docker run --net basic -p 8080:8080 my-node-app

Now, the applications run in the same network that is called basic. So the prometheus application can access the http://localhost:3002/metric endpoint.

Upvotes: 0

Maher Ismaail
Maher Ismaail

Reputation: 1663

and for windows, it would be

- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
  - targets: ['docker.for.win.localhost:8082']

Upvotes: 1

Brandon Gilzean
Brandon Gilzean

Reputation: 31

When you are inside a container, you cannot access the localhost directly. You will need to add docker.for.mac.localhost to your prometheus.yml file. See below:

Your Job in prometheus.yml file. - job_name: 'prometheus'

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

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

Upvotes: 3

Related Questions