It's better to avoid honor_labels unless you really need it. It allows the 
exporter to do bad things like overriding the "job" and "instance" labels.

Better to use metric_relabel_configs to relabel, at which point you have 
the combined set of labels from service discovery and the scrape. A missing 
label is the same as a label whose value is the empty string. So you can do 
(untested):

metric_relabel_configs:  
  - source_labels: [ source, __address__ ]
    regex: ";(.*)(:.*)?"
    target_label: source
    replacement: "${1}"

This works as long as the source label's value cannot start with a 
semicolon - if it can, define a different "separator" character. If you 
don't like that, another approach is to set a temporary label as a flag:

metric_relabel_configs:  
  - source_labels: [ source ]
    regex: ""
    target_label: __tmp_source_empty
    replacement: Y
  - source_labels: [ __tmp_source_empty, __address__ ]
    regex: "Y;(.*)(:.*)?"
    target_label: source
    replacement: "${1}"
  - target_label: __tmp_source_empty
    replacement: ""

On Tuesday, 11 March 2025 at 17:39:58 UTC mohan garden wrote:

> Hi Everyone,
>
> I need to handle a scenario where I add a label to ingested metrics, but 
> only if the label does not already exist in the collected data. If the 
> label is present in a metric, it should remain unchanged.
>
> For example, given a set of metrics:
>
> custom_metrics_from_server{ source="server1.example.com",status="run" } 
> 3213  
> custom_metrics_from_server{ source="server1.example.com",status="pend" } 
> 3215
>
> When additional metrics are collected from a node exporter, some will 
> already have a "source=" label, while others will not:
>
> custom_metrics_from_server{ source="server1.example.com",status="run" } 
> 3213  
> custom_metrics_from_server{ source="server1.example.com",status="pend" } 
> 3215  
> node_cooling_device_max_state{name="7",type="Processor"} 0
>
> To ensure that all metrics contain the "source=" label without overriding 
> existing ones, I figured the following approach using honor_labels and 
> relabel_configs:
>
> honor_labels: true  
> relabel_configs:  
>   - source_labels: [ _address_ ]  
>     regex: "(.*):(.*)"  
>     target_label: "source"  
>     replacement: '${1}'
>
> Is this the best way to handle this, or is there a more optimal approach?
> Please advice.
>
> Thanks!
>

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/prometheus-users/56e482fa-736f-4210-aa00-0217988c743an%40googlegroups.com.

Reply via email to