-
Notifications
You must be signed in to change notification settings - Fork 47
Docs: Add guide for data source plugin default variables and links #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
faaf724
docs: Add guide for data source plugin default variables and links
mckn 4027e76
chore: regenerate metadata.md from plugin.schema.json
mckn cec56bf
docs: fix documentation style per review feedback
mckn 765d687
docs: add prerequisites section and remove types reference
mckn e8532d5
Merge branch 'main' into mckn/docs-new-api-functions
mckn c9dac49
docs: document automatic variable name prefixing behavior
mckn c95174b
Merge branch 'main' into mckn/docs-new-api-functions
mckn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
...aurus/docs/how-to-guides/data-source-plugins/add-default-variables-and-links.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| id: add-default-variables-and-links | ||
| title: Add default variables and links | ||
| description: Learn how to register default variables and dashboard links from a data source plugin. | ||
| keywords: | ||
| - grafana | ||
| - plugins | ||
| - plugin | ||
| - datasource | ||
| - variables | ||
| - dashboard links | ||
| - default variables | ||
| - default links | ||
| --- | ||
|
|
||
| # Add default variables and links | ||
|
|
||
| :::note | ||
|
|
||
| This feature is available from Grafana 13.0 and later. | ||
|
|
||
| ::: | ||
|
|
||
| Data source plugins can register default variables and dashboard links that are automatically loaded onto any dashboard using that data source. This is useful when your data source requires certain filter variables or benefits from having quick-access links to external documentation or related tools. | ||
|
|
||
| ## Before you begin | ||
|
|
||
| Ensure your development environment meets the following prerequisites: | ||
|
|
||
| - **Grafana version:** Grafana 13.0 or later. | ||
| - **`@grafana/data`:** Version 13.0.0 or later. | ||
| - **`@grafana/schema`:** Version 13.0.0 or later. | ||
|
|
||
| ## Key behaviors | ||
|
|
||
| - **Not persisted.** Default variables and links are loaded at dashboard load time. They are not saved as part of the dashboard model. | ||
| - **URL sync.** Default variables appear in the URL as query parameters, so shared dashboard links preserve the selected values. | ||
| - **Read-only.** Default variables and links are displayed as read-only in the dashboard settings views. Users cannot edit or remove them from the dashboard. | ||
| - **Name prefixing.** Grafana automatically prefixes default variable names with the data source plugin ID to prevent collisions with user-defined template variables. For example, a variable named `environment` from a `prometheus` data source becomes `prometheus_environment` in the URL. The original `name` is used as the display label if no `label` is provided. | ||
|
|
||
| ## Add default variables | ||
|
|
||
| Implement the optional `getDefaultVariables` method on your `DataSourceApi` class to return an array of `VariableKind` objects using the v2 dashboard schema. Set `hide` to `'inControlsMenu'` so that default variables appear in the dashboard controls menu rather than the main variable bar: | ||
|
|
||
| ```ts title="src/datasource.ts" | ||
| import { DataSourceApi, DataSourceInstanceSettings } from '@grafana/data'; | ||
| import { VariableKind, DashboardLink } from '@grafana/schema/apis/dashboard.grafana.app/v2'; | ||
|
|
||
| import { MyQuery, MyDataSourceOptions } from './types'; | ||
|
|
||
| export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> { | ||
| constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) { | ||
| super(instanceSettings); | ||
| } | ||
|
|
||
| async getDefaultVariables(): Promise<VariableKind[]> { | ||
| return [ | ||
| { | ||
| kind: 'CustomVariable', | ||
| spec: { | ||
| name: 'environment', | ||
| label: 'Environment', | ||
| query: 'production,staging,development', | ||
| current: { | ||
| value: 'production', | ||
| text: 'Production', | ||
| }, | ||
| options: [ | ||
| { value: 'production', text: 'Production' }, | ||
| { value: 'staging', text: 'Staging' }, | ||
| { value: 'development', text: 'Development' }, | ||
| ], | ||
| allowCustomValue: false, | ||
| skipUrlSync: false, | ||
| hide: 'inControlsMenu', | ||
| multi: false, | ||
| }, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // ... other methods | ||
| } | ||
| ``` | ||
|
|
||
| ## Add default dashboard links | ||
|
|
||
| Implement the optional `getDefaultLinks` method on your `DataSourceApi` class to return an array of `DashboardLink` objects. Set `placement` to `'inControlsMenu'` so that default links appear in the dashboard controls menu: | ||
|
|
||
| ```ts title="src/datasource.ts" | ||
| async getDefaultLinks(): Promise<DashboardLink[]> { | ||
| return [ | ||
| { | ||
| title: 'Documentation', | ||
| type: 'link', | ||
| url: 'https://example.com/docs', | ||
| tooltip: '', | ||
| targetBlank: true, | ||
| icon: 'doc', | ||
| tags: [], | ||
| asDropdown: false, | ||
| includeVars: false, | ||
| keepTime: false, | ||
| placement: 'inControlsMenu', | ||
| }, | ||
| ]; | ||
| } | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.