tracing: make it possible to use remote IDs as parent span ID #2953
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.
Motivation
Currrently, only locally tracked spans can be used as a parent span. However, in distributed systems, the parent is actually often a span originating in another service and this relationship is desirable to be tracked.
For example, a server handling a request may have received a header specifying an ID of a remote span of the client making this call and some information from this should be saved and be available for subscribers to use.
The main example of this would be OpenTelemetry, which propagates globally unique trace and span IDs by which different spans from different systems are grouped into a single trace.
Currently,
tracing-opentelemetry
exposes a method that allows a span to to add the information about the remote parent after the span is already created. This is suboptimal because the span may be entered before this information is provided and it can be even changed later on. It would be instead better to force the user to provide the information during span creation time and not afterwards.Solution
Parent ID was tracked as
Option<span::Id>
which was changed to a custom enum with three variants, one equivalent to the formerOption::None
for root spans, one equivalent toOption::Some(id)
for locally tracked spans which can be looked up in the sameCollect
that tracks the child span (if it implementsLookupSpan
) and a new variant for remote spans.This variant wraps a trait object that can be then downcasted by
Subscribe
implementations. This is becausetracing
should not force users to use any representation for their remote spans.One possibly major change this creates is that
Span::child_of
now takes an argument ofT: Into<ParentId>
, where it used to takeT: Into<Option<Id>>
. All implementations that were provided before were added, but this may break some users which may have implementedInto<Option<Id>>
for their own types.TODO
These things should be done before merging but can wait until I get a greenlight on the current implementation.
no_std
support -- Since the remote ID variant usesBox
, this only works withalloc
. I will add conditional compilation as needed.tests -- all current tests work but of course they don't test for remote contexts at all.
docs -- new public API is documented but I will have to look through all current documentation for mentions of
Option<Id>
and possibly add some explanation in the root of the crate and other places.