✨ Unearthing Prometheus’s Hidden Gem: Diving into honor_labels

Hello everyone! Today, we’re going to take a deep dive into an interesting setting you might encounter while using Prometheus: honor_labels.

When collecting metrics with Prometheus, you might have experienced moments like, “Huh? Why did my configured label change?” or “My desired label disappeared!” The key to unlocking this mystery is very likely honor_labels! πŸ”‘


πŸ’₯ Label Conflicts, What’s Going On?

When Prometheus scrapes metrics, it automatically adds several labels to include useful information about those metrics. The most common ones are the job and instance labels.

  • job: Indicates which scraping job the metric belongs to.
  • instance: Indicates which target the metric was scraped from.

However, a problem can arise here. What if the metrics of the target we are trying to scrape already contain labels named job or instance? 🧐

For example, if the target exposes a metric like my_metric{job="my-app-exporter"}, and the Prometheus configuration scrapes this target with job_name: 'api-server', then the label name job will conflict.

The option that determines which label Prometheus chooses in this situation is honor_labels.


πŸ€” honor_labels: false (Default) – Prometheus’s Approach

This setting is the default value for honor_labels. It literally means “do not honor” the target’s labels (false), so labels automatically added by Prometheus take precedence.

  • How it works:
  1. Prometheus uses its own configured job and instance labels as they are. πŸ›‘οΈ
  2. Existing conflicting labels on the target (e.g., job) are renamed.
  3. A prefix exported_ is added to their names, changing them to something like exported_job. 🏷️
  • Example:
  • Target Metric: http_requests_total{job="frontend"}
  • Prometheus Configuration: job_name: 'web-crawlers'
  • Final Scraped Result: http_requests_total{job="web-crawlers", exported_job="frontend", ...}

As a result, Prometheus’s job label is applied first, and the original job label from the target is renamed to exported_job and preserved.


πŸ‘‘ honor_labels: true – Respecting the Target’s Labels!

If you change this setting to true, Prometheus behaves in the opposite way. It means “honor” the target’s labels (true), so Prometheus uses the labels provided by the target as they are.

  • How it works:
  1. Labels in the target’s metrics are respected with the highest priority. πŸ™
  2. If a label Prometheus intends to add conflicts in name, Prometheus quietly gives up adding its own label.
  3. The target’s labels are stored without any changes.
  • Example:
  • Target Metric: http_requests_total{job="frontend"}
  • Prometheus Configuration: job_name: 'web-crawlers' and honor_labels: true
  • Final Scraped Result: http_requests_total{job="frontend", ...}

As you can see, Prometheus’s job_name setting of ‘web-crawlers’ is ignored, and the job="frontend" label originally held by the target is retained.


πŸ’‘ When Should You Use honor_labels: true?

So, when is this option useful? It primarily shines in the following situations:

  1. When configuring Federation: Used when scraping the /federate endpoint of another Prometheus server. It’s essential when the original Prometheus already has meaningful job and instance labels that need to be preserved.
  2. When using Pushgateway: Pushgateway manages its own job and instance labels, so this is useful when you want to use those labels as they are without overwriting them.
  3. When utilizing Service Discovery: In environments like Kubernetes, when metadata labels obtained through service discovery hold more significant meaning than Prometheus’s job name, you can use this to make those labels act as the job label.

πŸ“ Summary at a Glance

Setting honor_labels: false (Default) honor_labels: true
Priority Prometheus’s automatically added labels πŸ‘‘ Target’s existing labels πŸ‘‘
Behavior on Conflict exported_ prefix added to target’s label name Prometheus’s labels are discarded
Resulting job Label Prometheus configuration value (job_name) Target’s original job label value
Primary Use Cases Most general scraping environments Federation, Pushgateway, Advanced Service Discovery

honor_labels is a powerful tool that allows you to finely control Prometheus’s labeling strategy. Understand and utilize this option well to manage cleaner and more meaningful metrics!πŸš€


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *