datasources-provisioning
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWorkflow
工作流程
1. Ask the starting point: from scratch, or from an existing data source?
1. 确认起始方式:从零开始,还是基于现有数据源?
Ask this before anything else (skip only if the user already made it clear):
- From scratch — the user names a plugin type to provision → continue with step 2.
- From an existing data source in a running instance → jump to Convert an existing data source, then return to step 6.
在开始任何操作前先询问此问题(仅当用户已明确说明时可跳过):
- 从零开始 — 用户指定要配置的插件类型 → 继续步骤2。
- 基于运行实例中的现有数据源 → 跳转至转换现有数据源,完成后返回步骤6。
2. Resolve the full plugin id
2. 获取完整的插件ID
Provisioning needs the canonical plugin id (), not the short name a user might say.
<org>-<name>-datasource- Already canonical (contains or
-datasource)? Use as-is:-app.yesoreyeram-infinity-datasource - Short name only (e.g. ,
infinity)? Search the catalog API withclickhouse:filter=<keyword>Multiple matches → show the candidates and ask which one.bashcurl -s "https://grafana.com/api/plugins?filter=infinity" \ | jq -r '.items[] | "\(.slug)\t\(.name)"' # → yesoreyeram-infinity-datasource Infinity
The snippets below use Infinity () as the worked example — substitute the id resolved here (and the version from step 3) in every command and output.
yesoreyeram-infinity-datasource配置需要标准的插件ID(格式),而非用户可能提到的简称。
<org>-<name>-datasource- 已为标准格式(包含或
-datasource)?直接使用:-app。yesoreyeram-infinity-datasource - 仅提供简称(如、
infinity)?使用clickhouse调用目录API搜索:filter=<keyword>存在多个匹配结果时 → 列出候选选项并询问用户选择哪一个。bashcurl -s "https://grafana.com/api/plugins?filter=infinity" \ | jq -r '.items[] | "\(.slug)\t\(.name)"' # → yesoreyeram-infinity-datasource Infinity
以下示例将使用Infinity()作为演示——请将此处获取的ID(以及步骤3获取的版本)替换到所有命令和输出中。
yesoreyeram-infinity-datasource3. Resolve the latest version
3. 获取最新版本
bash
curl -s "https://grafana.com/api/plugins/yesoreyeram-infinity-datasource" | jq -r '.version'Never hardcode a version — the CDN path is version-pinned and a stale version 404s.
bash
curl -s "https://grafana.com/api/plugins/yesoreyeram-infinity-datasource" | jq -r '.version'切勿硬编码版本——CDN路径与版本绑定,过时版本会返回404错误。
4. Fetch the settings schema (primary structured source)
4. 获取设置Schema(主要结构化来源)
https://plugins-cdn.grafana.net/<PLUGIN_ID>/<VERSION>/public/plugins/<PLUGIN_ID>/schema/dsconfig.jsonbash
ID=yesoreyeram-infinity-datasource
VER=$(curl -s "https://grafana.com/api/plugins/$ID" | jq -r '.version')
curl -sf "https://plugins-cdn.grafana.net/$ID/$VER/public/plugins/$ID/schema/dsconfig.json"This file conforms to the dsconfig schema spec — the source of truth for how to interpret it. Don't re-derive field semantics from memory ( alone spans , , , , , , ); consult the spec when a field isn't a plain scalar:
valueTypestringnumberbooleanarrayobjectmapany- Prose spec: https://raw.githubusercontent.com/grafana/dsconfig/refs/heads/main/dsconfig/schema.md
- Meta-schema (defines the format of every ): https://raw.githubusercontent.com/grafana/dsconfig/refs/heads/main/dsconfig/schema.json
dsconfig.json
What you need from each field to provision: (the provisioning key), , ( | | ), and (honor for selectors like ). Orientation example ():
keyvalueTypetargetrootjsonDatasecureJsonDatavalidationsallowedValuesauth_methodschemaVersion: "v1"json
{
"pluginType": "yesoreyeram-infinity-datasource",
"fields": [
{
"key": "auth_method",
"valueType": "string",
"target": "jsonData",
"validations": [
{
"type": "allowedValues",
"values": [
"none",
"basicAuth",
"apiKey",
"bearerToken",
"oauth2",
"aws",
"azureBlob"
]
}
]
}
]
}Select only the fields relevant to what the user asked for (chosen auth method + connection), not all of them. Each field's tells you which auth method it belongs to.
descriptionFor ready-made example configs, fetch :
v0alpha1.jsonhttps://plugins-cdn.grafana.net/<PLUGIN_ID>/<VERSION>/public/plugins/<PLUGIN_ID>/schema/v0alpha1.jsonbash
ID=yesoreyeram-infinity-datasource
VER=$(curl -s "https://grafana.com/api/plugins/$ID" | jq -r '.version')
curl -sf "https://plugins-cdn.grafana.net/$ID/$VER/public/plugins/$ID/schema/v0alpha1.json"Worked examples live under , an object keyed by scenario (e.g. , ). Each entry has a / (the scenario) and a holding the / payload to lift straight into the file:
settingsExamples.examplesapiKeyoauth2ClientCredentialssummarydescriptionvaluejsonDatasecureJsonDatabash
undefinedhttps://plugins-cdn.grafana.net/<PLUGIN_ID>/<VERSION>/public/plugins/<PLUGIN_ID>/schema/dsconfig.jsonbash
ID=yesoreyeram-infinity-datasource
VER=$(curl -s "https://grafana.com/api/plugins/$ID" | jq -r '.version')
curl -sf "https://plugins-cdn.grafana.net/$ID/$VER/public/plugins/$ID/schema/dsconfig.json"此文件符合dsconfig schema规范——这是解读文件内容的权威依据。不要凭记忆推导字段语义(涵盖、、、、、、);当字段不是普通标量时,请参考规范:
valueTypestringnumberbooleanarrayobjectmapany- 文字规范:https://raw.githubusercontent.com/grafana/dsconfig/refs/heads/main/dsconfig/schema.md
- 元schema(定义了每个的格式):https://raw.githubusercontent.com/grafana/dsconfig/refs/heads/main/dsconfig/schema.json
dsconfig.json
配置所需的字段信息包括:(配置项键名)、、( | | )以及(遵循,例如的可选值)。示例说明():
keyvalueTypetargetrootjsonDatasecureJsonDatavalidationsallowedValuesauth_methodschemaVersion: "v1"json
{
"pluginType": "yesoreyeram-infinity-datasource",
"fields": [
{
"key": "auth_method",
"valueType": "string",
"target": "jsonData",
"validations": [
{
"type": "allowedValues",
"values": [
"none",
"basicAuth",
"apiKey",
"bearerToken",
"oauth2",
"aws",
"azureBlob"
]
}
]
}
]
}仅选择与用户需求相关的字段(所选认证方式 + 连接信息),而非全部字段。每个字段的会说明其所属的认证方式。
description如需现成的示例配置,可获取:
v0alpha1.jsonhttps://plugins-cdn.grafana.net/<PLUGIN_ID>/<VERSION>/public/plugins/<PLUGIN_ID>/schema/v0alpha1.jsonbash
ID=yesoreyeram-infinity-datasource
VER=$(curl -s "https://grafana.com/api/plugins/$ID" | jq -r '.version')
curl -sf "https://plugins-cdn.grafana.net/$ID/$VER/public/plugins/$ID/schema/v0alpha1.json"可用示例位于下,这是一个按场景(如、)分类的对象。每个条目包含/(场景说明)和(可直接提取到配置文件中的/内容):
settingsExamples.examplesapiKeyoauth2ClientCredentialssummarydescriptionvaluejsonDatasecureJsonDatabash
undefinedlist scenarios, then pull one payload
列出场景,然后提取其中一个配置内容
... | jq -r '.settingsExamples.examples | keys[]'
... | jq '.settingsExamples.examples.apiKey.value'
undefined... | jq -r '.settingsExamples.examples | keys[]'
... | jq '.settingsExamples.examples.apiKey.value'
undefined5. Fallback when no schema is published
5. 无已发布Schema时的备选方案
If 404s (older plugins):
schema/dsconfig.json- Last resort: the generic structure in grafana-oss skill (§ Data source provisioning) can also tell the user the field names are best-effort, not plugin-authoritative.
NOTE: grafana-oss skill is available inplugin and also available as a standalone skill from the https://github.com/grafana/skills repositorygrafana-core
如果返回404(旧版插件):
schema/dsconfig.json- 最后手段:grafana-oss技能中的通用结构(§ 数据源配置)也可告知用户字段名称为尽力获取,并非插件官方定义。
注意:grafana-oss技能包含在插件中,也可从https://github.com/grafana/skills仓库单独获取grafana-core
6. Map each field by its target
target6. 根据target
映射每个字段
target | YAML | Terraform ( |
|---|---|---|
| top-level key on the datasource ( | top-level argument ( |
| under | key inside |
| under | key inside |
Use each field's for the scalar ( quoted in YAML, →/, bare). Never inline a real secret. Nested objects (, ) and arrays (, ) map directly.
valueTypestringbooleantruefalsenumberoauth2awsallowedHostsscopesAlways set ( target) and default it to — queries route through the Grafana server (the secure default); only use (browser → data source) if the user explicitly asks for it. In Terraform the argument is .
accessrootproxydirectaccess_mode | YAML | Terraform ( |
|---|---|---|
| 数据源的顶级键( | 顶级参数( |
| 位于 | |
| 位于 | 通过敏感变量放入 |
根据字段的设置标量类型(YAML中需加引号,→/,直接写数值)。切勿直接嵌入真实密钥。嵌套对象(、)和数组(、)直接映射即可。
valueTypestringbooleantruefalsenumberoauth2awsallowedHostsscopes务必设置(目标),默认值为——查询通过Grafana服务器路由(安全默认值);仅当用户明确要求时才使用(浏览器直接连接数据源)。在Terraform中对应的参数是。
accessrootproxydirectaccess_mode7. Ask the format, then emit the file
7. 确认格式后生成文件
Now ask: YAML or Terraform? Same fields, different output file and syntax. Don't assume: "provision X" may mean either; skip the question only if the user already named a format ("terraform for X"). YAML file provisioning is the native, zero-dependency path; Terraform needs the official provider.
grafana/grafana| Choice | Produces |
|---|---|
| YAML config file | |
| Terraform | |
<name>YAML → :
provisioning/datasources/<name>.yamlyaml
apiVersion: 1
datasources:
- name: Infinity # must be unique across the instance — collides even with a different datasource type
type: yesoreyeram-infinity-datasource # = pluginType from the schema
access: proxy # always set; default proxy (route queries through the Grafana server)
uid: infinity-ds # also unique and immutable so dashboards can reference it
jsonData:
auth_method: apiKey # value from validations.allowedValues
apiKeyKey: X-API-Key
apiKeyType: header
allowedHosts:
- https://api.example.com
secureJsonData:
apiKeyValue: ${API_KEY} # env var ref, never a literal secret
editable: falseTerraform → :
<name>.tfhcl
variable "api_key" {
type = string
sensitive = true
}
resource "grafana_data_source" "infinity" {
type = "yesoreyeram-infinity-datasource"
name = "Infinity"
uid = "infinity-ds"
access_mode = "proxy" # always set; default proxy (route queries through the Grafana server)
json_data_encoded = jsonencode({
auth_method = "apiKey"
apiKeyKey = "X-API-Key"
apiKeyType = "header"
allowedHosts = ["https://api.example.com"]
})
secure_json_data_encoded = jsonencode({
apiKeyValue = var.api_key
})
}grafana_data_sourcegrafana/grafanaaccess_modejson_data_encodedsecure_json_data_encodedrequired_providersprovider "grafana"现在询问用户:选择YAML还是Terraform? 两者字段相同,但输出文件和语法不同。不要自行假设:“配置X”可能指任意一种格式;仅当用户已指定格式(如“X的terraform配置”)时可跳过此问题。YAML文件配置是原生、零依赖的方式;Terraform需要官方的 provider。
grafana/grafana| 选择 | 生成文件类型 |
|---|---|
| YAML配置文件 | |
| Terraform | |
<name>YAML → :
provisioning/datasources/<name>.yamlyaml
apiVersion: 1
datasources:
- name: Infinity # 必须在实例中唯一——即使数据源类型不同也不能重名
type: yesoreyeram-infinity-datasource # = schema中的pluginType
access: proxy # 务必设置;默认proxy(查询通过Grafana服务器路由)
uid: infinity-ds # 同样唯一且不可变,以便仪表盘引用
jsonData:
auth_method: apiKey # 来自validations.allowedValues的值
apiKeyKey: X-API-Key
apiKeyType: header
allowedHosts:
- https://api.example.com
secureJsonData:
apiKeyValue: ${API_KEY} # 环境变量引用,切勿使用明文密钥
editable: falseTerraform → :
<name>.tfhcl
variable "api_key" {
type = string
sensitive = true
}
resource "grafana_data_source" "infinity" {
type = "yesoreyeram-infinity-datasource"
name = "Infinity"
uid = "infinity-ds"
access_mode = "proxy" # 务必设置;默认proxy(查询通过Grafana服务器路由)
json_data_encoded = jsonencode({
auth_method = "apiKey"
apiKeyKey = "X-API-Key"
apiKeyType = "header"
allowedHosts = ["https://api.example.com"]
})
secure_json_data_encoded = jsonencode({
apiKeyValue = var.api_key
})
}grafana_data_sourcegrafana/grafanaaccess_modejson_data_encodedsecure_json_data_encodedrequired_providersprovider "grafana"8. Return the file to the user
8. 将文件返回给用户
Present the complete file in a single code block for the user to copy and paste into their environment — note where it goes:
- YAML → (apply on Grafana start or a provisioning reload).
provisioning/datasources/<name>.yaml - Terraform → their Terraform config, applied with .
terraform apply
Optionally, tell them how to confirm it worked once applied:
bash
curl -s https://grafana.example.com/api/datasources/uid/<uid>/health \
-H "Authorization: Bearer <token>"将完整文件放在单个代码块中供用户复制粘贴到其环境中——注明文件存放路径:
- YAML → (Grafana启动时或配置重载时生效)。
provisioning/datasources/<name>.yaml - Terraform → 用户的Terraform配置中,通过生效。
terraform apply
可选:告知用户配置生效后如何验证是否成功:
bash
curl -s https://grafana.example.com/api/datasources/uid/<uid>/health \
-H "Authorization: Bearer <token>"{ "status": "OK" } → working
{ "status": "OK" } → 配置成功
{ "status": "ERROR" } → URL unreachable or auth misconfigured
{ "status": "ERROR" } → URL不可达或认证配置错误
Or verify in the UI: visit `<https://grafana.example.com>/connections/datasources/edit/<uid>` and click **Test**.
或在UI中验证:访问`<https://grafana.example.com>/connections/datasources/edit/<uid>`并点击**测试**。Convert an existing data source
转换现有数据源
To codify a data source already configured in a running instance, read its config through the Grafana MCP server (grafana/mcp-grafana).
Precondition: the Grafana MCP server is connected with its Datasources toolset enabled (it holds the instance credentials). If it isn't available, do not support this path — never ask the user to paste a Grafana token into chat. Fall back to the from-scratch Workflow instead.
- Find the data source with the MCP tools — to browse, then
list_datasources(byget_datasourceoruid) for the full config.name - The result carries every non-secret field directly: ,
type,uid,url,access,basicAuth, and the fullbasicAuthUserobject. Copy them as-is.jsonData - Secrets are never returned. The map lists which secret keys are set (e.g.
secureJsonFields) without their values. Emit an{"apiKeyValue": true}placeholder in${ENV_VAR}for each key it reportssecureJsonData.true - Cross-check against the schema (step 4) to confirm secret key names and placement, then continue at step 6 (map) and step 7 (emit) as normal.
target
要将已在运行实例中配置的数据源转换为代码形式,可通过Grafana MCP服务器(grafana/mcp-grafana)读取其配置。
前提条件: Grafana MCP服务器已连接且已启用Datasources工具集(持有实例凭据)。若不可用,则不支持此方式——切勿要求用户在聊天中粘贴Grafana令牌。请退回到从零开始的工作流程。
- 使用MCP工具查找数据源——通过浏览,然后通过
list_datasources(按get_datasource或uid)获取完整配置。name - 返回结果直接包含所有非机密字段:、
type、uid、url、access、basicAuth以及完整的basicAuthUser对象。直接复制这些内容即可。jsonData - 机密信息永远不会返回。映射列出了已设置的机密键名(如
secureJsonFields)但不包含值。为每个标记为{"apiKeyValue": true}的键在true中生成secureJsonData占位符。${ENV_VAR} - 与步骤4中的schema交叉核对,确认机密键名和位置,然后继续步骤6(映射)和步骤7(生成文件)。
target
Related
相关内容
- grafana-oss — generic data source / dashboard provisioning structure and provisioning paths.
- grafana-oss — 通用数据源/仪表盘配置结构及配置路径。