prometheus icon indicating copy to clipboard operation
prometheus copied to clipboard

Turn one source label into multiple target labels

Open Bios-Marcel opened this issue 3 years ago • 5 comments

Proposal

Hey

So, we have the usecase that we have one central prometheus instance and developers do not have access to the prometheus configuration. We are scraping consul for our metrics targets. While applications can provide their own labels, there is also certain data that the application doesn't know of, for example its (dynamic) id in our deployment system. What we'd like to be able to do, is to have some generic consul tag where can extract promtheus labels from.

For example with this config:

      - source_labels: [__meta_consul_tags]
        regex: "(?:^|.*?,)prom_label__(.+?)=(.+?)(?:$|,.*?)"
        action: replace
        target_label: $1
        replacement: $2

Given these consul tags:

a=b,b=c,prom_label__f=g,t=e,prom_label__z=l,l=o

I'd expect these labels in prometheus for the given target:

f=g,z=l

The workaround would be to manually list the labels for all services, which could be quite a lot.

I've already looked at the code and guess that we can easily change the regex code to use multiple matches instead of just one set of submatches. Adjusting the tests is an easy feat as well. This should imo not be a breaking change.

Bios-Marcel avatar Sep 27 '23 11:09 Bios-Marcel

Hello,

There is too much risk that this creates an avalanche of labels and therefore we have always said that it is better not to use this options for users. This could lead to many many cardinality issues.

roidelapluie avatar Oct 01 '23 18:10 roidelapluie

The issue arises from the "problem" that both consul and nomad provide their labels as a single comma separated list. I guess an alternative solution for this particular case would be to have an option in the SD implementation to split the labels before passing them into relabeling.

pschichtel avatar Oct 01 '23 19:10 pschichtel

Yes

roidelapluie avatar Oct 06 '23 08:10 roidelapluie

Hello from the bug scrub!

It's probably best to fix this in the SD mechanism. For example, in K8s SD, the dynamic amount of pod labels are all separate labels in the SD, from where they can be mapped into Prometheus labels as the operator sees fit (with labelmap).

Having said that, we don't believe the original concern of a cardinality explosion is valid because the cardinality by __meta_consul_tags as a single label would be the same as that of a set of labels derived from it. So maybe a feature like this might be more viable that originally thought.

Whoever wants to approach this problem might consider either approach.

beorn7 avatar Jan 28 '25 11:01 beorn7

I'd like to pick this up on the SD side, since that's where the bug scrub pointed. The idea is that Consul and Nomad SD also expose each tag as its own meta label, the same way K8s does for pod labels: a key=value tag becomes __meta_consul_tag_<key>="<value>", and a plain tag becomes __meta_consul_tagpresent_<tag>="true". Then labelmap with __meta_consul_tag_prom_label__(.+) covers the case in the issue. __meta_consul_tags stays as it is, and it would be the same for __meta_nomad_tag_ / __meta_nomad_tagpresent_.

Does that shape work for you, or would you rather keep it behind a config option? Happy to send the PR either way.

cvas22 avatar Nov 11 '26 23:11 cvas22