Any ways to specify default labels for all metrics using prometheus golang client library

3,525 views
Skip to first unread message

olivierb...@gmail.com

unread,
May 19, 2017, 11:58:30 AM5/19/17
to Prometheus Developers
I have a code written in Go(lang) which uses https://github.com/prometheus/client_golang
I tried to find in several places (including telegraf) if there was a way to have all metrics
expose a set of default labels/tags (such as hostname but not only) without having to modify all the code
to add ... .WithLabel("LBL"). ... and all prometheus.NewXXX() call adding the appropriate arguments
which is kind of tedious ?

Thanks for your answers

Brian Brazil

unread,
May 19, 2017, 12:01:03 PM5/19/17
to olivierb...@gmail.com, Prometheus Developers
This is considered an anti-pattern, which is why you can't find the feature.

 
--

olivierb...@gmail.com

unread,
May 23, 2017, 4:53:34 AM5/23/17
to Prometheus Developers, olivierb...@gmail.com
Brian, many thanks for this explanation. Fully understandable from my standpoint and I have implemented it and test was successful. However I have an additional question as the queries which looked like:
sum (felix_active_local_endpoints) by (instance)
have now been replaced by
sum by (host) (felix_active_local_endpoints * on (job, instance) group_left(host) felix_host)
where felix_host is the new metric I created to get the hostname

If there are quite a numerous number of hosts reporting these metrics, how would the change impact metrics gathering/performance of prometheus server as the complexity of the query is quite different now

Thanks again
Olivier

Brian Brazil

unread,
May 23, 2017, 5:12:53 AM5/23/17
to Olivier Bourdon, Prometheus Developers
You're only ingestion one additional series per host, that's insignificant performance wise.  Querying is completely separate to ingestion.

--

Ben Kochie

unread,
May 23, 2017, 9:48:07 AM5/23/17
to olivierb...@gmail.com, Prometheus Developers
Another option is to adjust these labels at ingestion time.  For example, if your "host" label is the FQDN hostname, you could split up the instance label with:

metric_relabel_configs:
  - source_labels: [instance]
    target_label: host
    regex: "(.+):\d+"

Doing this at service discovery time is the preferred method over adding inside the exporter.  The exporter should be "dumb" and only care about its dimensions.


--
You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-developers+unsub...@googlegroups.com.
To post to this group, send email to prometheus-developers@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/6ee457e1-d234-4b0f-bf44-6f4651159b29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages