executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
build-grafana-dashboards创建生产级 Grafana 仪表板,包含可复用面板、模板变量、注解及版本控制部署的预置配置。 适用于为 Prometheus、Loki 等数据源创建可视化图表、为 SRE 团队构建运营仪表板、 从手动创建仪表板迁移到版本控制预置配置,或建立高管级别的 SLO 合规报告。
Independent GitHub snapshot · Automated classification · Original documentation governs
设计和部署 Grafana 仪表板,遵循可维护性、可复用性和版本控制的最佳实践。
参见 Extended Examples 获取完整配置文件和模板。
在构建面板之前规划仪表板布局和组织。
创建仪表板规格文档:
# Service Overview Dashboard
## Purpose
Real-time operational view for on-call engineers monitoring the API service.
## Rows
1. High-Level Metrics (collapsed by default)
- Request rate, error rate, latency (RED metrics)
- Service uptime, instance count
2. Detailed Metrics (expanded by default)
- Per-endpoint latency breakdown
- Error rate by status code
- Database connection pool status
3. Resource Utilization
- CPU, memory, disk usage per instance
- Network I/O rates
4. Logs (collapsed by default)
- Recent errors from Loki
- Alert firing history
## Variables
- `environment`: production, staging, development
- `instance`: all instances or specific instance selection
- `interval`: aggregation window (5m, 15m, 1h)
## Annotations
- Deployment events from CI/CD system
- Alert firing/resolving events
关键设计原则:
预期结果: 仪表板结构文档清晰,利益相关者对指标和布局优先级达成一致。
失败处理:
使用可复用变量构建仪表板基础以实现过滤。
创建仪表板 JSON 结构(或使用 UI 创建后导出):
{
"dashboard": {
"title": "API Service Overview",
"uid": "api-service-overview",
"version": 1,
"timezone": "browser",
"editable": true,
"graphTooltip": 1,
"time": {
"from": "now-6h",
"to": "now"
},
"refresh": "30s",
"templating": {
"list": [
{
"name": "environment",
"type": "query",
"datasource": "Prometheus",
"query": "label_values(up{job=\"api-service\"}, environment)",
"multi": false,
"includeAll": false,
"refresh": 1,
"sort": 1,
"current": {
"selected": false,
"text": "production",
"value": "production"
}
},
{
"name": "instance",
"type": "query",
"datasource": "Prometheus",
"query": "label_values(up{job=\"api-service\",environment=\"$environment\"}, instance)",
"multi": true,
"includeAll": true,
"refresh": 1,
"allValue": ".*",
"current": {
"selected": true,
"text": "All",
"value": "$__all"
}
},
{
"name": "interval",
"type": "interval",
"options": [
{"text": "1m", "value": "1m"},
{"text": "5m", "value": "5m"},
{"text": "15m", "value": "15m"},
{"text": "1h", "value": "1h"}
],
"current": {
"text": "5m",
"value": "5m"
},
"auto": false
}
]
},
"annotations": {
"list": [
{
"name": "Deployments",
"datasource": "Prometheus",
"enable": true,
"expr": "changes(app_version{job=\"api-service\",environment=\"$environment\"}[5m]) > 0",
"step": "60s",
"iconColor": "rgba(0, 211, 255, 1)",
"tagKeys": "version"
}
]
}
}
}
变量类型和用例:
label_values()、query_result())预期结果: 变量从数据源正确填充,级联过滤器正常工作(环境过滤实例),默认选择适当。
失败处理:
allValue 字段中的正则表达式模式为每个指标创建具有适当可视化类型的面板。
时间序列面板(请求速率):
{
"type": "timeseries",
"title": "Request Rate",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
"targets": [
{
"expr": "sum(rate(http_requests_total{job=\"api-service\",environment=\"$environment\",instance=~\"$instance\"}[$interval])) by (method)",
"legendFormat": "{{method}}",
"refId": "A"
}
],
"fieldConfig": {
"defaults": {
"unit": "reqps",
"color": {
"mode": "palette-classic"
},
"custom": {
"drawStyle": "line",
"lineInterpolation": "smooth",
"fillOpacity": 10,
"spanNulls": true
},
"thresholds": {
"mode": "absolute",
"steps": [
{"value": null, "color": "green"},
{"value": 1000, "color": "yellow"},
{"value": 5000, "color": "red"}
]
}
}
},
"options": {
"tooltip": {
"mode": "multi",
"sort": "desc"
},
"legend": {
"displayMode": "table",
"placement": "right",
"calcs": ["mean", "max", "last"]
}
}
}
统计面板(错误率):
{
"type": "stat",
"title": "Error Rate",
"gridPos": {"h": 4, "w": 6, "x": 12, "y": 0},
"targets": [
{
# ... (see EXAMPLES.md for complete configuration)
热力图面板(延迟分布):
{
"type": "heatmap",
"title": "Request Duration Heatmap",
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 8},
"targets": [
{
# ... (see EXAMPLES.md for complete configuration)
面板选择指南:
预期结果: 面板正确渲染数据,可视化匹配预期指标类型,图例描述清晰,阈值突出显示问题。
失败处理:
将面板组织到可折叠的行中以实现逻辑分组。
{
"panels": [
{
"type": "row",
"title": "High-Level Metrics",
"collapsed": false,
# ... (see EXAMPLES.md for complete configuration)
布局最佳实践:
w(宽度)和 h(高度)预期结果: 仪表板布局逻辑有序,行正确折叠/展开,面板视觉对齐无间隙。
失败处理:
在相关仪表板之间创建导航路径。
仪表板级别链接的 JSON:
{
"links": [
{
"title": "Service Details",
"type": "link",
"icon": "external link",
# ... (see EXAMPLES.md for complete configuration)
面板级别数据链接:
{
"fieldConfig": {
"defaults": {
"links": [
{
"title": "View Logs for ${__field.labels.instance}",
# ... (see EXAMPLES.md for complete configuration)
链接变量:
$service、$environment:仪表板模板变量${__field.labels.instance}:来自点击数据点的标签值${__from}、${__to}:当前仪表板时间范围$__url_time_range:URL 编码的时间范围预期结果: 点击面板元素或仪表板链接导航到相关视图,上下文保持不变(时间范围、变量)。
失败处理:
includeVars 和 keepTime 标志是否按预期工作将仪表板作为代码进行版本控制以实现可重现的部署。
创建预置配置目录结构:
mkdir -p /etc/grafana/provisioning/{dashboards,datasources}
数据源预置配置(/etc/grafana/provisioning/datasources/prometheus.yml):
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
# ... (see EXAMPLES.md for complete configuration)
仪表板预置配置(/etc/grafana/provisioning/dashboards/default.yml):
apiVersion: 1
providers:
- name: 'default'
orgId: 1
folder: 'Services'
type: file
disableDeletion: false
updateIntervalSeconds: 30
allowUiUpdates: true
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: true
在 /var/lib/grafana/dashboards/ 中存储仪表板 JSON 文件:
/var/lib/grafana/dashboards/
├── api-service/
│ ├── overview.json
│ └── details.json
├── database/
│ └── postgres.json
└── infrastructure/
├── nodes.json
└── kubernetes.json
使用 Docker Compose:
version: '3.8'
services:
grafana:
image: grafana/grafana:10.2.0
ports:
- "3000:3000"
volumes:
- ./grafana/provisioning:/etc/grafana/provisioning
- ./grafana/dashboards:/var/lib/grafana/dashboards
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_USERS_ALLOW_SIGN_UP=false
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Viewer
预期结果: 仪表板在 Grafana 启动时自动加载,JSON 文件的更改在更新间隔后反映出来,版本控制跟踪仪表板更改。
失败处理:
docker logs grafana | grep -i provisioningpython -m json.tool dashboard.jsonchmod 644 *.jsonallowUiUpdates: false 测试以防止 UI 修改curl http://localhost:3000/api/admin/provisioning/dashboards/reload -X POST -H "Authorization: Bearer $GRAFANA_API_KEY"$variable 语法而非硬编码值。检查变量刷新设置legendFormat 仅显示相关标签,而非完整指标名称。示例:用 {{method}} - {{status}} 代替默认值allowUiUpdates: false${__field.labels.labelname},验证标签是否存在于查询结果中setup-prometheus-monitoring — 配置为 Grafana 仪表板提供数据的 Prometheus 数据源configure-log-aggregation — 设置 Loki 用于日志面板查询和基于日志的注解define-slo-sli-sla — 使用 Grafana 统计面板和仪表面板可视化 SLO 合规性和错误预算instrument-distributed-tracing — 添加从指标面板到 Tempo 追踪视图的追踪 ID 链接