Conditionally Rewriting Prometheus Instance Label
Something curious found ot today, and I don’t want to lose, is how to overwrite
the instance label in prometheus, specifically, we have an exporter
(openstack-exporter) that exposes some metrics, and it populates one label
sometimes named hostname.
But for several reasons when dealing with the metrics we only filter by
instance, problem is, that the instance metric is usually the address/name
of the target prometheus is pulling the metrics from, and is this case that does
not match the hostname.
Now, it seems simple right? What I what to do is (in pseudopython):
|
|
Well… it is not that simple xd, but found a relatively nice way of doing it.
|
|
Some things to highlight:
- Using
relabel_configsinstead ofmetric_relabel_configswould not work, as the first runs before the metrics are gathered, so thehostnamelabel is not there yet. - The order of the labels in the
source_labelsarray will match the order in which they are concatenated. - The regex is applied to the concatenated labels.
- I’m implementing the fallback behavior with regexes (ouch!), by using the
seprator (
;) as an anchor to match one group or the other as$1.
