Search code examples
prometheusgrafanaexporter

Grafana replace Variables


I would like some help with replacing variable values. I'm using Grafana + Cloudwatch Exporter.

I'm collecting metrics from aws DMS. When I run: label_values(replication_task_identifier) i have output: 5YUKKRZ6G5RYDW6UDR4 and **75UKQGIYYJMORPCMXP5T4.

The logic would be: **These IDS are task names. would need to replace 5YUKKRZ6G5RYDW6UDR4 per task-prod and 75UKQGIYYJMORPCMXP5T4 per task-prod-v1.

img1

img2

img3

I'm starting on grafana now.


Solution

  • If you have no way of knowing what value should be assigned to each semantic name, obviously it cannot be done through some query.

    But since you values are more or less long lived and constant, you can change your variable to type "Custom" and supply its values like this (screenshot):

    prod-test : 5YUKKRZ6G5RYDW6UDR4, prod-test-v1 : 75UKQGIYYJMORPCMXP5T4
    

    That way in your drop-down you'll see labels, but actual values will be substituted in queries.

    In the result, you'll get something like this: enter image description here

    Take into account, that this will change only the way, how variables shown in drop-down. Legends of graphs will still show labels as they are in Prometheus.


    If you want to replace labels everywhere, I believe your best shot will be to introduce relabeling into configuration of Prometheus, that will replace labels at scraping.

    Your relabeling config would look something like this:

    - source_labels: [replication_task_identifier]
      regex: "5YUKKRZ6G5RYDW6UDR4.+"
      replacement: "prod-prod"
      target_label: "replication_task_name"
      action: "replace"
    - source_labels: [replication_task_identifier]
      regex: "75UKQGIYYJMORPCMXP5T4.+"
      replacement: "prod-prod-v1"
      target_label: "replication_task_name"
      action: "replace"
    

    More examples on how to do relabeling can be found here.