How can I drop a specific label in a specific Prometheus ServiceMonitor metric using relabeling/metricRelabeling?

4.2k Views Asked by At

For example, I have teo metrics with different label

node_metrics_first{foo="bar",AAA="aaa"}
node_metrics_second{BBB="bbb",CCC="ccc"}

how can I use relabeling/metricRelabeling drop/labeldrop in servicemonitor to remove foo label in node_metrics_first, which means I should get the result:

node_metrics_first{AAA="aaa"}
node_metrics_second{BBB="bbb",CCC="ccc"}

2

There are 2 best solutions below

2
Rick Rackow On

There is an extensive blog from Grafana about how the relabeling works. In your case:

  - job_name: some_job
    metric_relabel_configs:
      - regex: "foo"
        action: labeldrop
0
Aref Riant On

In Prometheus operator ServiceMonitors, you can use spec.endpoints[*].relabelings to alter labels and metrics:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
spec:
  endpoints:
  - interval: 30s
    port: metrics
    scheme: http
    metricRelabelings:   <-- here you shine!
    - action: labeldrop
      regex: (foo|otherlabeltodrop)