# 分布式追踪

分布式追踪是一种可观测性技术，它记录请求在分布式系统各服务间传播的路径，捕获时序与因果数据，从而能够跨服务边界诊断延迟与故障。它通过保留单个请求执行的因果结构，对指标和日志形成补充。采用程度仍不均衡：SIGCOMM 2024 的一篇论文指出，截至 2021 年，仅约 20% 的企业应用进行了追踪插桩，因为上下文传播传统上需要应用层插桩。<sup>[1](https://sachin.cs.illinois.edu/papers/traceweaver-ashok-sigcomm24.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 数据模型 | 一条追踪是由跨度构成的有向无环图；每个跨度具有操作名称、开始时间和持续时间，跨度之间嵌套以建模因果关系<sup>[2](https://www.jaegertracing.io/docs/1.76/architecture/)</sup> |
| 标识符 | OTLP 的 trace_id 为 16 字节数组，span_id 为 8 字节；一条追踪中的所有跨度共享同一个 trace_id<sup>[3](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/trace/v1/trace.proto)</sup> |
| 传播标准 | W3C Trace Context 定义了 `traceparent` 和 `tracestate` 头，包含版本、trace-id、parent-id 和 trace-flags 字段<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup> |
| 带内与带外 | 只有 ID 和 baggage 随请求传输；时序、标签和日志则异步导出至后端<sup>[2](https://www.jaegertracing.io/docs/1.76/architecture/)</sup> |
| 采样 | 头部一致采样、尾部一致采样和酉采样是三种基本方案<sup>[5](https://www.pdl.cmu.edu/ftp/SelfStar/CMU-PDL-14-102.pdf)</sup> |
| 规模示例 | Facebook 的 Canopy 每天记录 13 亿条追踪<sup>[6](https://cs.brown.edu/people/jcmace/papers/kaldor2017canopy.pdf)</sup> |

## 工作原理

追踪框架为每个传入请求分配唯一的 trace_id 和一个采样标志。<sup>[7](https://arxiv.org/pdf/2502.06318)</sup> 处理该请求的每个组件都会创建跨度，记录其所做的工作。在 [OpenTelemetry](https://www.edgepedia.org/zh/opentelemetry) 协议中，一个跨度携带跨度名称、父跨度 ID、跨度 ID、以 UNIX 纪元纳秒表示的起止时间戳、事件和属性；跨度类型区分 INTERNAL、SERVER、CLIENT、PRODUCER 和 CONSUMER 角色。<sup>[3](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/trace/v1/trace.proto)</sup> 当服务 A 调用服务 B 时，A 在上下文中包含 trace ID 和 span ID，B 在同一追踪中创建一个新跨度，并以 A 的跨度作为其父跨度。<sup>[8](https://opentelemetry.io/docs/concepts/context-propagation/)</sup> 随后，后端利用 trace_id、parent_span_id 和 span_id 将分散在各组件中的跨度组装成一条连贯的追踪。<sup>[7](https://arxiv.org/pdf/2502.06318)</sup>

上下文传播是将这一身份跨边界传递的机制。W3C Trace Context 标准定义了 `traceparent` HTTP 头，包含四个字段：version、trace-id、parent-id 和 trace-flags。trace-id 为 16 字节数组，parent-id 为 8 字节数组，全零值无效；可选的 `tracestate` 头携带厂商特定的名称/值对。<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup> 收到 `traceparent` 请求头的厂商必须在发出请求时将其继续发送，并可修改其值。<sup>[9](https://www.w3.org/TR/trace-context-1/)</sup> 对于 gRPC，传播使用 gRPC 元数据，即 `traceparent` 头或 `grpc-trace-bin` 元数据键。<sup>[10](https://cloud.google.com/trace/docs/trace-context)</sup> 除 ID 之外，OpenTelemetry 的 baggage 机制可跨服务传播任意键值对，但需注意不应将凭证、API 密钥或个人数据放入其中。<sup>[8](https://opentelemetry.io/docs/concepts/context-propagation/)</sup>

## 实现方式

插桩遵循一种常见模式。Dapper 的设计将插桩限制在一小组通用库（线程、控制流和 RPC 框架）中，而非要求开发者修改应用代码，并使用采样来保持低开销。<sup>[11](https://mwhittaker.github.io/papers/html/sigelman2010dapper.html)</sup> 随后，追踪器仅将 ID 带内随请求传播，而已完成的跨度则带外异步上报至后端；Zipkin 的追踪器正是这样工作的。<sup>[12](https://zipkin.io/pages/architecture.html)</sup> Jaeger 的行为相同：未采样的追踪完全不收集数据，追踪 API 调用被短路。<sup>[2](https://www.jaegertracing.io/docs/1.76/architecture/)</sup>

随后，采集将跨度移至存储。Zipkin 支持 HTTP、Kafka 和 Scribe 作为传输方式，其采集器负责验证、存储和索引追踪数据。<sup>[12](https://zipkin.io/pages/architecture.html)</sup> Jaeger 直接接受 OTLP，可使用 Kafka 作为采集器与存储之间的持久中间队列，并提供存储插件框架。<sup>[2](https://www.jaegertracing.io/docs/1.76/architecture/)</sup>

采样决定记录什么。追踪基础设施作者的一篇回顾文章指出了三种根本不同的方案：头部一致采样、尾部一致采样和酉采样。<sup>[5](https://www.pdl.cmu.edu/ftp/SelfStar/CMU-PDL-14-102.pdf)</sup> W3C 规范描述了概率采样（例如每 100 条追踪采样 1 条）、基于持续时间或结果的延迟决策，以及由被调用方决定的延迟采样，并指出在高负载下记录所有请求会变得极其昂贵。<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup> Dapper 的生产经验发现，记录 1/1024 的追踪足以调试大多数问题，1/16 足以避免开销。

## 起源

现代追踪的谱系贯穿四个先驱系统：Magpie（Barham、Isaacs、Mortier 和 Narayanan，2003 年）、Whodunit（Chanda、Cox 和 Zwaenepoel，2007 年，发表于 ACM SIGOPS Operating Systems Review）、X-Trace（Fonseca 及其同事，2007 年）和 Dapper（Sigelman 及其同事，2010 年）。<sup>[7](https://arxiv.org/pdf/2502.06318)</sup> X-Trace 发表于 NSDI 2007，是一个跨层、跨应用的框架，它为任务产生的所有网络操作打上同一任务标识符，并重建由此产生的任务树；其元数据在 IP 选项、TCP 选项和 HTTP 头等协议扩展字段中携带 TreeInfo 元组（ParentID、OpID、EdgeType）。<sup>[13](https://www.usenix.org/legacy/event/nsdi07/tech/full_papers/fonseca/fonseca_html/index.html)</sup> Dapper 的作者将 Pinpoint、Magpie 和 X-Trace 列为与其工作最密切相关的系统，并区分了黑盒统计推断方案与传播全局标识符的基于标注的方案。Google 表示，Dapper 论文启发了 Zipkin 等开源项目，Dapper 风格的追踪成为行业标准。<sup>[14](https://cloud.google.com/blog/products/gcp/distributed-tracing-for-go)</sup> 源自这一谱系的行业实现包括 Google 的 Dapper、Cloudera 的 HTrace 和 Twitter 的 Zipkin。<sup>[5](https://www.pdl.cmu.edu/ftp/SelfStar/CMU-PDL-14-102.pdf)</sup>

## 变体

插桩层分阶段整合。OpenTracing 计划为各种语言提供了标准扩展和自动插桩；Google 于 2018 年开源了 OpenCensus；2019 年，两个项目合并组成 OpenTelemetry，此后迁移和前身项目的过渡持续进行。<sup>[15](https://icpe2026.spec.org/preprint/Benchmarking_the_Overhead_of_Distributed_Tracing_Agents_2.pdf)</sup> OpenTelemetry 标准化了遥测采集 API、插桩库和语义约定，并可与 Jaeger、Zipkin 和 [Prometheus](https://www.edgepedia.org/zh/prometheus) 等后端配合使用。<sup>[7](https://arxiv.org/pdf/2502.06318)</sup>

一种较新的变体消除了插桩要求。OpenTelemetry 基于 eBPF 的 OBI 自动插桩通过传播 W3C `traceparent` 头实现分布式追踪，出站网络级传播默认禁用，可通过 `OTEL_EBPF_BPF_CONTEXT_PROPAGATION=all` 启用。<sup>[17](https://opentelemetry.io/docs/zero-code/obi/distributed-traces/)</sup> 研究系统 DeepFlow 同样在内核中使用 eBPF（一种在内核中运行沙箱程序的 [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)技术）建立以网络为中心的追踪平面，而 TraceWeaver 则从 eBPF 捕获的请求和响应数据重建追踪，无需修改应用。<sup>[7](https://arxiv.org/pdf/2502.06318)</sup> 近期文献中与 Jaeger 和 Zipkin 并列提及的商业后端包括 Instana 和 Datadog。<sup>[1](https://sachin.cs.illinois.edu/papers/traceweaver-ashok-sigcomm24.pdf)</sup> 近期的标准化工作包括 W3C Trace Context Level 2，它增加了一个随机 trace-id 标志，要求 trace-id 最右侧至少 7 个字节按 \( [0..2^{56}-1] \) 上的均匀分布随机生成。<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup>

## 应用

追踪支持延迟诊断与聚合。上下文传播支持按路径的指标，例如测量 `GET /checkout > GET /product` 为每秒 40 次调用、平均响应时间 1703 毫秒，而该端点所有调用者的平均响应时间为 300 毫秒。<sup>[8](https://opentelemetry.io/docs/concepts/context-propagation/)</sup> 在生产规模下，Canopy 通过系统传播标识符以关联各组件间的信息，每天记录 13 亿条追踪。<sup>[6](https://cs.brown.edu/people/jcmace/papers/kaldor2017canopy.pdf)</sup>

## 局限与替代方案

记录一切是昂贵的：W3C 规范指出，在高负载下记录所有请求会变得极其昂贵，这正是采样存在的原因。<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup> 采样反过来会丢弃数据；在头部采样、尾部采样和酉采样之间的选择是决定追踪效用的四个设计轴之一（保留因果关系、追踪方法、采样和可视化）。<sup>[5](https://www.pdl.cmu.edu/ftp/SelfStar/CMU-PDL-14-102.pdf)</sup> 在公共 API 上天真地传播采样标志是一种安全风险：W3C 规范警告，攻击者可能以追踪开销压垮应用、伪造 `trace-id` 冲突使监控数据不可用，或抬高 SaaS 追踪账单。<sup>[4](https://www.w3.org/TR/trace-context-2/)</sup>

传播中断是零代码方案的主要失败模式。对于 HTTPS 流量，OBI 无法注入头，而是在 TCP/IP 数据包级别注入追踪信息，因此传播只能到达其他经 OBI 插桩的服务，并会被 L7 代理和负载均衡器打断；网络级传播通常需要 Linux 内核 5.17 或更新版本。<sup>[17](https://opentelemetry.io/docs/zero-code/obi/distributed-traces/)</sup> 在智能体 AI 环境中，Llama Stack 0.2.x 和 0.3.x 不会自动将跨度上下文从 Llama Stack 服务器传播到 MCP 服务器，需要手动将父上下文注入 HTTP 头。<sup>[16](https://developers.redhat.com/articles/2026/04/06/distributed-tracing-agentic-workflows-opentelemetry)</sup> 关于开销，[Red Hat](https://www.edgepedia.org/zh/red-hat) 的一篇文章指出，手动插桩增加的开销极小，通常每个跨度为微秒级，但告诫不要为琐碎操作创建过多细粒度跨度。<sup>[16](https://developers.redhat.com/articles/2026/04/06/distributed-tracing-agentic-workflows-opentelemetry)</sup>

## References

1. [TraceWeaver: Distributed Request Tracing for Microservices Without Application Modification (SIGCOMM 2024)](https://sachin.cs.illinois.edu/papers/traceweaver-ashok-sigcomm24.pdf)
2. [Jaeger Architecture (official documentation)](https://www.jaegertracing.io/docs/1.76/architecture/)
3. [opentelemetry/proto/trace/v1/trace.proto (OTLP)](https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/trace/v1/trace.proto)
4. [Trace Context Level 2 (W3C Candidate Recommendation Draft)](https://www.w3.org/TR/trace-context-2/)
5. [So, you want to trace your distributed system? Key design insights from years of practical experience (CMU-PDL-14-102)](https://www.pdl.cmu.edu/ftp/SelfStar/CMU-PDL-14-102.pdf)
6. [Canopy: An End-to-End Performance Tracing And Analysis System (Facebook, SOSP 2017)](https://cs.brown.edu/people/jcmace/papers/kaldor2017canopy.pdf)
7. [Distributed tracing survey (arXiv preprint, Feb 2025)](https://arxiv.org/pdf/2502.06318)
8. [Context propagation | OpenTelemetry](https://opentelemetry.io/docs/concepts/context-propagation/)
9. [Trace Context (W3C Recommendation, Level 1)](https://www.w3.org/TR/trace-context-1/)
10. [Trace context | Google Cloud documentation](https://cloud.google.com/trace/docs/trace-context)
11. [Notes on Sigelman 2010 Dapper paper](https://mwhittaker.github.io/papers/html/sigelman2010dapper.html)
12. [Zipkin Architecture (official documentation)](https://zipkin.io/pages/architecture.html)
13. [X-Trace: A Pervasive Network Tracing Framework (NSDI 2007)](https://www.usenix.org/legacy/event/nsdi07/tech/full_papers/fonseca/fonseca_html/index.html)
14. [Distributed tracing for Go (Google Cloud blog)](https://cloud.google.com/blog/products/gcp/distributed-tracing-for-go)
15. [Benchmarking the Overhead of Distributed Tracing Agents (ICPE 2026 preprint)](https://icpe2026.spec.org/preprint/Benchmarking_the_Overhead_of_Distributed_Tracing_Agents_2.pdf)
16. [Distributed tracing for agentic workflows with OpenTelemetry | Red Hat Developer](https://developers.redhat.com/articles/2026/04/06/distributed-tracing-agentic-workflows-opentelemetry)
17. [Distributed traces with OBI | OpenTelemetry](https://opentelemetry.io/docs/zero-code/obi/distributed-traces/)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/distributed-tracing

English original: https://www.edgepedia.org/distributed-tracing
