mcpField
G
grafana-dashboards

grafana-dashboards

github.com/wshobson云与 DevOps41.9k13.3k 次安装2 周前更新
代码仓库

关于 grafana-dashboards

用于设计和配置 Grafana 仪表板的技能,面向应用、基础设施和业务指标监控,涵盖常见面板、变量、告警及仪表板即代码方法。

主要功能

  1. 01RED 与 USE 仪表板模式
  2. 02统计、时序、表格和热图面板
  3. 03灵活仪表板的查询变量
  4. 04仪表板告警与配置
  5. 05Terraform 和 Ansible 部署示例

适用场景

  1. 01可视化 Prometheus 指标
  2. 02监控 API 请求率、错误和延迟
  3. 03跟踪基础设施与数据库健康状况
  4. 04构建 SLO 和业务 KPI 仪表板

安装

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

$npx skills add https://github.com/wshobson/agents --skill grafana-dashboards

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

详细介绍

Grafana 仪表盘

创建和管理可投入生产使用的 Grafana 仪表盘,实现全面的系统可观测性。

用途

设计高效的 Grafana 仪表盘,用于监控应用、基础设施和业务指标。

何时使用

  • 可视化 Prometheus 指标
  • 创建自定义仪表盘
  • 实现 SLO 仪表盘
  • 监控基础设施
  • 跟踪业务 KPI

仪表盘设计原则

1. 信息层级
┌─────────────────────────────────────┐
│  Critical Metrics (Big Numbers)     │
├─────────────────────────────────────┤
│  Key Trends (Time Series)           │
├─────────────────────────────────────┤
│  Detailed Metrics (Tables/Heatmaps) │
└─────────────────────────────────────┘
2. RED 方法(服务)
  • Rate - 每秒请求数
  • Errors - 错误率
  • Duration - 延迟/响应时间
3. USE 方法(资源)
  • Utilization - 资源繁忙的时间百分比
  • Saturation - 队列长度/等待时间
  • Errors - 错误数

仪表盘结构

API 监控仪表盘
{
  "dashboard": {
    "title": "API Monitoring",
    "tags": ["api", "production"],
    "timezone": "browser",
    "refresh": "30s",
    "panels": [
      {
        "title": "Request Rate",
        "type": "graph",
        "targets": [
          {
            "expr": "sum(rate(http_requests_total[5m])) by (service)",
            "legendFormat": "{{service}}"
          }
        ],
        "gridPos": { "x": 0, "y": 0, "w": 12, "h": 8 }
      },
      {
        "title": "Error Rate %",
        "type": "graph",
        "targets": [
          {
            "expr": "(sum(rate(http_requests_total{status=~\"5..\"}[5m])) / sum(rate(http_requests_total[5m]))) * 100",
            "legendFormat": "Error Rate"
          }
        ],
        "alert": {
          "conditions": [
            {
              "evaluator": { "params": [5], "type": "gt" },
              "operator": { "type": "and" },
              "query": { "params": ["A", "5m", "now"] },
              "type": "query"
            }
          ]
        },
        "gridPos": { "x": 12, "y": 0, "w": 12, "h": 8 }
      },
      {
        "title": "P95 Latency",
        "type": "graph",
        "targets": [
          {
            "expr": "histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service))",
            "legendFormat": "{{service}}"
          }
        ],
        "gridPos": { "x": 0, "y": 8, "w": 24, "h": 8 }
      }
    ]
  }
}

面板类型

1. Stat 面板(单值)
{
  "type": "stat",
  "title": "Total Requests",
  "targets": [
    {
      "expr": "sum(http_requests_total)"
    }
  ],
  "options": {
    "reduceOptions": {
      "values": false,
      "calcs": ["lastNotNull"]
    },
    "orientation": "auto",
    "textMode": "auto",
    "colorMode": "value"
  },
  "fieldConfig": {
    "defaults": {
      "thresholds": {
        "mode": "absolute",
        "steps": [
          { "value": 0, "color": "green" },
          { "value": 80, "color": "yellow" },
          { "value": 90, "color": "red" }
        ]
      }
    }
  }
}
2. 时间序列图
{
  "type": "graph",
  "title": "CPU Usage",
  "targets": [
    {
      "expr": "100 - (avg by (instance) (rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)"
    }
  ],
  "yaxes": [
    { "format": "percent", "max": 100, "min": 0 },
    { "format": "short" }
  ]
}
3. 表格面板
{
  "type": "table",
  "title": "Service Status",
  "targets": [
    {
      "expr": "up",
      "format": "table",
      "instant": true
    }
  ],
  "transformations": [
    {
      "id": "organize",
      "options": {
        "excludeByName": { "Time": true },
        "indexByName": {},
        "renameByName": {
          "instance": "Instance",
          "job": "Service",
          "Value": "Status"
        }
      }
    }
  ]
}
4. 热力图
{
  "type": "heatmap",
  "title": "Latency Heatmap",
  "targets": [
    {
      "expr": "sum(rate(http_request_duration_seconds_bucket[5m])) by (le)",
      "format": "heatmap"
    }
  ],
  "dataFormat": "tsbuckets",
  "yAxis": {
    "format": "s"
  }
}

变量

查询变量
{
  "templating": {
    "list": [
      {
        "name": "namespace",
        "type": "query",
        "datasource": "Prometheus",
        "query": "label_values(kube_pod_info, namespace)",
        "refresh": 1,
        "multi": false
      },
      {
        "name": "service",
        "type": "query",
        "datasource": "Prometheus",
        "query": "label_values(kube_service_info{namespace=\"$namespace\"}, service)",
        "refresh": 1,
        "multi": true
      }
    ]
  }
}
在查询中使用变量
sum(rate(http_requests_total{namespace="$namespace", service=~"$service"}[5m]))

仪表盘中的告警

{
  "alert": {
    "name": "High Error Rate",
    "conditions": [
      {
        "evaluator": {
          "params": [5],
          "type": "gt"
        },
        "operator": { "type": "and" },
        "query": {
          "params": ["A", "5m", "now"]
        },
        "reducer": { "type": "avg" },
        "type": "query"
      }
    ],
    "executionErrorState": "alerting",
    "for": "5m",
    "frequency": "1m",
    "message": "Error rate is above 5%",
    "noDataState": "no_data",
    "notifications": [{ "uid": "slack-channel" }]
  }
}

仪表盘预置

dashboards.yml:

apiVersion: 1

providers:
  - name: "default"
    orgId: 1
    folder: "General"
    type: file
    disableDeletion: false
    updateIntervalSeconds: 10
    allowUiUpdates: true
    options:
      path: /etc/grafana/dashboards

常见仪表盘模式

基础设施仪表盘

关键面板:

  • 每个节点的 CPU 利用率
  • 每个节点的内存使用量
  • 磁盘 I/O
  • 网络流量
  • 按命名空间统计的 Pod 数量
  • 节点状态
数据库仪表盘

关键面板:

  • 每秒查询数
  • 连接池使用率
  • 查询延迟(P50、P95、P99)
  • 活动连接数
  • 数据库大小
  • 复制延迟
  • 慢查询
应用仪表盘

关键面板:

  • 请求速率
  • 错误率
  • 响应时间(百分位数)
  • 活跃用户/会话数
  • 缓存命中率
  • 队列长度

最佳实践

  1. 从模板开始(Grafana 社区仪表盘)
  2. 使用一致的命名来命名面板和变量
  3. 将相关指标分组到行中
  4. 设置合适的时间范围(默认:最近 6 小时)
  5. 使用变量以获得灵活性
  6. 添加面板描述以提供上下文
  7. 正确配置单位
  8. 为颜色设置有意义的阈值
  9. 在不同仪表盘中使用一致的颜色
  10. 使用不同的时间范围进行测试

仪表盘即代码

Terraform 预置
resource "grafana_dashboard" "api_monitoring" {
  config_json = file("${path.module}/dashboards/api-monitoring.json")
  folder      = grafana_folder.monitoring.id
}

resource "grafana_folder" "monitoring" {
  title = "Production Monitoring"
}
Ansible 预置
- name: Deploy Grafana dashboards
  copy:
    src: "{{ item }}"
    dest: /etc/grafana/dashboards/
  with_fileglob:
    - "dashboards/*.json"
  notify: restart grafana

相关技能

  • prometheus-configuration - 用于指标采集
  • slo-implementation - 用于 SLO 仪表盘

评论

支持 Markdown