@@ -4,60 +4,62 @@ Status: **Work-In-Progress**
44
55## Introduction
66
7- This document provides comprehensive guidance on leveraging OpenTelemetry traces
8- in Rust applications.
7+ This document provides guidance on leveraging OpenTelemetry traces in Rust
8+ applications.
99
10- ## Instrumentation Guidance
11-
12- 1 . ** Use OTel API for distributed traces**
13-
14- Use the ` opentelemetry::trace ` API to create spans. This supports context
15- propagation, span kinds (server/client), links, and remote parents.
16-
17- 2 . ** Use tracing for logs/events**
18-
19- Use ` tracing::info! ` , ` tracing::event! ` , etc. for structured logging. This
20- will be converted to OTel LogRecords via opentelemetry-appender-tracing and
21- will be automatically correlated with the current active OTel trace context
22- as well.
23-
24- 3 . ** In-proc contextual enrichment for logs/events**
25-
26- Use ` tracing::span! ` macros to add contextual metadata (e.g., filename) that
27- applies to a group of logs. The ` otel-appender-tracing ` crate will be
28- enhanced to extract span attributes and attach them to logs automatically.
29-
30- OpenTelemetry does not have a spec-ed out solution for in-process contextual
31- enrichment. This is very specific to the logging library (tracing) and its
32- bridge.
10+ In short: use the OpenTelemetry Tracing API to create spans; [ ` tracing ` ] is
11+ for logs and events, not spans. See [ logs.md] ( logs.md ) for log guidance.
3312
34- 4 . ** If using tracing::span! to create spans**
35-
36- This is not directly supported by OpenTelemetry. Use the
37- ` tracing-opentelemetry ` bridge to convert tracing spans into OTel spans.
38-
39- There are some limitations with this approach arising due to ` tracing ` s lack of support for
40- creating Spans following OpenTelemetry specification. For example,
41- ` tracing ` is unable to:
42- - Set remote parent
43- - Specify span kind (e.g., server/client/producer/consumer).
44- - Add span links
45-
46- The bridge offers extension APIs to support some of these, but they are not
47- standard and are maintained outside the OpenTelemetry and Tracing project and
48- within the bridge itself.
49-
50- TODO: Should we make a recommendation about
51- avoiding this extension APIs for instrumentation?
52-
53- If you are creating spans to track in-proc work (what OTel calls "internal" spans),
54- ` tracing:span ` API is sufficient with ` tracing-opentelemetry ` bridge converting the
55- ` tracing ` Span to OTel Span, and properly activating/de-activating OTel's context,
56- to ensure correlation.
57-
58- 5 . ** Use instrumentation libraries when possible**
13+ ## Instrumentation Guidance
5914
60- If you're manually creating ` tracing::span! ` and converting to OTel span for
61- "edge" spans, consider using official instrumentation libraries where
62- available. These handle proper span creation and context propagation using
63- the OpenTelemetry API directly.
15+ 1 . ** Use the OpenTelemetry Tracing API to create spans.** The
16+ ` opentelemetry::trace ` API is designed around the OpenTelemetry
17+ specification, with first-class support for span kind
18+ (server/client/producer/consumer/internal), links, remote parents, and
19+ context propagation across process boundaries.
20+
21+ 2 . ** [ ` tracing ` ] is not a complete substitute for the OpenTelemetry Tracing API.**
22+ The ` tracing ` crate does not have a first-class notion of an OpenTelemetry
23+ Span. It cannot, on its own, set span kind, attach links, or set a remote
24+ parent — concepts central to the OpenTelemetry specification, particularly
25+ for * edge* spans (see #3 below for nuance). Use it primarily for
26+ logs/events (see [ logs.md] ( logs.md ) ).
27+
28+ 3 . ** Bridging from ` tracing::span! ` to OpenTelemetry spans.** If you are
29+ already using ` tracing::span! ` and want those spans surfaced as
30+ OpenTelemetry spans, the third-party [ ` tracing-opentelemetry ` ] crate
31+ provides a bridge. It is maintained outside the OpenTelemetry project and
32+ is not part of this repo; we point to it so users are aware of the option
33+ in the broader ecosystem.
34+
35+ For * internal* spans (spans that represent in-process work and never cross
36+ a process boundary), ` tracing::span! ` through this bridge produces a
37+ result nearly identical to using the OpenTelemetry Tracing API directly —
38+ span kind, links, and remote parent are not relevant for internal spans.
39+ The ` tracing ` limitations matter primarily for * edge* spans (e.g.,
40+ incoming/outgoing HTTP, messaging), where span kind, links, and remote
41+ parents are central to the OpenTelemetry data model. The bridge offers
42+ extension APIs to express these concepts.
43+
44+ 4 . ** Prefer instrumentation libraries.** For framework-level spans (HTTP
45+ servers/clients, database drivers, messaging), prefer instrumentation
46+ libraries that use the OpenTelemetry Tracing API directly. No stable
47+ instrumentation libraries exist yet in the OpenTelemetry Rust ecosystem,
48+ but in-progress ones for Tower and Actix-Web exist in the
49+ [ opentelemetry-rust-contrib] repository:
50+ [ ` opentelemetry-instrumentation-tower ` ] and
51+ [ ` opentelemetry-instrumentation-actix-web ` ] .
52+
53+ ## See Also
54+
55+ - [ OpenTelemetry Traces Specification] ( https://opentelemetry.io/docs/specs/otel/trace/ )
56+ - [ Main README] ( ../README.md )
57+ - [ logs.md] ( logs.md ) — guidance for logs/events
58+ - [ examples/tracing-http-propagator] ( ../examples/tracing-http-propagator/ ) — end-to-end span creation and W3C context propagation
59+ - [ examples/tracing-grpc] ( ../examples/tracing-grpc/ ) — span creation and propagation over gRPC
60+
61+ [ `tracing` ] : https://crates.io/crates/tracing
62+ [ `tracing-opentelemetry` ] : https://crates.io/crates/tracing-opentelemetry
63+ [ opentelemetry-rust-contrib ] : https://github.com/open-telemetry/opentelemetry-rust-contrib
64+ [ `opentelemetry-instrumentation-tower` ] : https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-instrumentation-tower
65+ [ `opentelemetry-instrumentation-actix-web` ] : https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-instrumentation-actix-web
0 commit comments