Open-Source Wikis

/

Grafana

/

Packages

/

@grafana/runtime

grafana/grafana

@grafana/runtime

Runtime services that need a host. Source under packages/grafana-runtime/.

@grafana/data and @grafana/ui are pure libraries; @grafana/runtime is different — it exposes singletons that must be set up by the host (Grafana itself) before plugins can use them. The host calls setBackendSrv(...), setDataSourceSrv(...), setLocationService(...) during bootstrap (see Bootstrap and runtime). Plugins call the corresponding getBackendSrv() etc.

Core services

getBackendSrv()

Typed HTTP client. Returns a BackendSrv with get, post, put, delete, patch, fetch. Handles CSRF tokens, organization headers, and 401 redirects.

import { getBackendSrv } from '@grafana/runtime';

const me = await getBackendSrv().get<User>('/api/user');

locationService

Wraps the React Router history. push, replace, getLocation, partial (update query string), getSearchObject.

getDataSourceSrv()

Look up a configured datasource by UID, name, or ref. Returns a DataSourceApi instance (frontend plugin's DataSource class). Used by panels, explore, and external plugins that need to query other datasources.

getTemplateSrv()

Variable interpolation. replace('${var}'), getVariables(), containsTemplate(...). Used by datasource plugins to expand template variables in queries.

config

The frozen GrafanaBootConfig from window.grafanaBootData. Carries:

  • featureToggles — object keyed by toggle name.
  • theme2 — the active GrafanaTheme2.
  • bootData — user, settings, navTree.
  • datasources — initial map of UID → DataSourceInstanceSettings.
  • plugins — initial plugin metadata.
  • Build info (buildInfo.version, buildInfo.edition, buildInfo.commit).

Plugin extensions

usePluginExtensions(extensionPointId, context), usePluginLinks, usePluginComponent — hooks that let host UI render plugin-contributed components and links. The matching declaration goes in the plugin's plugin.json under extensions.addedComponents etc.

EchoSrv

Internal analytics dispatcher. Plugins typically don't use this directly; the host wires it to Sentry/GA/custom backends.

LocaleService, getEventBus, reportInteraction

Helpers for i18n boundary, an event bus, and analytics events.

How plugins consume

External plugins declare @grafana/data, @grafana/ui, and @grafana/runtime as peerDependencies so they share the host's instance at runtime. The bundled module.js does NOT bundle these; the host provides them.

How Grafana populates

Bootstrap in public/app/index.ts calls every set*Srv setter with concrete implementations:

  • setBackendSrv(new BackendSrv(...)) from public/app/core/services/backend_srv.ts.
  • setDataSourceSrv(new DatasourceSrv(...)) from public/app/features/plugins/datasource_srv.ts.
  • setTemplateSrv(new TemplateSrv()) from public/app/features/templating/template_srv.ts.
  • setLocationService(...) from public/app/core/navigation/locationService.ts.

Calling a get*Srv() before set*Srv() has been called throws. This is by design — plugins can rely on the services being available by the time their module evaluates.

Versioning notes

@grafana/runtime evolves carefully because external plugins depend on its public types. Adding new APIs is fine; removing or changing existing APIs requires a deprecation cycle and is communicated through release notes.

Key source files

File Purpose
packages/grafana-runtime/src/services/backendSrv.ts BackendSrv interface
packages/grafana-runtime/src/services/LocationService.ts Location service
packages/grafana-runtime/src/services/dataSourceSrv.ts DataSourceSrv interface
packages/grafana-runtime/src/config.ts config frozen object
packages/grafana-runtime/src/services/EchoSrv.ts Echo analytics
packages/grafana-runtime/src/services/pluginExtensions/ Plugin extension hooks

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

@grafana/runtime – Grafana wiki | Factory