Home > Enterprise >  Prometheus multiple source label in relabel config
Prometheus multiple source label in relabel config

Time:11-26

For some reason, my Pod in k8s could possibly labeled with syscode or cib_syscode, how can I relabel both of them to cib_syscode? That is to say if pod labeled with one of them, choose its label value as the result. I have tried as follow, but it doesn't work because the target_label is duplicate.

    - job_name: ncr
      honor_labels: true
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics
      scheme: http
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_syscode]
        separator: ;
        regex: (.*)
        target_label: cib_syscode
        replacement: ${1}
        action: replace
      - source_labels: [__meta_kubernetes_pod_label_cib_syscode]
        separator: ;
        regex: (.*)
        target_label: cib_syscode
        replacement: ${1}
        action: replace

Looking forward to your reply, thank you!

CodePudding user response:

Try the following:

    - job_name: ncr
      honor_labels: true
      scrape_interval: 30s
      scrape_timeout: 30s
      metrics_path: /metrics
      scheme: http
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_syscode, __meta_kubernetes_pod_label_cib_syscode]
        separator: ;
        regex: ([^;] )
        target_label: cib_syscode
        replacement: ${1}
        action: replace

If either __meta_kubernetes_pod_label_syscode or __meta_kubernetes_pod_label_cib_syscode is set, then the regex should select its value (i.e. a continuous string not containing the separator ;) and assign it to the cib_syscode target label.

CodePudding user response:

Found this while fixing the same issue. The answer by weibeld is correct by design, but unfortunately that regexp replacement doesn't work (Prometheus 1.20). This one does:

relabel_configs:
  - source_labels: [__meta_kubernetes_pod_label_syscode, __meta_kubernetes_pod_label_cib_syscode]
    separator: ;
    regex: '((.*);(.*))'
    target_label: cib_syscode
    replacement: '$2$3'
    action: replace
  • Related