3

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.

4 Answers 4

3

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']  
Sign up to request clarification or add additional context in comments.

Comments

1

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']

Comments

0

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.

Comments

0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.