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:
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
:
How do I do that?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With