Data Source Plugin Development
Data source plugins enable Grafana to query and visualize data from external sources. This guide covers the architecture and implementation of data source plugins.Overview
Data source plugins consist of:- Frontend: Query editor, configuration UI, and data formatting
- Backend (optional): Query execution, authentication, and data transformation
Frontend Implementation
Creating a Data Source Plugin
Data source plugins extend theDataSourcePlugin class from @grafana/data:
Implementing the DataSourceApi
The main data source class extendsDataSourceApi (from packages/grafana-data/src/types/datasource.ts):
Query Editor Component
The query editor allows users to build queries:Configuration Editor
The configuration editor manages data source settings:Backend Implementation
Plugin Structure
Backend plugins use the Grafana Plugin SDK for Go:Query Handler
Implement theQueryData method to handle queries:
Health Check
Implement health checks to verify connectivity:Resource Handler
Implement custom HTTP endpoints:Plugin.json Configuration
Data source plugins require specific metadata inplugin.json:
Key Configuration Options
metrics: Supports time series datalogs: Supports log datatracing: Supports trace dataannotations: Supports annotationsalerting: Can be used in alerting rulesbackend: Has a backend componentqueryOptions: Supported query options (minInterval, maxDataPoints)routes: HTTP routes for proxying to external APIs
Real-World Example: Prometheus
The Prometheus data source (public/app/plugins/datasource/prometheus/) is an excellent reference:
Frontend Entry Point
Plugin Metadata
Frompublic/app/plugins/datasource/prometheus/plugin.json:
Testing Data Sources
Unit Tests
Test your data source implementation:Integration Testing
Test the plugin in a running Grafana instance:- Build the plugin
- Copy to Grafana’s plugin directory
- Restart Grafana
- Add a new data source instance
- Test queries in Explore
Best Practices
- Use backend plugins for security: Keep credentials and API keys server-side
- Implement proper error handling: Return meaningful error messages
- Support streaming: Use Observables for real-time data
- Add query help: Provide a query editor help component
- Implement variable support: Allow template variables in queries
- Add annotation support: Enable event overlays on graphs
- Follow naming conventions: Use
orgname-datasourcename-datasourcefor plugin ID - Document your plugin: Include README and inline help
Common Patterns
Proxy Configuration
Use routes to proxy requests through Grafana:Secure Data Storage
Store sensitive data insecureJsonData:
Query Caching
Implement caching for expensive queries in the backend.Resources
- Build a data source plugin tutorial
- Plugin SDK for Go documentation
- Grafana Plugin Examples
- Source:
pkg/plugins/andpublic/app/plugins/datasource/