Plugin migration guide

Introduction

This guide helps you identify the steps you need to take based on the Grafana version your plugin supports and explains how to migrate the plugin to the 8.2.x or a later version.

Note: If you’ve successfully migrated your plugin using this guide, then share your experiences with us! If you find missing information, then we encourage you to submit an issue on GitHub so that we can improve this guide!

Table of contents

From version 8.3.x to 8.4.x

This section explains how to migrate Grafana v8.3.x plugins to the updated plugin system available in Grafana v8.4.x. Depending on your plugin, you need to perform one or more of the following steps.

Value Mapping Editor has been removed from @grafana-ui library

Removed due to being an internal component.

Thresholds Editor has been removed from @grafana-ui library

Removed due to being an internal component.

8.4 deprecations

LocationService replaces getLocationSrv

In a previous release, we migrated to use a new routing system and introduced a new service for managing locations, navigation, and related information. In this release, we are making that new service the primary service.

Example: Import the service.

  1. // before
  2. import { getLocationSrv } from '@grafana/runtime';
  3. // after
  4. import { locationService } from '@grafana/runtime';

Example: Navigate to a path and add a new record in the navigation history so that you can navigate back to the previous one.

  1. // before
  2. getLocationSrv.update({
  3. path: '/route-to-navigate-to',
  4. replace: false,
  5. });
  6. // after
  7. locationService.push('/route-to-navigate-to');

Example: Navigate to a path and replace the current record in the navigation history.

  1. // before
  2. getLocationSrv.update({
  3. path: '/route-to-navigate-to',
  4. replace: true,
  5. });
  6. // after
  7. locationService.replace('/route-to-navigate-to');

Example: Update the search or query parameter for the current route and add a new record in the navigation history so that you can navigate back to the previous one.

  1. // How to navigate to a new path
  2. // before
  3. getLocationSrv.update({
  4. query: {
  5. value: 1,
  6. },
  7. partial: true,
  8. replace: false,
  9. });
  10. // after
  11. locationService.partial({ value: 1 });

Example: Update the search or query parameter for the current route and add replacing it in the navigation history.

  1. // before
  2. getLocationSrv.update({
  3. query: {
  4. 'var-variable': 1,
  5. },
  6. partial: true,
  7. replace: true,
  8. });
  9. // after
  10. locationService.partial({ 'var-variable': 1 }, true);

From version 7.x.x to 8.x.x

This section explains how to migrate Grafana v7.x.x plugins to the updated plugin system available in Grafana v8.x.x. Depending on your plugin, you need to perform one or more of the following steps. We have documented the breaking changes in Grafana v8.x.x and the steps you need to take to upgrade your plugin.

Backend plugin v1 support has been dropped

Use the new plugin sdk to run your backend plugin running in Grafana 8.

1. Add dependency on grafana-plugin-sdk-go

Add a dependency on the https://github.com/grafana/grafana-plugin-sdk-go. We recommend using go modules to manage your dependencies.

2. Update the way you bootstrap your plugin

Update your main package to bootstrap via the new plugin sdk.

  1. // before
  2. package main
  3. import (
  4. "github.com/grafana/grafana_plugin_model/go/datasource"
  5. hclog "github.com/hashicorp/go-hclog"
  6. plugin "github.com/hashicorp/go-plugin"
  7. "github.com/myorgid/datasource/pkg/plugin"
  8. )
  9. func main() {
  10. pluginLogger.Debug("Running GRPC server")
  11. ds, err := NewSampleDatasource(pluginLogger);
  12. if err != nil {
  13. pluginLogger.Error("Unable to create plugin");
  14. }
  15. plugin.Serve(&plugin.ServeConfig{
  16. HandshakeConfig: plugin.HandshakeConfig{
  17. ProtocolVersion: 1,
  18. MagicCookieKey: "grafana_plugin_type",
  19. MagicCookieValue: "datasource",
  20. },
  21. Plugins: map[string]plugin.Plugin{
  22. "myorgid-datasource": &datasource.DatasourcePluginImpl{Plugin: ds},
  23. },
  24. GRPCServer: plugin.DefaultGRPCServer,
  25. })
  26. }
  27. // after
  28. package main
  29. import (
  30. "os"
  31. "github.com/grafana/grafana-plugin-sdk-go/backend/log"
  32. "github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
  33. "github.com/myorgid/datasource/pkg/plugin"
  34. )
  35. func main() {
  36. log.DefaultLogger.Debug("Running GRPC server")
  37. if err := datasource.Manage("myorgid-datasource", NewSampleDatasource, datasource.ManageOpts{}); err != nil {
  38. log.DefaultLogger.Error(err.Error())
  39. os.Exit(1)
  40. }
  41. }

3. Update the plugin package

Update your plugin package to use the new plugin sdk.

  1. // before
  2. package plugin
  3. import (
  4. "context"
  5. "github.com/grafana/grafana_plugin_model/go/datasource"
  6. "github.com/hashicorp/go-hclog"
  7. )
  8. func NewSampleDatasource(pluginLogger hclog.Logger) (*SampleDatasource, error) {
  9. return &SampleDatasource{
  10. logger: pluginLogger,
  11. }, nil
  12. }
  13. type SampleDatasource struct{
  14. logger hclog.Logger
  15. }
  16. func (d *SampleDatasource) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
  17. d.logger.Info("QueryData called", "request", req)
  18. // logic for querying your datasource.
  19. }
  20. // after
  21. package plugin
  22. import (
  23. "context"
  24. "github.com/grafana/grafana-plugin-sdk-go/backend"
  25. "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
  26. "github.com/grafana/grafana-plugin-sdk-go/backend/log"
  27. "github.com/grafana/grafana-plugin-sdk-go/data"
  28. )
  29. func NewSampleDatasource(_ backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
  30. return &SampleDatasource{}, nil
  31. }
  32. type SampleDatasource struct{}
  33. func (d *SampleDatasource) Dispose() {
  34. // Clean up datasource instance resources.
  35. }
  36. func (d *SampleDatasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
  37. log.DefaultLogger.Info("QueryData called", "request", req)
  38. // logic for querying your datasource.
  39. }
  40. func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
  41. log.DefaultLogger.Info("CheckHealth called", "request", req)
  42. // The main use case for these health checks is the test button on the
  43. // datasource configuration page which allows users to verify that
  44. // a datasource is working as expected.
  45. }

Sign and load backend plugins

We strongly recommend that you not allow unsigned plugins in your Grafana installation. By allowing unsigned plugins, we cannot guarantee the authenticity of the plugin, which could compromise the security of your Grafana installation.

To sign your plugin, see Sign a plugin.

You can still run and develop an unsigned plugin by running your Grafana instance in development mode. Alternatively, you can use the allow_loading_unsigned_plugins configuration setting.

Update react-hook-form from v6 to v7

We have upgraded react-hook-form from version 6 to version 7. To make your forms compatible with version 7, refer to the react-hook-form-migration-guide.

Update the plugin.json

The property that defines which Grafana version your plugin supports has been renamed and now it is a range instead of a specific version.

  1. // before
  2. {
  3. "dependencies": {
  4. "grafanaVersion": "7.5.x",
  5. "plugins": []
  6. }
  7. }
  8. // after
  9. {
  10. "dependencies": {
  11. "grafanaDependency": ">=8.0.0",
  12. "plugins": []
  13. }
  14. }

Update imports to match emotion 11

Grafana uses Emotion library to manage frontend styling. We have updated the Emotion package and this can affect your frontend plugin if you have custom styling. You only need to update the import statements to get it working in Grafana 8.

  1. // before
  2. import { cx, css } from 'emotion';
  3. // after
  4. import { cx, css } from '@emotion/css';

Update needed for app plugins using dashboards

To make side navigation work properly - app plugins targeting Grafana 8.+ and integrating into the side menu via addToNav property need to adjust their plugin.json and all dashboard json files to have a matching uid.

plugin.json

  1. {
  2. "id": "plugin-id",
  3. // ...
  4. "includes": [
  5. {
  6. "type": "dashboard",
  7. "name": "(Team) Situation Overview",
  8. "path": "dashboards/example-dashboard.json",
  9. "addToNav": true,
  10. "defaultNav": false,
  11. "uid": "l3KqBxCMz"
  12. }
  13. ]
  14. // ...
  15. }

dashboards/example-dashboard.json

  1. {
  2. // ...
  3. "title": "Example Dashboard",
  4. "uid": "l3KqBxCMz",
  5. "version": 1
  6. // ...
  7. }

8.0 deprecations

Grafana theme v1

In Grafana 8 we have introduced a new improved version of our theming system. The previous version of the theming system is still available but is deprecated and will be removed in the next major version of Grafana.

You can find more detailed information on how to apply the v2 theme here.

How to style a functional component

The useStyles hook is the preferred way to access the theme when styling. It provides basic memoization and access to the theme object.

  1. // before
  2. import React, { ReactElement } from 'react';
  3. import css from 'emotion';
  4. import { GrafanaTheme } from '@grafana/data';
  5. import { useStyles } from '@grafana/ui';
  6. function Component(): ReactElement | null {
  7. const styles = useStyles(getStyles);
  8. }
  9. const getStyles = (theme: GrafanaTheme) => ({
  10. myStyle: css`
  11. background: ${theme.colors.bodyBg};
  12. display: flex;
  13. `,
  14. });
  15. // after
  16. import React, { ReactElement } from 'react';
  17. import { css } from '@emotion/css';
  18. import { GrafanaTheme2 } from '@grafana/data';
  19. import { useStyles2 } from '@grafana/ui';
  20. function Component(): ReactElement | null {
  21. const theme = useStyles2(getStyles);
  22. }
  23. const getStyles = (theme: GrafanaTheme2) => ({
  24. myStyle: css`
  25. background: ${theme.colors.background.canvas};
  26. display: flex;
  27. `,
  28. });

How to use the theme in a functional component

  1. // before
  2. import React, { ReactElement } from 'react';
  3. import { useTheme } from '@grafana/ui';
  4. function Component(): ReactElement | null {
  5. const theme = useTheme();
  6. }
  7. // after
  8. import React, { ReactElement } from 'react';
  9. import { useTheme2 } from '@grafana/ui';
  10. function Component(): ReactElement | null {
  11. const theme = useTheme2();
  12. // Your component has access to the theme variables now
  13. }

How to use the theme in a class component

  1. // before
  2. import React from 'react';
  3. import { Themeable, withTheme } from '@grafana/ui';
  4. type Props = {} & Themeable;
  5. class Component extends React.Component<Props> {
  6. render() {
  7. const { theme } = this.props;
  8. // Your component has access to the theme variables now
  9. }
  10. }
  11. export default withTheme(Component);
  12. // after
  13. import React from 'react';
  14. import { Themeable2, withTheme2 } from '@grafana/ui';
  15. type Props = {} & Themeable2;
  16. class Component extends React.Component<Props> {
  17. render() {
  18. const { theme } = this.props;
  19. // Your component has access to the theme variables now
  20. }
  21. }
  22. export default withTheme2(Component);

Gradually migrating components

If you need to use both the v1 and v2 themes due to using migrated and non-migrated components in the same context, use the v1 property on the v2 theme as described in the following example.

  1. function Component(): ReactElement | null {
  2. const theme = useTheme2();
  3. return (
  4. <NonMigrated theme={theme.v1}>
  5. <Migrated theme={theme] />
  6. </NonMigrate>
  7. );
  8. };

From version 6.2.x to 7.4.0

Legend components

The Legend components have been refactored and introduced the following changes within the @grafana/ui package.

  1. // before
  2. import { LegendItem, LegendOptions, GraphLegend } from '@grafana/ui';
  3. // after
  4. import { VizLegendItem, VizLegendOptions, VizLegend } from '@grafana/ui';
  • LegendPlacement has been updated from 'under' | 'right' | 'over' to 'bottom' | 'right' so you can not place the legend above the visualization anymore.
  • The isVisible in the LegendItem has been renamed to disabled in VizLegendItem.

From version 6.5.x to 7.3.0

getColorForTheme changes

The getColorForTheme function arguments have changed from (color: ColorDefinition, theme?: GrafanaThemeType) to (color: string, theme: GrafanaTheme).

  1. // before
  2. const color: ColorDefinition = {
  3. hue: 'green';
  4. name: 'dark-green';
  5. variants: {
  6. light: '#19730E'
  7. dark: '#37872D'
  8. };
  9. }
  10. const themeType: GrafanaThemeType = 'dark';
  11. const themeColor = getColorForTheme(color, themeType);
  12. // after
  13. const color = 'green';
  14. const theme: GrafanaTheme = useTheme();
  15. const themeColor = getColorForTheme(color, theme);

From version 6.x.x to 7.0.0

What’s new in Grafana 7.0?

Grafana 7.0 introduced a whole new plugin platform based on React. The new platform supersedes the previous Angular-based plugin platform.

Plugins built using Angular still work for the foreseeable future, but we encourage new plugin authors to develop with the new platform.

New data format

Along with the move to React, the new plugin platform introduced a new internal data format called data frames.

Previously, data source plugins could send data either as time series or tables. With data frames, data sources can send any data in a table-like structure. This gives you more flexibility to visualize your data in Grafana.

Improved TypeScript support

While the previous Angular-based plugin SDK did support TypeScript, for the React platform, we’ve greatly improved the support. All our APIs are now TypeScript, which might require existing code to update to the new stricter type definitions. Grafana 7.0 also introduced several new APIs for plugin developers that take advantage of many of the new features in Grafana 7.0.

Grafana Toolkit

With Grafana 7.0, we released a new tool for making it easier to develop plugins. Before, you’d use Gulp, Grunt, or similar tools to generate the minified assets. Grafana Toolkit takes care of building and testing your plugin without complicated configuration files.

For more information, refer to @grafana/toolkit.

Field options

Grafana 7.0 introduced the concept of field options, a new way of configuring your data before it gets visualized. Since this was not available in previous versions, any plugin that enables field-based configuration will not work in previous versions of Grafana.

For plugins prior to Grafana 7.0, all options are considered Display options. The tab for field configuration isn’t available.

Backend plugins

While backend plugins were available as an experimental feature in previous versions of Grafana, the support has been greatly improved for Grafana 7. Backend plugins for Grafana 7.0 are backwards-compatible and will continue to work. However, the old backend plugin system has been deprecated, and we recommend that you use the new SDK for backend plugins.

Since Grafana 7.0 introduced signing of backend plugins, community plugins won’t load by default if they’re unsigned.

To learn more, refer to Backend plugins.

Migrate a plugin from Angular to React

If you’re looking to migrate a plugin to the new plugin platform, then we recommend that you release it under a new major version. Consider keeping a release branch for the previous version to be able to roll out patch releases for versions prior to Grafana 7.

While there’s no 1-to-1 migration path from an Angular plugin to the new React platform, from early adopters, we’ve learned that one of the easiest ways to migrate is to:

  1. Create a new branch called migrate-to-react.
  2. Start from scratch with one of the templates provided by Grafana Toolkit.
  3. Move the existing code into the new plugin incrementally, one component at a time.

Migrate a panel plugin

Prior to Grafana 7.0, you would export a MetricsPanelCtrl from module.ts.

src/module.ts

  1. import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk';
  2. class MyPanelCtrl extends MetricsPanelCtrl {
  3. // ...
  4. }
  5. export { MyPanelCtrl as PanelCtrl };

Starting with 7.0, plugins now export a PanelPlugin from module.ts where MyPanel is a React component containing the props from PanelProps.

src/module.ts

  1. import { PanelPlugin } from '@grafana/data';
  2. export const plugin = new PanelPlugin<MyOptions>(MyPanel);

src/MyPanel.tsx

  1. import { PanelProps } from '@grafana/data';
  2. interface Props extends PanelProps<SimpleOptions> {}
  3. export const MyPanel: React.FC<Props> = ({ options, data, width, height }) => {
  4. // ...
  5. };

Migrate a data source plugin

While all plugins are different, we’d like to share a migration process that has worked for some of our users.

  1. Define your configuration model and ConfigEditor. For many plugins, the configuration editor is the simplest component so it’s a good candidate to start with.
  2. Implement the testDatasource() method on the class that extends DataSourceApi using the settings in your configuration model to make sure you can successfully configure and access the external API.
  3. Implement the query() method. At this point, you can hard-code your query, because we haven’t yet implemented the query editor. The query() method supports both the new data frame response and the old TimeSeries response, so don’t worry about converting to the new format just yet.
  4. Implement the QueryEditor. How much work this requires depends on how complex your query model is.

By now, you should be able to release your new version.

To fully migrate to the new plugin platform, convert the time series response to a data frame response.

Migrate to data frames

Before 7.0, data source and panel plugins exchanged data using either time series or tables. Starting with 7.0, plugins use the new data frame format to pass data from data sources to panels.

Grafana 7.0 is backward compatible with the old data format used in previous versions. Panels and data sources using the old format will still work with plugins using the new data frame format.

The DataQueryResponse returned by the query method can be either a LegacyResponseData or a DataFrame.

The toDataFrame() function converts a legacy response, such as TimeSeries or Table, to a DataFrame. Use it to gradually move your code to the new format.

  1. import { toDataFrame } from '@grafana/data';
  1. async query(options: DataQueryRequest<MyQuery>): Promise<DataQueryResponse> {
  2. return {
  3. data: options.targets.map(query => {
  4. const timeSeries: TimeSeries = await doLegacyRequest(query);
  5. return toDataFrame(timeSeries);
  6. }
  7. };
  8. }

For more information, refer to Data frames.

Troubleshoot plugin migration

As of Grafana 7.0, backend plugins can now be cryptographically signed to verify their origin. By default, Grafana ignores unsigned plugins. For more information, refer to Allow unsigned plugins.