Dashboard templating variables: mapping values from another variable

I have a recurring requirement for dashboards where a user may pick from a variable dropdown and I then need other variables automatically set based on that variable.

This is fine when the secondary hidden variables can be queried from a datasource and I can use the primary variable in the datasource query. However, some of these variables are simply not queryable from datasources. For example:

Kubernetes cluster name: List pulled from prometheus using label_values(…)
Amazon cloudwatch log name: << not available anywhere

I had hoped that csv datasource plugin would allow me to setup a datasource where I could manually map values - however, I could not see a way to actually filter csv values for variables (i.e. setup a variable using csv datasource and select columnx where columny value is equal to another variable).

I have worked around the problem for now by installing mariadb on the server and manually creating some mapping tables. Then I have created a mysql datasource in Grafana and can then query that to transform the variables.

So my question is, is there a neater solution to this workaround? If not, might there be a plugin solution to this problem that could be developed? Ideally, I’d like to setup these variable mappings directly in Grafana.

I have had the same problem.

Thinking out loud, I see two possible solutions:

  • Extend the CSV datasource plugin so that it supports this kind of query; this makes sense to me because you can keep your mapping in some location under revision control, etc.
  • Write a datasource plugin specifically for this purpose; I suspect it would be extremely similar to the CSV datasource; the different might be that you could use JSON and use JSON path expressions to query the data.

There is a third solution, which works right now, but it’s a little clunky and it depends on your ability to create recording rules in Prometheus.

In Prometheus, this query does not produce a result:

dummy{label="value"}

(assuming there’s no metric called dummy, that is)

But this query does produce a result:

absent(dummy{label="value"})

the value is 1, it has a label named label with a value value.

If you create a recording rule (call it mapping_info), now you can query mapping_info and you’ll get a result like mapping_info{label="value"}.

More interestingly for your purposes, you can do this:

absent(dummy{a="1", b="x"})

Now you hava a mapping from a=1 to b=x.

Even more interestingly, this expression:

absent(dummy{a="1", b="x"})
OR
absent(dummy{a="2", b="y"})
OR
absent(dummy{a="42", b="z"})

will produce three metrics.

If you add that to your recording rule, now you have something useful. You can select your Prometheus datasource in the variable editor, and enter a query using label_values to fetch what you need. The first label’s value would come from your other variable, and you’d be extracting the value of the second label. Notice you can add more labels and you can have a more complete mapping.

Like I said, this all depends on dummy NOT existing. You can do various things to make sure that is true, but that’s the basic idea.

For the JSON datasource approach, there’s a plugin. I have not tried to see if this solves this problem.