Commit c4fe96a
feat: OpenTelemetry context activation (#202)
An OpenTelemetry `Context` that mirrors the active `tracing` `Span` is
now active on the executing thread while in a `tracing` `Span`. Having
_proper_ OpenTelemetry context activation will not only make
interoperability easier for users of the OpenTelemetry API, but will
also open up the possibility for simpler log/trace/baggage correlation
and in the future profiling/trace correlation among other things.
## Motivation
The aim is to provide users of the OpenTelemetry API a more seamless and
consistent experience. One such thing is that calling the OpenTelemetry
API `Context::current()` did not give you a `Context` that contained an
OpenTelemetry `Span` that represented the current `tracing` `Span`.
Another thing is propagation of `Baggage` or user defined types that
were not picked up either.
A lot of effort has been put into making the existing
`OpenTelemetrySpanExt` API work the same way to maintain maximum
backwards compatibility and to avoid fragmentation by building a
separate OpenTelemetry and Tokio Tracing bridge.
[Long
discussion](open-telemetry/opentelemetry-rust#1571)
about OpenTelemetry and Tokio Tracing interoperability, and an
[issue](open-telemetry/opentelemetry-rust#2420)
specifically discussing this POC.
## Solution
This solution adds a new feature `activate_context` that will
activate/deactivate an OpenTelemetry `Context` that mirrors the current
`tracing` `Span`. This feature is currently on by default, but can be
turned off to avoid any performance overhead incurred by the context
activation and bookkeeping.
The second big change is that the OpenTelemetry `SpanBuilder` is lazily
consumed and a real OpenTelemetry `Span` is created when necessary, so
there is no longer any need for the `PreSampledTracer` and the special
code in OpenTelemetry that it relies on. This has some implications,
like you can no longer set the `parent_context` after you have entered a
`Span` or called the `context` method on the `Span`.
Benchmark numbers are available in this
[comment](open-telemetry/opentelemetry-rust#2420 (comment)),
and when the `activate_context` feature is turned off, the performance
stays the same. (There has been one more optimization since the last run
there, fixing the regression in the `many_events` benchmarks)
---------
Co-authored-by: Scott Gerring <scottgerring@users.noreply.github.com>1 parent 764cd73 commit c4fe96a
File tree
11 files changed
+1350
-648
lines changed- benches
- examples
- src
- tests
11 files changed
+1350
-648
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
264 | 264 | | |
265 | 265 | | |
266 | 266 | | |
267 | | - | |
268 | | - | |
269 | | - | |
| 267 | + | |
270 | 268 | | |
271 | 269 | | |
272 | 270 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
0 commit comments