grafana/grafana
Datasources & queries
Querying a datasource in Grafana involves three layers:
- The datasource registry — the configured connection (URL, credentials, type). Lives in
pkg/services/datasources/. - The plugin host — the runtime that loads the datasource's backend plugin. Lives in
pkg/plugins/(see Plugin host). - The query orchestrator — fans queries out to plugins, merges results, applies expressions. Lives in
pkg/services/query/andpkg/expr/.
Datasource registry
pkg/services/datasources/ handles datasource CRUD, secret encryption, and per-org isolation:
pkg/services/datasources/
├── datasource.go # Service interface + types
├── service/ # Implementation
├── api/ # /api/datasources/* handlers
├── manager/ # Higher-level helpers
└── models/ # Persistence + DTOEach datasource has:
id(numeric, org-scoped) anduid(random string, org-scoped, the canonical reference).typematching a pluginid(e.g.prometheus,loki,mysql).url,access(proxyvsdirect),basicAuth, …- A
jsonDatablob (per-plugin settings) and asecureJsonDatablob (encrypted secrets viapkg/services/secrets/).
Datasource secrets are encrypted at rest with a per-instance encryption key, optionally wrapped by an external KMS configured in pkg/services/kmsproviders/.
Query orchestration
pkg/services/query/query.go is the entry point for /api/ds/query. It:
- Receives a list of
Queryobjects (one per panel target). - Authorizes the user against each referenced datasource (
datasources:query). - Groups queries by datasource and dispatches them to the corresponding backend plugin via the plugin host.
- Resolves any expression queries through
pkg/expr/. - Merges responses into a single
QueryDataResponse.
sequenceDiagram
participant UI as Panel/Explore
participant Query as services/query
participant Expr as expr engine
participant Plug as plugin host
participant TSDB as pkg/tsdb/<name>
UI->>Query: POST /api/ds/query
Query->>Query: authorize datasources
Query->>Query: group by datasource
Query->>Plug: QueryData(req)
Plug->>TSDB: gRPC QueryData
TSDB-->>Plug: QueryDataResponse (DataFrames)
Plug-->>Query: response
Query->>Expr: evaluate __expr__ queries (if any)
Query-->>UI: merged QueryDataResponseExpressions
pkg/expr/ implements server-side expressions: a small DSL that lets dashboards and alert rules combine, reduce, threshold, or transform results from one or more datasources before they hit the panel/condition.
Math— arithmetic over series ($A * 2 + $B).Reduce— collapse a series to a scalar (mean, max, last).Resample— uniform sampling.Threshold— boolean comparison.Classic conditions— Grafana-styleWHEN <reducer> OF query(<ref>) IS ABOVE <value>.SQL expression— newer SQL-over-frames experiment.
Used heavily in alerting and increasingly in dashboards through the __expr__ datasource type.
Datasource backends (pkg/tsdb/)
Each first-party datasource that needs server-side execution has a Go package under pkg/tsdb/<name>/ implementing the grafana-plugin-sdk-go QueryDataHandler interface:
prometheus,loki,tempo,jaeger,zipkin— observability backendscloudwatch,azuremonitor,cloud-monitoring— cloud monitoring vendorsmysql,mssql,grafana-postgresql-datasource,influxdb,opentsdb,graphite— database / TSDBparca,grafana-pyroscope-datasource— profilinggrafana-testdata-datasource,grafanads— internal/testexpr— the expression engine (registered as a "datasource" so the existing pipeline can run it)
These are compiled into the Grafana binary and loaded as in-process plugins. External datasource plugins follow the same SDK contract but run as separate processes spawned by the plugin host.
Datasource proxy
For datasources configured with access=proxy, the frontend doesn't talk to the datasource directly — it goes through /api/datasources/proxy/<id>/<path>. pkg/services/datasourceproxy/ and pkg/api/pluginproxy/ implement the proxy: they apply auth (e.g. inject API keys), enforce permissions, rewrite URLs, and forward.
This is mostly used by simple HTTP datasources that don't ship a backend plugin (e.g. simple JSON APIs).
Caching
pkg/services/caching/ is a hook point for caching query results — used by Grafana Cloud / Enterprise to cache expensive queries between dashboard renders. The OSS hook is a no-op pass-through.
Query history
User-issued Explore queries are persisted to pkg/services/queryhistory/ so users can review and re-run them.
Key source files
| File | Purpose |
|---|---|
pkg/services/query/query.go |
/api/ds/query orchestrator |
pkg/services/datasources/datasource.go |
Datasource service |
pkg/expr/ |
Server-side expression engine |
pkg/services/datasourceproxy/ |
HTTP proxy for access=proxy datasources |
pkg/api/pluginproxy/ |
Plugin proxy implementation |
pkg/tsdb/<name>/ |
Per-datasource backends |
See TSDB query backends for a tour of the individual pkg/tsdb/ packages and Plugin host for how plugins are loaded and dispatched.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.