一.honor_labels的应用

- job_name: monitoring/kubelet/0  honor_labels: true  scrape_interval: 30s  scrape_timeout: 5s  metrics_path: /metrics  scheme: https

honor_labels用于解决 <scrape的指标label> 与 <target的label> 的抵触,比方job、instance等:

  • honor_labels=true时,当labelKey抵触时,间接应用scrape的指标label,不顾及target的label;

    • 通常用于联邦集群或pushgateway的场景,因为该状况下,所有原始的labels须要保留;
  • honor_labels=false时,当labelKey抵触时,将scrape的label批改为:exported_labelKey—>lableValue;
  • 默认honor_labels=false;

honor_labels controls how Prometheus handles conflicts between labels that are
already present in scraped data and labels that Prometheus would attach
server-side ("job" and "instance" labels, manually configured target
labels, and labels generated by service discovery implementations).

If honor_labels is set to "true", label conflicts are resolved by keeping label
values from the scraped data and ignoring the conflicting server-side labels.

If honor_labels is set to "false", label conflicts are resolved by renaming
conflicting labels in the scraped data to "exported_<original-label>" (for
example "exported_instance", "exported_job") and then attaching server-side
labels.

Setting honor_labels to "true" is useful for use cases such as federation and
scraping the Pushgateway, where all labels specified in the target should be
preserved.

Note that any globally configured "external_labels" are unaffected by this
setting. In communication with external systems, they are always applied only
when a time series does not have a given label yet and are ignored otherwise.
[ honor_labels: <boolean> | default = false ]

二.honor_labels的源码

prometheus的源码中:

// scrape/scrape.gofunc mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*relabel.Config) labels.Labels {    lb := labels.NewBuilder(lset)        // 解决scrape.label与target.label的抵触                     if honor {    // honor=true时,,当labelKey抵触时,间接应用scrape的label,不顾及target的label        for _, l := range target.Labels() {            if !lset.Has(l.Name) {                lb.Set(l.Name, l.Value)            }        }    } else {    // 默认honor=false时,当labelKey抵触时,将scrape的label批改为:exported_labelKey,lableValue        for _, l := range target.Labels() {            // existingValue will be empty if l.Name doesn't exist.            existingValue := lset.Get(l.Name)            if existingValue != "" {                lb.Set(model.ExportedLabelPrefix+l.Name, existingValue)    // exported_前缀            }            // It is now safe to set the target label.            lb.Set(l.Name, l.Value)        }    }    res := lb.Labels()                              // 输入Builder结构实现的Labels    if len(rc) > 0 {                                // 执行relabel操作        res = relabel.Process(res, rc...)    }    return res}

参考:

1.官网doc: https://prometheus.io/docs/pr...