Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I map one variable's values to another variable in Grafana?

In my Grafana dashboard (with Prometheus as a data source), I have a custom $tier variable, which allows the user to pick the tier from a dropdown. It's defined as:

Values separated by comma: production, stage, development

I need to filter a Prometheus metric by a label which contains a shortened version of the tier name:

  • "foo-dev"
  • "foo-stage"
  • "foo-prod"

I was thinking that I'd create a hidden variable $shortened_tier so I could use that in my query filter, like this:

my_label=~"foo-$shortened_tier"

I'd like to define it based on the value of $tier:

  • "development" -> "dev"
  • "stage" -> "stage"
  • "production" -> "prod"

How do I do that?

like image 226
Steve K Avatar asked Oct 11 '19 19:10

Steve K


1 Answers

I figured out a workaround for this, but it is suuuuper hacky:

Name: shortened_tier
Type: Query
Data Source: Prometheus
Query: label_values(up{env="$tier"}, env)
Regex: (dev|stage|prod).*

What I wanted to do was simply Query: $tier, but since Grafana wouldn't let me do that, I had to use a completely different metric (up) where I could pass in $tier and get back the same exact value as a string. Then I use regex to just look for dev|stage|prod at the beginning of the string, capture that part, and throw away the rest.

This has the result that I'm looking for, with the value of $shortened_tier dynamically changing based on the value that's selected and assigned to $tier. But man I wish Grafana had a less hacky way to do this.

like image 153
Steve K Avatar answered Nov 11 '22 20:11

Steve K