下载工作台
企业级架构运维

可观测性体系(Metrics / Logs / Traces)

试读上半部分 · 解锁后可读全文

第 7 章 · 可观测性体系(Metrics / Logs / Traces)

本章构建 三位一体可观测性,支撑架构决策、SLO 治理与故障定位,并为 L3 认证提供可观测证据。

前置:PaaS ch6、ch10;小紫云计算监控;架构 ch01 NFR。


7.1 三支柱与关联模型

支柱回答问题工具贤紫对应
Metrics多快、多忙、多错?Prometheus + Grafana集群插件 / prometheus
Logs发生了什么?Loki / ELKloki + Explore
Traces请求经过谁、慢在哪?Jaegerjaeger OTLP 4317/4318

关联键traceId + spanId + service + namespace

traceId=7f3a9c2e
  ├─ gateway      12ms
  ├─ order-api    85ms ── mysql 42ms / redis 3ms
  └─ notify-worker 210ms (async)
场景先看再看
错误率突增Metrics 5xxLogs ERROR + traceId
P99 变慢histogramTraces 最长 Span
单用户投诉Logs userIdTraces 该 traceId

7.2 指标设计:RED / USE / SLI

RED(每个核心 API)

字母指标PromQL 思路
RRatesum(rate(http_requests_total[5m]))
EErrors5xx rate / total rate
DDurationhistogram_quantile(0.99, ..._bucket)

USE(资源层):Utilization / Saturation / Errors — CPU%、队列深度、OOM。

社区电商 SLI 建议

SLISLO 示例
可用性 1-5xx/total99.9%/月
下单 P99< 300ms
订单创建成功率> 99.95%

埋点规范

规则正确反例
单位后缀_seconds_total无单位 latency
标签基数method, statususer_id 作 label
Histogram覆盖 P50~P99仅 3 个桶
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: order-api
  namespace: monitor
spec:
  selector:
    matchLabels:
      app: order-api
  endpoints:
    - port: metrics
      interval: 15s

7.3 日志规范与 LogQL

{
  "time": "2026-08-18T10:00:00.123Z",
  "level": "INFO",
  "traceId": "7f3a9c2e8b1d4f6a",
  "service": "order-api",
  "msg": "order created",
  "orderId": "ORD202608180001",
  "durationMs": 85
}
规则prod 要求
结构化 JSON 一行一条
traceId 必填与 Jaeger 一致
默认 INFOERROR 含 stacktrace
禁止明文密码、token、身份证
{namespace="prod-app", app="order-api"} | json | level="ERROR"
{namespace="prod-app"} | json | traceId="7f3a9c2e8b1d4f6a"
sum(count_over_time({app="order-api"} | json | level="ERROR" [1m]))

7.4 分布式追踪与采样

传播头说明
traceparentW3C 标准(推荐)
X-Trace-Id遗留兼容
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(
    OTLPSpanExporter(endpoint="jaeger.monitor.svc.cluster.local:4317", insecure=True)
))
FastAPIInstrumentor.instrument_app(app)
策略采样率适用
always_on100%staging
traceidratio5%~10%prod
错误强制ERROR 100%叠加比例采样

数值:日请求 2000 万、10% 采样、均 5 span → 约 1 亿 span/天;需规划 Jaeger 存储保留 7 天。


以下内容需解锁后阅读

试读已结束。解锁本章 ¥5.00,或开通年度会员畅读全部教程。
年度会员 ¥199.00/年; 小紫 AI 工作台有效会员 ¥99.00/年

正文仅在服务端鉴权后下发,未付费无法获取下半部分内容。