mcpField
G
grafana-dashboards

grafana-dashboards

github.com/ormus-solutions云与 DevOps12379 次安装4 周前更新
代码仓库

关于 grafana-dashboards

面向使用 Grafana 监控应用、基础设施和业务指标的团队,介绍仪表板设计与配置。内容包括面板类型、变量、告警,以及以代码方式配置仪表板。

主要功能

  1. 01仪表板布局与设计原则
  2. 02统计、时序、表格和热图面板
  3. 03查询变量与仪表板告警
  4. 04基础设施和应用仪表板模式
  5. 05使用 Terraform 或 Ansible 配置

适用场景

  1. 01可视化 Prometheus 指标
  2. 02监控基础设施与服务
  3. 03跟踪 SLO 和业务 KPI
  4. 04通过代码配置仪表板

安装

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

$npx skills add https://github.com/ormus-solutions/libreuiux-claude-code --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 方法(服务)
  • 速率 - 每秒请求数
  • 错误 - 错误率
  • 耗时 - 延迟/响应时间
3. USE 方法(资源)
  • 利用率 - 资源繁忙的时间百分比
  • 饱和度 - 队列长度/等待时间
  • 错误 - 错误计数

仪表板结构

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}
      }
    ]
  }
}

参考: 请参阅 assets/api-dashboard.json

面板类型

1. 统计面板(单值)
{
  "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 数量
  • 节点状态

参考: 请参阅 assets/infrastructure-dashboard.json

数据库仪表板

关键面板:

  • 每秒查询数
  • 连接池使用情况
  • 查询延迟(P50、P95、P99)
  • 活跃连接数
  • 数据库大小
  • 复制延迟
  • 慢查询

参考: 请参阅 assets/database-dashboard.json

应用程序仪表板

关键面板:

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

最佳实践

  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

参考文件

  • assets/api-dashboard.json - API 监控仪表板
  • assets/infrastructure-dashboard.json - 基础设施仪表板
  • assets/database-dashboard.json - 数据库监控仪表板
  • references/dashboard-design.md - 仪表板设计指南

相关技能

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

评论

支持 Markdown