Example of backend datasource plugins writing data back to store?

For a frontend data source solution, you can check out this code example.

For a backend data source solution, you can register a resource handler, which extends the Grafana HTTP API with plugin-specific endpoints, which you can then call from your dashboard.

Since you specifically asked for examples of data source plugins, here’s how you extend your backend plugin with a resource handler:

func (d *SampleDatasource) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
	items, err := d.client.ListItems()
	if err != nil {
		return err
	}

	b, err := json.Marshal(items)
	if err != nil {
		return err
	}

	return sender.Send(&backend.CallResourceResponse{
		Status: http.StatusOK,
		Body:   b,
	})
}

You can then access the plugin resource from: /api/datasources/<your-plugin-id>/resources/, where your-plugin-id is an integer that identifies the data source instance you want to access.

Here’s the frontend code to query the data source resource:

const items = await getBackendSrv()
                .fetch<any>({
                  url: `/api/datasources/${props.datasource.id}/resources/`,
                })
                .toPromise();
2 Likes