Adding target-specific labels in the static_configs section of your Prometheus configuration file (prometheus.yml) allows you to associate additional metadata with your targets. This can be useful for filtering or aggregating metrics later on. Here's how to do it step-by-step.
Step 1 — Modify your prometheus.yml
Open your prometheus.yml file, and find the scrape_configs section where you define your static targets. Here’s an example of how to add target-specific labels.
Breakdown of the configuration
job_name: This is a unique name for the group of targets you're scraping. It's used to identify this group in queries and dashboards.targets: List the targets you want to scrape. Each entry can be an IP address or hostname followed by the port number.labels: Here, you define any additional labels you want to attach to all targets listed understatic_configs. You can add as many key-value pairs as needed.
Step 2 — Reload the Prometheus configuration
After making changes to your prometheus.yml file, you need to reload the configuration for the changes to take effect. You can do this without restarting Prometheus by sending a SIGHUP signal:
Alternatively, if you are using the Prometheus UI, you can go to http://<prometheus-server>:9090/-/reload to trigger a reload.
Step 3 — Querying the labels
Once the configuration is reloaded, you can query metrics while using the new labels. For example, to see all metrics for your application in the production environment, you can use:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.