grafanagrafana-variable

How to use Grafana template variable to select datasource


I have multiple Prometheus instances and I have a Custom multi-value variable PrometheusInstance with values A,B,C.

I have 3 different data sources whose URLs look like this:
http://A.foo.com:9090 etc.

I would like the data source 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 data source URL or name did not work.

Grafana version 9.1.6


Solution

  • First of all, you cannot dynamically specify address for your data source. Main reason: variables exist in the scope of dashboard, but data source is in global scope.

    If all you need is to allow to switch between data sources on dashboard, you can create variable with variable type "Data source", set type to "Prometheus". After that you'll need to go through your panels, and change data source to this variable (in drop-down for data source).

    Selecting the value for this variable will look exactly the same as other variables, with value populated by names of existing Prometheus data sources.

    Note, your data sources have to be configured manually as usual.

    If you need to also use it in ways, other then selecting data source for panels, you can still use it as a usual variable ${variable_name} ${variable_name:text} (thanks @allen for pointing out changes), and data source name will be used as a value.