Lesson 5.5: Go 可观测性实战
学习目标
- 在 Go 应用中实现全链路可观测性
1. 三层埋点
go
func handleRequest(ctx context.Context, req *Request) {
// 1. 链路追踪
ctx, span := tracer.Start(ctx, "handleRequest")
defer span.End()
// 2. 业务日志
log.InfoContext(ctx, "processing request",
"user_id", req.UserID,
"action", req.Action,
)
// 3. 性能指标
timer := prometheus.NewTimer(httpRequestDuration.WithLabelValues(req.Action))
defer timer.ObserveDuration()
}关键点
- 上下文传播:通过
context.Context传递 Trace 信息 - 自动埋点:使用 Middleware/Interceptor 自动采集
- 不要过度埋点:聚焦关键路径(外部调用、数据库、缓存)

