Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relabel instance to hostname in Prometheus

Tags:

I have Prometheus scraping metrics from node exporters on several machines with a config like this:

scrape_configs:   - job_name: node_exporter     static_configs:       - targets:         - 1.2.3.4:9100         - 2.3.4.5:9100         - 3.4.5.6:9100 

When viewed in Grafana, these instances are assigned rather meaningless IP addresses; instead, I would prefer to see their hostnames. I think you should be able to relabel the instance label to match the hostname of a node, so I tried using relabelling rules like this, to no effect whatsoever:

relabel_configs:   - source_labels: ['nodename']     target_label: 'instance' 

I can manually relabel every target, but that requires hardcoding every hostname into Prometheus, which is not really nice. I see that the node exporter provides the metric node_uname_info that contains the hostname, but how do I extract it from there?

node_uname_info{domainname="(none)",machine="x86_64",nodename="myhostname",release="4.13.0-32-generic",sysname="Linux",version="..."} 1 
like image 723
Norrius Avatar asked Apr 18 '18 10:04

Norrius


People also ask

What is relabel in Prometheus?

Prometheus supports relabeling, which allows performing the following tasks: Adding new label. Updating existing label. Rewriting existing label. Updating metric name.

How do I add labels to Prometheus metrics?

Unfortunately, it is not possible to change the labels on old metrics in Prometheus. The storage is only updated by new scrapes and then it becomes immutable.

What is Relabel_configs?

Relabel configs allow you to select which targets you want scraped, and what the target labels will be. So if you want to say scrape this type of machine but not that one, use relabel_configs .


2 Answers

I juste came across this problem and the solution is to use a group_left to resolve this problem. You can't relabelled with a non existant value in the request, you are limited to the different parameters that you gave to prometheus or those that exists in the module use for the request (gcp,aws...).

So the solution I used is to combine an existing value containing what we want (the hostnmame) with a metric from the node exporter. Our answer exist inside the node_uname_info metrics wich contain the nodename value.

I used the answer to this post as a model for my request: https://stackoverflow.com/a/50357418 .

The solution is this one:

node_memory_Active * on(instance) group_left(nodename) (node_uname_info) 

With this, the node_memory_Active metrics wich contains only instance and job by default as a third value nodename that you can use in the description field of grafana.

Hope that this will help others.

like image 192
night-gold Avatar answered Sep 23 '22 18:09

night-gold


I found hardcode solution:

      global:       scrape_interval: 5s       scrape_timeout: 5s       external_labels:         monitor: 'Prometheus'      scrape_configs:      - job_name: 'shelby'       static_configs:       - targets:         - 10.100.0.01:9100       relabel_configs:       - source_labels: [__address__]         regex: '.*'         target_label: instance         replacement: 'shelby'      - job_name: 'camaro'       static_configs:       - targets:         - 10.101.0.02:9100       relabel_configs:       - source_labels: [__address__]         regex: '.*'         target_label: instance         replacement: 'camaro'      - job_name: 'verona'       static_configs:       - targets:         - 10.101.0.03:9100       relabel_configs:       - source_labels: [__address__]         regex: '.*'         target_label: instance         replacement: 'verona'  

Result:

      node_load15{instance="camaro",job="camaro"}    0.16     node_load15{instance="shelby",job="shelby"}    0.4     node_load15{instance="verona",job="verona"}    0.07  
like image 44
vitams Avatar answered Sep 20 '22 18:09

vitams