feat: OpenTelemetry context activation POC #202
Draft
+908
−382
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
This PR is opened as a basis for discussion around providing a more seamless interoperability for users of the OpenTelemetry APIs when using frameworks and libraries that use
tracing
at their core.An OpenTelemetry
Context
that mirrors the activetracing
Span
is now active on the executing thread while in atracing
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 aContext
that contained an OpenTelemetrySpan
that represented the currenttracing
Span
. Another thing is propagation ofBaggage
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 about OpenTelemetry and Tokio Tracing interoperability, and an issue specifically discussing this POC.
Solution
This solution adds a new feature
activate_context
that will activate/deactivate an OpenTelemetryContext
that mirrors the currenttracing
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 OpenTelemetrySpan
is created when necessary, so there is no longer any need for thePreSampledTracer
and the special code in OpenTelemetry that it relies on. This has some implications, like you can no longer set theparent_context
after you have entered aSpan
or called thecontext
method on theSpan
.Benchmark numbers are available in this 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 themany_events
benchmarks)