Skip to main content

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
Most modern data source plugins include a backend component for security and performance.

Frontend Implementation

Creating a Data Source Plugin

Data source plugins extend the DataSourcePlugin class from @grafana/data:

Implementing the DataSourceApi

The main data source class extends DataSourceApi (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 the QueryData 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 in plugin.json:

Key Configuration Options

  • metrics: Supports time series data
  • logs: Supports log data
  • tracing: Supports trace data
  • annotations: Supports annotations
  • alerting: Can be used in alerting rules
  • backend: Has a backend component
  • queryOptions: 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

From public/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:
  1. Build the plugin
  2. Copy to Grafana’s plugin directory
  3. Restart Grafana
  4. Add a new data source instance
  5. Test queries in Explore

Best Practices

  1. Use backend plugins for security: Keep credentials and API keys server-side
  2. Implement proper error handling: Return meaningful error messages
  3. Support streaming: Use Observables for real-time data
  4. Add query help: Provide a query editor help component
  5. Implement variable support: Allow template variables in queries
  6. Add annotation support: Enable event overlays on graphs
  7. Follow naming conventions: Use orgname-datasourcename-datasource for plugin ID
  8. 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 in secureJsonData:

Query Caching

Implement caching for expensive queries in the backend.

Resources