3.07L Scraping metric to Prometheus - MartinWong06/grafana GitHub Wiki

How do you set up a new Prometheus instance to monitor a target?

  1. Configure Prometheus: In the Prometheus configuration file (prometheus.yml), you need to specify the targets that you want to monitor. This can be done by defining the scrape_configs section in the configuration file, where you can specify the target endpoints, labels, and other settings.

For example, the following configuration scrapes metrics from a node exporter running on localhost:9100:

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  1. Start Prometheus / Restart Prometheus container

  2. Verify targets Check if the targets are being scraped and if the metrics are being collected correctly. You can navigate to the Prometheus web interface (http://localhost:9090 by default) and check the "Status" page to see the list of targets and their scrape statuses.

How can you add labels to Prometheus metrics?

- job_name: 'my-job'
  scrape_interval: 5s
  static_configs:
  - targets: ['localhost:9090']
    labels:
      environment: 'production'
      app: 'my-app'
      instance: 'my-instance'
  relabel_configs:
  - source_labels: [__address__]
    target_label: instance
    replacement: '${1}:8080'
    regex: (.*):9090
  - source_labels: [__name__]
    target_label: metric_name
  - source_labels: [instance]
    target_label: instance
    regex: '(.*)'
    replacement: '$1'

In this example, the labels section adds the labels environment, app, and instance to the metrics from the target localhost:9090. The relabel_configs section uses the label_replace function to add a metric_name label based on the metric name, and to add an instance label based on the existing instance label with a regex replacement. This will result in metrics with the added labels that can be used for aggregation, filtering, and querying in Prometheus.

How to change job scrape interval in Prometheus?

  1. Open the prometheus.yml configuration file for editing. This file is typically located in the /etc/prometheus/ directory.
  2. Locate the job for which you want to change the scrape interval.
  3. Under the scrape_configs section, find the scrape_interval option and change its value to the desired scrape interval. The scrape interval is specified in seconds.
  4. Save the prometheus.yml configuration file.
  5. Restart Prometheus container

Here is an example configuration that sets the scrape interval for a job named my_job to 15 seconds:

scrape_configs:
  - job_name: 'my_job'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:9090']
⚠️ **GitHub.com Fallback** ⚠️