Prometheus + Grafana 学习项目
一个完整的可观测性学习环境:Go HTTP Server → Prometheus → Grafana。
交互教学:可观测性实战:Prometheus + Grafana 全栈监控
┌──────────┐ ┌────────────┐ scrape ┌────────────┐ query ┌─────────┐
│ Client │────────▶│ Go Server │◀──────────│ Prometheus │◀──────── │ Grafana │
│ (压测) │ HTTP │ :8080 │ /metrics │ :9090 │ │ :3000 │
└──────────┘ └────────────┘ └────────────┘ └─────────┘
Docker & Docker Compose
Go 1.21+(仅本地开发需要)
docker compose up --build
# 使用 client 生成负载
cd client && go run main.go -url http://localhost:8080 -concurrency 10
# 或用 curl 手动测试
curl http://localhost:8080/api/hello
curl http://localhost:8080/api/slow
curl http://localhost:8080/api/error
curl http://localhost:8080/api/order
curl http://localhost:8080/metrics
.
├── server/ # Go HTTP 服务(带 Prometheus 埋点)
│ ├── main.go
│ ├── go.mod
│ └── Dockerfile
├── client/ # 压测客户端
│ ├── main.go
│ ├── go.mod
│ └── Dockerfile
├── prometheus/ # Prometheus 配置
│ └── prometheus.yml
├── grafana/ # Grafana 配置
│ ├── provisioning/ # 自动化配置(数据源 + Dashboard)
│ └── dashboards/ # Dashboard JSON
├── docs/ # 学习文档
│ ├── 01-concepts.md # 核心概念
│ ├── 02-metrics-types.md # 指标类型详解
│ ├── 03-promql.md # PromQL 查询语法
│ └── 04-grafana-guide.md # Grafana 使用指南
├── docker-compose.yml
└── README.md
指标
类型
说明
http_requests_total
Counter
请求总数(按 method/endpoint/status 分组)
http_request_duration_seconds
Histogram
请求延迟分布
http_requests_in_flight
Gauge
当前处理中的请求
business_orders_total
Counter
业务订单数
business_order_amount
Histogram
订单金额分布
启动环境 → docker compose up --build
查看原始指标 → 访问 http://localhost:8080/metrics
Prometheus 查询 → 在 http://localhost:9090 练习 PromQL
Grafana 面板 → 在 http://localhost:3000 查看预置 Dashboard
阅读文档 → docs/ 目录下的学习资料
动手实验 → 修改 server 代码添加新指标,创建新的 Dashboard
docker compose down
# 清除数据卷
docker compose down -v