You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/how-to-guides/data-source-plugins/add-support-for-variables.md
+70-6Lines changed: 70 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,13 +105,23 @@ Grafana queries your data source whenever you update a variable. Excessive updat
105
105
106
106
A [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) is a type of variable that allows you to query a data source for the values. By adding support for query variables to your data source plugin, users can create dynamic dashboards based on data from your data source.
107
107
108
+
There are two ways to do this:
109
+
110
+
- Implement the `metricFindQuery` method on your `DataSourceApi` class. This is the simplest option, but it's limited:
111
+
- It only works with Grafana's built-in string query input, so you can't render a custom variable query editor with it alone.
112
+
- The query is passed as a plain `string`, so you don't get the type safety or editor reuse of a typed query model.
113
+
- It returns a `Promise`, so it can't stream results the way an `Observable`-based query can.
114
+
- Assign a variable support class to the `variables` property of your data source. Extend one of the classes exported from `@grafana/data` when you want a custom or reusable query editor, typed query models, or streaming responses. Refer to [Choose a variable support class](#choose-a-variable-support-class).
115
+
116
+
This guide uses both: it implements `metricFindQuery` for the lookup and wraps it in a `CustomVariableSupport` class to provide a custom editor.
117
+
108
118
To add support for query variables, you need to:
109
119
110
-
1. Define a variable query model
111
-
2. Implement `metricFindQuery` in your data source
112
-
3. Create a `VariableQueryEditor` component
113
-
4. Create a `VariableSupport`class
114
-
5. Assign the `VariableSupport`to your data source
120
+
1. Define a variable query model.
121
+
2. Implement `metricFindQuery` in your data source.
122
+
3. Create a `VariableQueryEditor` component.
123
+
4. Create a variable support class. This guide extends `CustomVariableSupport`, but you can extend `DataSourceVariableSupport` or `StandardVariableSupport` instead. Refer to [Choose a variable support class](#choose-a-variable-support-class).
124
+
5. Assign the variable support class to your data source.
115
125
116
126
Let's start by defining a query model for the variable query:
117
127
@@ -294,7 +304,61 @@ export class DataSource extends DataSourceApi<MyQuery> {
294
304
}
295
305
```
296
306
297
-
That's it! You can now try out the plugin by adding a [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) to your dashboard.
307
+
That's it! Now you can try out the plugin by adding a [query variable](https://grafana.com/docs/grafana/latest/dashboards/variables/add-template-variables#add-a-query-variable) to your dashboard.
308
+
For a working reference implementation, see how the Grafana built-in TestData data source implements variable support in [variables.ts](https://github.com/grafana/grafana/blob/main/public/app/plugins/datasource/grafana-testdata-datasource/variables.ts).
309
+
310
+
#### Choose a variable support class
311
+
312
+
The previous steps use `CustomVariableSupport`, but `@grafana/data` exports three base classes you can assign to the `variables` property. Choose the one that matches the editor experience you want:
313
+
314
+
| Class | Use it when |
315
+
| --- | --- |
316
+
|`CustomVariableSupport`| You want to provide your own query editor component for variables. |
317
+
|`DataSourceVariableSupport`| Your data source's main query editor already works for variable queries. |
318
+
|`StandardVariableSupport`| You want to reuse Grafana's standard variable query editor. |
319
+
320
+
##### `CustomVariableSupport`
321
+
322
+
Provide an `editor` component and a `query` method that returns an `Observable`, as shown in the [steps above](#create-a-variablesupport-class).
323
+
324
+
##### `DataSourceVariableSupport`
325
+
326
+
Grafana reuses your existing query editor and `query` method, so there's nothing extra to implement:
0 commit comments