Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Prometheus scrape URL for every target

Tags:

Every instance of my application has a different URL. How can I configure prometheus.yml so that it takes path of a target along with the host name?

scrape_configs:
- job_name:       'example-random'

# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s

static_configs:
  - targets: ['localhost:8090','localhost:8080']
    labels:
      group: 'dummy'
like image 643
poojabh Avatar asked Jul 05 '17 13:07

poojabh


People also ask

What is Prometheus default scrape interval?

global: scrape_interval: 15s # By default, scrape targets every 15seconds.

How do I change the metrics path in Prometheus?

You currently can't configure the metrics_path per target within a job but you can create separate jobs for each of your targets so you can define metrics_path per target.

How often does Prometheus scrape?

In this case the global setting is to scrape every 15 seconds. The evaluation_interval option controls how often Prometheus will evaluate rules. Prometheus uses rules to create new time series and to generate alerts. The rule_files block specifies the location of any rules we want the Prometheus server to load.

Can Prometheus scrape itself?

Prometheus collects metrics from targets by scraping metrics HTTP endpoints. Since Prometheus exposes data in the same manner about itself, it can also scrape and monitor its own health.


3 Answers

You currently can't configure the metrics_path per target within a job but you can create separate jobs for each of your targets so you can define metrics_path per target.

Your config file would look something like this:

scrape_configs:
- job_name:       'example-target-1'
  scrape_interval: 5s
  metrics_path: /target-1-path-to-metrics
  static_configs:
    - targets: ['localhost:8090']
      labels:
        group: 'dummy'

- job_name:       'example-target-2'
  scrape_interval: 5s
  metrics_path: /totally-different-path-for-target-2
  static_configs:
    - targets: ['localhost:8080']
      labels:
        group: 'dummy-2'
like image 160
Oliver Avatar answered Sep 24 '22 11:09

Oliver


I achieved this by using file_sd_config option. All targets are described in separate file(s), which can be either in YML or JSON format.

prometheus.yml:

scrape_configs:
  - job_name: 'dummy'  # This will be overridden in targets.yml
    file_sd_configs:
      - files:
        - targets.yml

targets.yml:

- targets: ['host1:9999']
  labels:
    job: my_job
    __metrics_path__: /path1

- targets: ['host2:9999']
  labels:
    job: my_job  # can belong to the same job
    __metrics_path__: /path2
like image 20
Yaroslav Stavnichiy Avatar answered Sep 22 '22 11:09

Yaroslav Stavnichiy


I believe you need to do some relabelling of the __metrics_path__ label set to include the varying paths of your applications.

The Prometheus configuration docs will prove useful to you here and this article should help you understand relabelling a little better.

like image 6
Conor Avatar answered Sep 22 '22 11:09

Conor