一.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.go
func 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...


a朋
63 声望38 粉丝