env:
nvm use 22
refer doc link: Build a data source backend plugin | Grafana Plugin Tools
fronend datasource.ts code, using DataSourceWithBackend default query method
export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptions> {
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
}
getDefaultQuery(_: CoreApp): Partial<MyQuery> {
return DEFAULT_QUERY;
}
applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars) {
return {
...query,
queryText: getTemplateSrv().replace(query.queryText, scopedVars),
};
}
filterQuery(query: MyQuery): boolean {
// if no query has been provided, prevent the query from being executed
return !!query.queryText;
}
backend query code
func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
backend.Logger.Info("query data!!!")
// create response struct
response := backend.NewQueryDataResponse()
// loop over queries and execute them individually.
for _, q := range req.Queries {
res := d.query(ctx, req.PluginContext, q)
// save the response in a hashmap
// based on with RefID as identifier
response.Responses[q.RefID] = res
}
return response, nil
}
plugin.json code
{
"$schema": "https://raw.githubusercontent.com/grafana/grafana/main/docs/sources/developers/plugins/plugin.schema.json",
"type": "datasource",
"name": "Example-Datasource",
"id": "oma-example-datasource",
"metrics": true,
"backend": true,
"executable": "gpx_example_datasource",
"info": {
"description": "",
"author": {
"name": "oma"
},
"keywords": ["datasource"],
"logos": {
"small": "img/logo.svg",
"large": "img/logo.svg"
},
"links": [],
"screenshots": [],
"version": "%VERSION%",
"updated": "%TODAY%"
},
"dependencies": {
"grafanaDependency": ">=10.4.0",
"plugins": []
}
}
and i follow the guid doc to run it using docker compose up, after the server run ,i can access grafana on localhots:3000, but the datasource query action did not call my backend api, look seems it call a grafana default query api ,return a json always like this:
{
"results": {
"A": {
"status": 200,
"frames": [
{
"schema": {
"name": "response",
"refId": "A",
"fields": [
{
"name": "time",
"type": "time",
"typeInfo": {
"frame": "time.Time"
}
},
{
"name": "values",
"type": "number",
"typeInfo": {
"frame": "int64"
}
}
]
},
"data": {
"values": [
[
1743001727830,
1743003527830
],
[
10,
20
]
]
}
}
]
}
}
}
how could it call my backend query api when dashboard depend on this datasource query somthing ? thank a lot !!!