mcpField
D
dashboarding

dashboarding

github.com/grafana云与 DevOps2958.7k 次安装1 天前更新
代码仓库

关于 dashboarding

用于编写和维护 Grafana 仪表板的 Skill,面向通过 JSON 和 HTTP API 管理仪表板的用户。它涵盖创建、修改、搜索及验证仪表板的常见流程。

主要功能

  1. 01通过 API 推送仪表板
  2. 02获取与搜索仪表板
  3. 03编辑模板变量和转换
  4. 04参考 JSON Schema 配置仪表板

适用场景

  1. 01发布新仪表板并验证
  2. 02为现有仪表板添加变量
  3. 03通过转换计算面板字段

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/grafana/skills --skill dashboarding

安装后在 Claude Code 中输入/dashboarding确认已加载。

附带文件references/json-schema.md

详细介绍

Grafana 仪表板编写

文档:https://grafana.com/docs/grafana/latest/dashboards/

仪表板采用 JSON 格式。编写一次,通过 API 推送,并使用 uid 分享。

前提条件

  • 可从你的机器访问的 Grafana 堆栈(OSS、Enterprise 或 Cloud)
  • 具有 dashboards:write 权限的 API 令牌(Authorization: Bearer <token>)
  • 用于检查响应的 jq
  • references/json-schema.md 中的 JSON Schema 速查表

常见工作流

1. 通过 API 推送新仪表板并进行验证
# 1. Build the payload — wrap the dashboard JSON, set folder, mark overwrite
cat > /tmp/dash.json <<'JSON'
{
  "dashboard": {
    "uid": "demo-svc-v1",
    "title": "Demo Service",
    "schemaVersion": 41,
    "tags": ["demo"],
    "time": { "from": "now-1h", "to": "now" },
    "templating": { "list": [] },
    "panels": [{
      "id": 1, "type": "timeseries", "title": "Request Rate",
      "gridPos": { "x": 0, "y": 0, "w": 24, "h": 8 },
      "datasource": { "type": "prometheus", "uid": "prometheus" },
      "targets": [{
        "expr": "sum(rate(http_requests_total[5m])) by (status_code)",
        "legendFormat": "{{status_code}}", "refId": "A"
      }],
      "fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
    }]
  },
  "folderUid": "",
  "overwrite": true,
  "message": "initial push"
}
JSON

# 2. Validate the JSON BEFORE you send it (catches trailing-comma typos)
jq empty /tmp/dash.json && echo "json ok"

# 3. POST
RESP=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  "$GRAFANA/api/dashboards/db" -d @/tmp/dash.json)
echo "$RESP" | jq '{status, uid, url, version}'
# Expect: status="success", url="/d/demo-svc-v1/...", version=1 (incremented on each push)

# 4. Verify the round-trip — read it back and confirm one panel + the expected title
curl -s -H "Authorization: Bearer $TOKEN" \
  "$GRAFANA/api/dashboards/uid/demo-svc-v1" \
  | jq '{title: .dashboard.title, panels: (.dashboard.panels | length)}'
# Expect: {"title":"Demo Service","panels":1}

# 5. Open the dashboard in a browser — confirm the panel renders with data.
2. 为现有仪表板添加 $job 模板变量
# 1. Fetch existing dashboard
curl -s -H "Authorization: Bearer $TOKEN" \
  "$GRAFANA/api/dashboards/uid/demo-svc-v1" > /tmp/dash.json

# 2. Edit templating.list — append:
#   { "name":"job", "type":"query",
#     "datasource":{"type":"prometheus","uid":"prometheus"},
#     "query":{"query":"label_values(up, job)","refId":"A"},
#     "refresh":2, "includeAll":true, "multi":true, "label":"Service" }
#  (Use jq, an editor, or the Grafana UI — schema in references/json-schema.md.)

# 3. Update the panel expr to use the variable: rate(http_requests_total{job=~"$job"}[5m])

# 4. POST it back with overwrite: true. Verify the variable appears in the UI dropdown.
3. 使用转换计算“错误率”列
{
  "id": "calculateField",
  "options": {
    "alias": "Error %", "mode": "reduceRow",
    "reduce": { "reducer": "last" },
    "binary": { "left": "errors", "right": "total", "operator": "/" }
  }
}

将此内容添加到面板的 transformations: [] 中。在 UI 面板检查器中验证——应显示新字段,并随变量选择而更新。

完整 Schema(面板、单位、所有转换、注释、链接):references/json-schema.md。

API 参考

# Get
curl -s -H "Authorization: Bearer $TOKEN" \
  "$GRAFANA/api/dashboards/uid/<uid>" | jq '.dashboard'

# Search
curl -s -H "Authorization: Bearer $TOKEN" \
  "$GRAFANA/api/search?query=kubernetes&type=dash-db" | jq '.[] | {uid,title,folderTitle}'

# Create folder
curl -s -X POST -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" "$GRAFANA/api/folders" \
  -d '{"uid":"platform-team","title":"Platform Team"}'

对于嵌入应用插件的仪表板,请使用 @grafana/scenes(技能 grafana-o11y:grafana-scenes)。

资源

评论

支持 Markdown