拓冰建站拓冰建站
首页 / 资讯中心 / 正文

创业团队监控体系搭建:Prometheus 与 Grafana 最简指标采集实践

创业团队监控体系搭建Prometheus 与 Grafana 最简指标采集实践许多初创公司在系统可观测性Observability上的表现往往走向两个极端一种是彻底裸奔生产环境没有监控、没有告警服务挂了全靠客户在微信群里艾特客服或者数据库磁盘被日志打爆停机半天后才由开发者登机df -h排查另一种是过度建设一共就 5 台云服务器却兴师动众地搭建了 ELK 日志集群、Jaeger 分布式链路追踪、Prometheus 和自定义 APM Agent仅监控组件就占了 16GB 内存每个月云账单增加数千元但在实际排障时大家还是靠grep看日志。对于 10~30 人的技术团队最理想的工程策略是用最小的运维维护成本搭建一套覆盖 90% 核心故障的“黄金指标监控闭环”。一、 监控的核心方法论RED 模型与 USE 原则初创团队不要去监控成百上千个冷门指标只需牢牢抓住两套经典的监控模型----------------------------------------------------------------------- | 针对面向用户的应用服务 (Application Level): RED 模型 | | 1. Rate (吞吐速率) : 每秒请求数 (QPS / RPS) | | 2. Errors (错误数量) : HTTP 5xx 错误率与核心业务失败率 | | 3. Duration (响应延迟) : P95 / P99 毫秒级耗时响应时间 | ----------------------------------------------------------------------- | 针对底层基础设施与主机资源 (Host/OS Level): USE 原则 | | 1. Utilization (利用率): CPU 使用率、物理内存消耗占比 | | 2. Saturation (饱和度) : 系统平均负载 (Load Average)、网络队列积压 | | 3. Errors (硬件错误) : 磁盘 I/O 错误、网卡丢包 (Drops/Errors) | -----------------------------------------------------------------------二、 最简基础设施架构与 Docker Compose 一键启动一套单节点、高可用、低开销的极简监控栈只需四个容器node-exporter采集 Linux 主机 CPU/内存/磁盘/网络物理指标prometheus时间序列数据库与指标拉取引擎alertmanager告警聚合、抑制与飞书/钉钉 Webhook 路由grafana可视化仪表盘大屏。# docker-compose.monitoring.yml version: 3.8 services: node-exporter: image: prom/node-exporter:v1.8.0 container_name: node-exporter restart: unless-stopped volumes: - /proc:/host/proc:ro - /sys:/host/sys:ro - /:/rootfs:ro command: - --path.procfs/host/proc - --path.sysfs/host/sys - --path.rootfs/rootfs ports: - 9100:9100 prometheus: image: prom/prometheus:v2.52.0 container_name: prometheus restart: unless-stopped volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro - ./alert_rules.yml:/etc/prometheus/alert_rules.yml:ro - prometheus_data:/prometheus command: - --config.file/etc/prometheus/prometheus.yml - --storage.tsdb.retention.time15d # 保留15天历史数据防止撑爆磁盘 ports: - 9090:9090 grafana: image: grafana/grafana:11.0.0 container_name: grafana restart: unless-stopped environment: - GF_SECURITY_ADMIN_PASSWORDStrongAdminSecret2026 volumes: - grafana_data:/var/lib/grafana ports: - 3000:3000 volumes: prometheus_data: grafana_data:三、 业务服务埋点与高价值告警规则配置在业务应用以 Python FastAPI 为例中只需通过标准中间件暴露 3 个核心 Prometheus 指标from fastapi import FastAPI, Request import time from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST from starlette.responses import Response app FastAPI() # 1. 吞吐与错误计数器 (Rate Errors) HTTP_REQUESTS_TOTAL Counter( http_requests_total, Total HTTP Requests, [method, endpoint, status_code] ) # 2. 耗时直方图 (Duration) HTTP_REQUEST_DURATION_SECONDS Histogram( http_request_duration_seconds, HTTP Request Latency in Seconds, [method, endpoint], buckets[0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0] ) app.middleware(http) async def prometheus_middleware(request: Request, call_next): start_time time.time() response await call_next(request) duration time.time() - start_time endpoint request.url.path status_code str(response.status_code) method request.method HTTP_REQUESTS_TOTAL.labels(methodmethod, endpointendpoint, status_codestatus_code).inc() HTTP_REQUEST_DURATION_SECONDS.labels(methodmethod, endpointendpoint).observe(duration) return response app.get(/metrics) def metrics(): 暴露给 Prometheus 定期刮削的 HTTP Endpoint return Response(contentgenerate_latest(), media_typeCONTENT_TYPE_LATEST)生产级告警规则配置alert_rules.ymlgroups: - name: core_alerts rules: # 1. 服务不可用告警 - alert: ServiceInstanceDown expr: up 0 for: 1m labels: severity: critical annotations: summary: 实例 {{ $labels.instance }} 已失联挂死超过 1 分钟 # 2. HTTP 5xx 错误率超过 5% - alert: HighHttp5xxErrorRate expr: (sum(rate(http_requests_total{status_code~5..}[2m])) / sum(rate(http_requests_total[2m]))) * 100 5 for: 2m labels: severity: critical annotations: summary: 服务 {{ $labels.instance }} 5xx 错误率已达 {{ $value | printf \%.2f\ }}% # 3. 磁盘剩余空间低于 15% - alert: HostDiskSpaceRunningLow expr: (node_filesystem_free_bytes / node_filesystem_size_bytes) * 100 15 for: 5m labels: severity: warning annotations: summary: 主机挂载点 {{ $labels.mountpoint }} 剩余磁盘空间不足 15%四、 避坑与落地准则绝对禁止“告警疲劳Alert Fatigue”如果告警群里每天刷屏上百条无关痛痒的“CPU 瞬时利用率 80%”团队很快就会将该群设置免打扰。所有发到手机或群里的告警必须是‘需要立即人工介入’的紧急事件在 Grafana 导入社区成熟模板无需从零手动绘制每个图表面板直接在 Grafana 官方仪表盘市场导入Node Exporter FullID: 1860即可获得专业级的主机全景监控面板监控数据不要与生产 DB 放同一块磁盘Prometheus 自身的高频写日志必须配置独立的存储卷或保留时长如--storage.tsdb.retention.time15d防止监控自身成为撑爆磁盘的元凶。
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门