Use datasource based on another datasource variable

I have a case where I have Prometheus datasource called cluster1, Loki datasource called logs-cluster1, Tempo datasource called traces-cluster1 and Pyroscope datasource called profiles-cluster1

And I want to select the respective Loki/Tempo/Pyroscope datasource based on Prometheus datasource value.
So I created a datasource variable with Type Prometheus and tried to use it in the Instance name filter for other datasources. But it doesn’t work. It works fine in case I use Custom variable, but not one of type Datasource.

Did I do something wrong, or is it impossible to use datasource variables for templating?

You can achieve this in Grafana by creating template variables for each datasource and using them dynamically in your queries. This allows you to switch between datasources based on the selected variable.

1 Like

all variables will be independent in this case. But I want to use Prometheus datasource variable as the only that I need to change, and Loki/Tempo/Pyrosope datasources will be changed accordingly.
I mean if I select Prometheus as cluster2, Loki datasource will become logs-cluster2, Tempo - traces-cluster2 and Pyroscope - profiles-cluster2

Unfortunately, Grafana does not currently support using a datasource based on another datasource variable directly within a query. However, you can achieve similar functionality by using template variables and repeating panels. This involves setting up template variables based on the first datasource and then using those variables in queries for the second datasource.

1 Like

Try advance formatting in Instance name filter, e.g.:


Because /traces-$datasource/ may have datasource UID, not name there.

Unfortunately, this one doesn’t work as well

Are you sure that you are using some recent Grafana version, e. g. 10.3 and not any old one?

@jangaraj yup, I’m on the latest Grafana version:

Here is what my Prometheus datasource is look like:

Here are some values:

Here is what I tried to do for logs

and here is a list of available Loki datasources:

Debug how is ${datasource:text} interpolated in your case,pls. Check errors in browser console,…

Yeah, it was my mistake (datasources with and without MIMIR at the end)
@jangaraj you solutions works perfectly. Thanks a lot!