Grafana 简介
什么是 Grafana?
Grafana 是一个开源的数据可视化与监控平台,由 Grafana Labs 开发。它能够将来自各种数据源的指标、日志、链路追踪数据统一展示在可交互的仪表盘(Dashboard)上,是现代可观测性(Observability)技术栈的核心组件之一。
核心定位:可观测性三支柱
┌─────────────────────────────────────────────────────┐
│ 可观测性三支柱 (3 Pillars) │
│ │
│ 📊 Metrics 📝 Logs 🔗 Traces │
│ (指标) (日志) (链路追踪) │
│ │
│ Prometheus → Loki → Tempo / Jaeger │
│ │ │ │ │
│ └──────────────┴────────────────┘ │
│ │ │
│ ┌───────▼───────┐ │
│ │ Grafana │ ← 统一可视化入口 │
│ └───────────────┘ │
└─────────────────────────────────────────────────────┘
核心概念
1. Data Source(数据源)
Grafana 本身不存储数据,而是连接各种数据源进行查询和展示:
时序数据库: Prometheus / InfluxDB / Graphite / VictoriaMetrics
日志系统: Loki / Elasticsearch / OpenSearch
链路追踪: Tempo / Jaeger / Zipkin
关系型数据库:MySQL / PostgreSQL / MSSQL
云服务: CloudWatch / Azure Monitor / Google Cloud
其他: GitHub / Datadog / Splunk / CSV ...
2. Dashboard(仪表盘)
┌─────────────────────────────────────────────────────┐
│ Dashboard │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Panel 1 │ │ Panel 2 │ │ Panel 3 │ │
│ │ Time Series│ │ Gauge │ │ Bar Chart │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ ┌──────────────────────────┐ ┌──────────────────┐ │
│ │ Panel 4 │ │ Panel 5 │ │
│ │ Table │ │ Heatmap │ │
│ └──────────────────────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────┘
3. Panel(面板)
每个 Panel 是仪表盘的最小单元,支持多种可视化类型:
4. Alert(告警)
┌──────────────────────────────────────────┐
│ Grafana 告警流程 │
│ │
│ 数据源 → 查询 → 阈值判断 → 触发告警 │
│ │ │
│ ┌────────▼────────┐ │
│ │ 通知渠道 │ │
│ │ • Email │ │
│ │ • Slack │ │
│ │ • PagerDuty │ │
│ │ • Webhook │ │
│ │ • 钉钉 / 飞书 │ │
│ └─────────────────┘ │
└──────────────────────────────────────────┘
产品矩阵
┌─────────────────────────────────────────────────────┐
│ Grafana Labs 产品栈 │
│ │
│ 📊 Grafana → 可视化平台(核心) │
│ 📈 Mimir → 大规模 Prometheus 指标存储 │
│ 📝 Loki → 日志聚合系统(类 Prometheus) │
│ 🔗 Tempo → 分布式链路追踪 │
│ 🔥 Pyroscope → 持续性能分析(Profiling) │
│ 📡 Alloy → 统一数据采集 Agent(原 Agent) │
│ ☁️ Grafana Cloud → 托管 SaaS 版本 │
└─────────────────────────────────────────────────────┘
快速上手
Docker 启动
docker run -d \
--name grafana \
-p 3000:3000 \
-v grafana-storage:/var/lib/grafana \
grafana/grafana:latest
# 访问 http://localhost:3000
# 默认账号:admin / admin
Docker Compose(含 Prometheus)
version: '3.8'
services:
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.retention.time=15d'
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=yourpassword
- GF_USERS_ALLOW_SIGN_UP=false
volumes:
- grafana-storage:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning
depends_on:
- prometheus
volumes:
grafana-storage:
Prometheus 配置示例
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
- job_name: 'vllm' # 接入 vLLM 监控
static_configs:
- targets: ['vllm-server:8000']
metrics_path: '/metrics'
- job_name: 'tgi' # 接入 TGI 监控
static_configs:
- targets: ['tgi-server:8080']
metrics_path: '/metrics'
PromQL 查询示例
Grafana 中查询 Prometheus 数据使用 PromQL 语言:
# CPU 使用率
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 内存使用率
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# HTTP 请求速率
rate(http_requests_total[5m])
# P99 延迟
histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m]))
# TGI 生成 token 速率
rate(tgi_generated_tokens_total[1m])
# vLLM GPU 缓存使用率
vllm:gpu_cache_usage_perc
Dashboard as Code
Grafana 支持通过配置文件自动化管理仪表盘,避免手动点击:
Provisioning(原生方式)
# grafana/provisioning/datasources/prometheus.yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
jsonData:
timeInterval: "15s"
Grafonnet(Jsonnet 方式)
// dashboard.jsonnet
local grafana = import 'grafonnet/grafana.libsonnet';
grafana.dashboard.new(
'LLM Inference Monitor',
timezone='browser',
refresh='30s',
)
.addPanel(
grafana.graphPanel.new(
'Token Generation Rate',
datasource='Prometheus',
)
.addTarget(
grafana.prometheus.target(
'rate(tgi_generated_tokens_total[1m])',
legendFormat='tokens/s'
)
),
gridPos={ x: 0, y: 0, w: 12, h: 8 }
)
实际监控场景:LLM 服务监控
结合 vLLM / TGI 的完整监控方案:
┌─────────────────────────────────────────────────────┐
│ LLM 服务监控大盘 │
│ │
│ ┌───────────────┐ ┌───────────────┐ │
│ │ 请求 QPS │ │ 平均延迟 │ │
│ │ 1,234/s │ │ 238ms │ │
│ └───────────────┘ └───────────────┘ │
│ │
│ ┌───────────────┐ ┌───────────────┐ │
│ │ GPU 显存使用率 │ │ Token 生成率 │ │
│ │ [████████░] │ │ [折线图] │ │
│ │ 85.3% │ │ │ │
│ └───────────────┘ └───────────────┘ │
│ │
│ ┌─────────────────────────────────────┐ │
│ │ P50 / P90 / P99 延迟分布 │ │
│ │ [时序折线图] │ │
│ └─────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘
版本对比
总结
Grafana 已成为云原生可观测性领域的事实标准,其强大之处在于:
🔌 数据源无关:统一接入几乎所有监控系统
🎨 可视化丰富:满足各类业务展示需求
🔔 告警完善:从发现到通知全链路覆盖
🏗️ 生态完整:与 Prometheus + Loki + Tempo 构成完整可观测性方案
📎 官方网站:grafana.com
📎 官方仓库:github.com/grafana/grafana