Add support for variables in plugins
This guide explains how to leverage template variables in your panel plugins and data source plugins.
We’ll see how you can turn a string like this:
into
Grafana provides a couple of helper functions to interpolate variables in a string template. Let’s see how you can use them in your plugin.
For panels, the function is available in the PanelProps.
Add replaceVariables
to the argument list, and pass it a user-defined template string.
export const SimplePanel: React.FC<Props> = ({ options, data, width, height, replaceVariables }) => {
const query = replaceVariables('Now displaying $service');
return <div>{query}</div>;
};
For data sources, you need to use the getTemplateSrv, which returns an instance of .
Import
getTemplateSrv
from theruntime
package.import { getTemplateSrv } from '@grafana/runtime';
In your
query
method, call thereplace
method with a user-defined template string.async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
const query = getTemplateSrv().replace('SELECT * FROM services WHERE id = "$service"', options.scopedVars);
const data = makeDbQuery(query);
return { data };
}
A data source can define the default format option when no format is specified by adding a third argument to the interpolation function.
Let’s change the SQL query to use CSV format by default:
Now, when users write $service
, the query looks like this:
SELECT * FROM services WHERE id IN (admin,auth,billing)
For more information on the available variable formats, refer to .
Not only can you read the value of a variable, you can also update the variable from your plugin. Use to update a variable using query parameters.
The following example shows how to update a variable called service
.
query
contains the query parameters you want to update. Query parameters controlling variables are prefixed withvar-
.partial: true
makes the update only affect the query parameters listed inquery
, and leaves the other query parameters unchanged.
getLocationSrv().update({
query: {
'var-service': 'billing',
},
partial: true,
replace: true,
});
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.
Let’s start by defining a query model for the variable query.
export interface MyVariableQuery {
namespace: string;
rawQuery: string;
}
Note: By default, Grafana provides a default query model and editor for simple text queries. If that’s all you need, then you can leave the query type as
string
.
async metricFindQuery(query: string, options?: any)
Let’s create a custom query editor to allow the user to edit the query model.
Create a
VariableQueryEditor
component.``` import React, { useState } from ‘react’; import { MyVariableQuery } from ‘./types’;
interface VariableQueryProps { query: MyVariableQuery; onChange: (query: MyVariableQuery, definition: string) => void; }
export const VariableQueryEditor: React.FC
= ({ onChange, query }) => { const [state, setState] = useState(query); const saveQuery = () => {
onChange(state, `${state.query} (${state.namespace})`);
};
const handleChange = (event: React.FormEvent
) => setState({
...state,
[event.currentTarget.name]: event.currentTarget.value,
});
return (
<>
<div className="gf-form">
name="namespace"
className="gf-form-input"
onBlur={saveQuery}
onChange={handleChange}
value={state.namespace}
/>
</div>
<div className="gf-form">
<span className="gf-form-label width-10">Query</span>
<input
name="rawQuery"
className="gf-form-input"
onBlur={saveQuery}
onChange={handleChange}
value={state.rawQuery}
/>
</div>
</>
);
};
```
Grafana saves the query model whenever one of the text fields loses focus (`onBlur`) and then previews the values returned by `metricFindQuery`.
That’s it! You can now try out the plugin by adding a query variable to your dashboard.