Documentation
¶
Overview ¶
演示: 分布式链路追踪 —— Wire 一行装配 trace, span 中间件自动注入, 零外部服务可跑。
trace.NewBoot 经 Wire 首位装配后自动获得三件事:
- OTLP 导出器初始化(懒连接, 无 collector 也能正常启动)
- HTTP span 中间件注入(路由模板命名, 接管 X-Request-Id 为 32 位 hex trace id)
- 关停时最后关闭, 其余资源释放产生的在途 span 全量刷盘
查看 trace 效果(可选): 本地起一个 Jaeger all-in-one 作为 OTLP collector:
docker run -d -p 16686:16686 -p 4317:4317 jaegertracing/all-in-one:latest
打开 http://localhost:16686 选服务 trace-demo 即可看到链路瀑布图。
运行:
cd example/trace && go run .
访问(另开终端):
curl "http://127.0.0.1:8085/api/v1/greet?name=alice" -> 响应体与 X-Request-Id 均带 trace id curl -i http://127.0.0.1:8085/api/v1/boom -> 演示 panic 路径: span 记录异常后由 Recovery 统一 500 curl http://127.0.0.1:8085/metrics -> Prometheus 指标(4xx/5xx 同样计数)
Click to show internal directories.
Click to hide internal directories.