Using template variable to select a datasource

I’m trying to use templates to select a datasource, based on a different template variable.

I have several Sensu servers deployed to different environments. Grafana can talk to each of these and has them configured as datasources (example: Sensu-Dev, Sensu-QA, Sensu-Prod). I have a template for accessing Graphite data from the different environments (Dev, QA, Prod).

I want my dashboard to be able to present data from both, but I want them to stay in sync without needing to set each template value individually. Basically, I want to have a $environment variable, and have that update a hidden template value for the Sensu datasource. If $environment is set to Prod, then the Sensu datasource becomes Sensu-Prod.

I have tried to set this up using a datasource template, with Sensu as my datasource type, and using the $environment variable in the filter in various ways, but it is not working out.

Any suggestions would be appreciated. TIA

Hi,

Support for using other variables in the datasource variable regex filter was just merged to master a couple of days ago.

This was merged after we branched of 4.2 (which is going to be released as stable next week). So this merge will only be available in 4.3

https://github.com/grafana/grafana/pull/7547

1 Like

Hello torkel, thanks for your response so far. I wanted to get clarity of this feature to understand if it is something that I am looking for.

Similar to kilteer, I have a Sensu use case. I have two template variables, $server, $application. I would like the value selected in the first list of $server to refresh / update a list of applications in the second list - $application

Yes, if the query you use for the application variable uses the sever variable it will be update every time you change the server variable

Appreciated … I’ll try that now.

Can not figure out how to do that. Have a variable (called env) of Type: Custom and want that selected value of this is used to prefix another variable of type: Datasource / Cloudwatch. Tried to write a regular expression in the instance name filter but doesn’t seem to work. Only getting “No datasource found”. Please can anyone point me in the right direction? Thanks

Are there data sources defined with the env in name?

  • For example -

Type: custom
values: dev,sit,prod

type: Datasource/Cloudwatch
name: $env-DS

Datasources:
Type: Cloudwatch
names:

  • dev-DS
  • prod-DS

The value of $env would be the first part of the data source name

1 Like

Hi, I cannot figure out how to use it.
I have multiple Prometheus instances and I have a Custom multi-value variable called PrometheusInstance with choices A,B,C.

I have 3 different datasources whose urls look like:
http://A.foo.com:9090 etc.

I would like the datasource to change based on the value of the variable and my 19 panels to display the metrics from the corresponding instance of Prometheus.

How to achieve this? Using $PrometheusInstance in the datasource url or name did not work.

1 Like