Skip to content

Grafana variables

Mike Nosal edited this page Jan 16, 2020 · 1 revision

Grafana variables and filtering

Grafana dashboards support the use of variables within panels to create more interactive visualizations. This page will show an example of defining and using a variable in a panel visualization. When a user selects a value for the variable, it can be used to filter the resulting values in the panel.

Create a variable

We will create a variable called fhir_version which will allow panels to filter their display based on the reported FHIR version of an endpoint response.

You add a variable from the Dashboard settings menu. Select the Variables tab on the left and you get a panel to manage your dashboard's variables.

Click "Add Variable". Name the variable "fhir_version" and label "FHIR Version" and select "Query" under Type.

In Data source options, select the PostgreSQL data source. To get the FHIR versions in the metrics, add a query:

SELECT DISTINCT value 
FROM metrics_values
WHERE labels_id IN (
  SELECT id as label_id
  FROM metrics_labels
  WHERE metric_name='AllEndpoints_fhir_version'
)

Leave Regex and Sort options disabled. In Selection Options, turn on the "Include All option".

You should see a Preview of the available values returned by the query. If you are not seeing the expected values, check your data source and query for any errors.

If it looks correct, click "Add" and you should have a new variable available for use in your visualizations.

Using the variable

The new FHIR Version variable should be displayed at the top of the dashboard.

Now, we can use the variable in our queries used to fetch data for a visualization. Create a new Panel, and set the Query to PostgreSQL.

We'll enter the SQL for our query directly. Click the Pencil icon ("Toggle text edit mode"). This will change the query dialog to a text field where you can enter your SQL.

To use a variable in your query, you will reference it using the $name notation. Our variable is called $fhir_version. You can control how the variable is resolved to actual values in your query. Since we are filtering on FHIR version, we'll have a query that uses an IN clause. Because there is an 'All' option available to the user, we want a comma separated list of values. For more information, view Grafana's documentation on Dashboard Variables.

If you write SELECT * FROM tablename WHERE version IN (${fhir_version:csv}) Grafana will render the query as SELECT * FROM tablename WHERE version IN (102,103). It substitutes the current value of the variable with the actual values returned by the query you had defined for your variable.

You can see this in action by expanding the Generated SQL option at the bottom of the Query dialog. Select a value from the "FHIR Version" dropdown at the top of the screen, and you should see your values change in the IN clause.

NOTE: Be careful writing your own queries, as Grafana tries to re-run the query each time the SQL is updated. This could result in an incomplete query being run, potentially returning more rows of data than you intended, possibly causing errors.

Clone this wiki locally