Grafana Plugin Examples

repository·main·Indexed 42 days ago

https://github.com/grafana/grafana-plugin-examples

A collection of reference implementations for Grafana plugin types, including App, Panel, and Data Source plugins. Showcases various architectures such as frontend-only and full-stack (with backend) plugins, providing guidance on development workflows, testing with Jest and Playwright, signing for distribution, and using the grafana-plugin-sdk-go.

Tokens
5K
Snippets
18
Records
22
Agent score
52%

What's inside Grafana Plugin Examples

  1. Available Grafana plugin examples

    main

    This repository provides several example plugins categorized by their type to demonstrate different use cases and architectures:

    App Plugins

    • app-basic: Demonstrates a basic app plugin using custom routing.
    • app-with-backend: Demonstrates an app plugin with custom routing and a backend component.

    Panel Plugins

    • panel-basic: Demonstrates a panel plugin that utilizes the time series graph from @grafana/ui to read and update the dashboard time range.

    Data Source Plugins

    • datasource-basic: Demonstrates a basic data source plugin.
    • datasource-with-backend: Demonstrates a data source plugin with a backend component.

    Note: These examples use NPM to manage frontend dependencies. While you may use Yarn or PNPM, official support is only provided for NPM.

  2. Automate plugin signing with GitHub Actions

    main

    If your project uses the GitHub Actions supplied by @grafana/create-plugin, signing is automated via a release workflow.

    Setup

    1. In your GitHub repository, go to Settings > Secrets > Actions.
    2. Create a New repository secret named GRAFANA_API_KEY.
    3. Paste your Grafana Cloud API key into the Secret field.

    Triggering a Release

    To trigger the signing and release workflow, push a version tag to GitHub:

    1. Bump the version: npm version <major|minor|patch>
    2. Push to origin: git push origin main --follow-tags
    npm version patch
    git push origin main --follow-tags
  3. Test a Grafana panel plugin

    main

    Testing involves unit tests (Jest), end-to-end tests (Playwright), and running the plugin within a Grafana instance via Docker.

    Unit Testing (Jest)

    • Watch mode: npm run test (requires git init first).
    • CI mode: npm run test:ci (runs all tests and exits).

    End-to-End Testing (Playwright)

    1. Start Grafana server: Run npm run server. You can specify a version using the GRAFANA_VERSION environment variable (defaults to latest).
    2. Run E2E tests: Run npm run e2e after the server is running.
    # Unit tests
    npm run test
    npm run test:ci
    
    # E2E tests
    GRAFANA_VERSION=11.3.0 npm run server
    npm run e2e
  4. Check Grafana API compatibility with Levitate

    main

    If your plugin is written in TypeScript, you can use @grafana/levitate to verify if the Grafana APIs you are consuming are compatible with specific versions of Grafana. This helps prevent breaking changes when upgrading Grafana core.

    To generate a compatibility report against the latest release of the specified Grafana packages, use the is-compatible command. You can target specific packages like @grafana/data, @grafana/ui, and @grafana/runtime.

    # Check against latest versions
    npx @grafana/levitate@latest is-compatible --path src/module.ts --target @grafana/data,@grafana/ui,@grafana/runtime
    
    # Check against specific versions
    npx @grafana/levitate@latest is-compatible --path src/module.ts --target @grafana/data@9.0.5,@grafana/ui@9.0.5,@grafana/runtime@9.0.5
  5. Set up the Frontend for a Data Source Plugin

    main

    To develop the frontend component of a Grafana data source plugin, use the following npm commands:

    • Install dependencies: Run npm install to set up the node environment.
    • Development mode: Run npm run dev to build the plugin in development mode with watch functionality.
    • Production build: Run npm run build to create a production-ready build.
    • Linting: Use npm run lint to check code quality or npm run lint:fix to automatically fix linting issues.
    npm install
    npm run dev
    npm run build
    npm run lint
    npm run lint:fix
  6. Distribute and sign a Grafana plugin

    main

    To distribute a plugin (privately or via the community), it must be signed so Grafana can verify its authenticity. Signing is not required during local development.

    Prerequisites for Signing

    1. Create a Grafana Cloud account.
    2. Ensure the first part of your plugin ID (found in plugin.json) matches your Grafana Cloud account slug (e.g., if your slug is acmecorp, your plugin ID must start with acmecorp-).
    3. Create a Grafana Cloud API key with the PluginPublisher role.

    Automated Signing via GitHub Actions

    If you are using the GitHub Actions workflow supplied with @grafana/create-plugin, you can automate signing by adding your API key as a repository secret:

    1. Navigate to Settings > Secrets > Actions in your GitHub repository.
    2. Create a New repository secret.
    3. Name: GRAFANA_API_KEY.
    4. Value: Your Grafana Cloud API key.

    Triggering a Release

    To trigger the automated signing and release workflow, push a version tag to GitHub:

    1. Bump the version:
      npm version <major|minor|patch>
    2. Push the tag:
      git push origin main --follow-tags
    npm version patch
    git push origin main --follow-tags
  7. Develop a Grafana panel plugin

    main

    Panel plugins allow you to add new types of visualizations to dashboards (e.g., maps, pie charts, lists). Use them to visualize data from queries, navigate between dashboards, or control external systems.

    To start development, follow these steps in the frontend directory:

    1. Install dependencies: npm install
    2. Development mode: Use npm run dev to build and run in watch mode.
    3. Production build: Use npm run build for a production-ready build.
    4. Linting: Run npm run lint to check code quality or npm run lint:fix to automatically fix issues.
    npm install
    npm run dev
    npm run build
    npm run lint
  8. Best practices for plugin README documentation

    main

    When preparing a README for a Grafana plugin, follow these structural and technical guidelines to ensure compatibility with Grafana.com:

    • Absolute URLs: Ensure all links in your README are absolute URLs. Relative links may fail when the README is rendered within the Grafana UI or on Grafana.com.

    Image Requirements

    • plugin.json: Use relative links for screenshots defined in the info section of your plugin.json.
    • README: Use absolute URLs for any images or screenshots included directly in the README.
    1. Badges: Use dynamic badges for versioning and downloads.
    2. Overview / Introduction: Explain the value proposition and include screenshots.
    3. Requirements: List all dependencies needed to run the plugin.
    4. Getting Started: Provide a quick start guide for configuration and usage.
    5. Documentation: Link to external or dedicated documentation.
    6. Contributing: Provide instructions for feedback and community contributions.
  9. Set up the Backend for an App Plugin

    main

    To initialize the backend of your Grafana app plugin, update the grafana-plugin-sdk-go dependency and build the binaries for multiple platforms using mage.

    1. Update SDK: Ensure you are using the latest minor version of the Go SDK.
    2. Build Binaries: Use mage to compile binaries for Linux, Windows, and Darwin.
    3. List Commands: Use mage -l to see all available build targets.
    # Update SDK
    go get -u github.com/grafana/grafana-plugin-sdk-go
    go mod tidy
    
    # Build binaries for Linux, Windows, and Darwin
    mage -v
    
    # List available Mage targets
    mage -l
  10. Sign a plugin using GitHub Actions

    main

    If your plugin uses the GitHub Actions workflow supplied by @grafana/create-plugin, signing is automated via a release workflow. You must first configure your Grafana Cloud API key as a GitHub Secret.

    1. Configure GitHub Secrets

    1. Navigate to Settings > Secrets > Actions in your GitHub repository.
    2. Click New repository secret.
    3. Name the secret GRAFANA_API_KEY.
    4. Paste your Grafana Cloud API key into the Secret field.
    5. Click Add secret.

    2. Trigger the Signing Workflow

    To start the automated signing and release process, push a new version tag to your repository:

    npm version <major|minor|patch>
    git push origin main --follow-tags
    npm version patch
    git push origin main --follow-tags
  11. Create dynamic marketplace badges for your plugin

    main

    You can use Shields.io combined with the Grafana.com API to create dynamic badges that automatically update when you publish new versions to the marketplace.

    To do this, use the URL parameter https://grafana.com/api/plugins/your-plugin-id and query the JSON response for specific fields. You can also include the grafana logo in the parameter.

    Available JSON query fields:

    • $.downloads: Total number of downloads.
    • $.version: The current catalog version.
    • $.grafanaDependency: The required Grafana version.
    • $.versionSignatureType: The version signature type.
    https://img.shields.io/badge/dynamic/json?logo=grafana&query=$.version&url=https://grafana.com/api/plugins/grafana-polystat-panel&label=Marketplace&prefix=v&color=F47A20