tracing-subscriber: remove clone_span on enter #3289
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
I briefly discussed this on Discord. At work, we enabled continuous profiling which gives us a flamegraph on our production workloads. Inspecting one of our hot-paths, I was surprised to see Span::enter being a significant percent of the samples.
This is currently using
tracing-subscriber = "0.3.19"
.Concrete numbers:
7.7% of samples were found within the stacktrace of a
Span::enter
.Of those samples, 22.8% were inside
clone_span
.Collectively, that's 1.8% of total CPU time spent in
clone_span
as a result ofSpan::enter
.These numbers don't surprise me. Our hot-loop of our service is a modified copy_bidirectional. Each poll will often be very short, so the
Span::enter
will take a not insignificant percentage of the time. And since it's only IO that can wake the task, tokio will likely move it around threads depending on which thread happened to be the driver, so any atomic operations (eg a fetch_add) will likely be a cache miss.The rest of the time of
clone_span
seems to be within our various layers, we can address those somewhere else.When looking through the code, I couldn't find a good justification for
enter
to needclone_span
. I understand it might be defensive programming - someone can use theCollect
api without usingtracing::Span
, and they can use it incorrectly. It would thus prevent some panics on exit (a layer using LookupSpan on exit and unwrapping) which would better prevent double-panics on drop handling during a panic. However, I see no unsafe code relying on spans definitely being alive during exit, and when usingtracing::Span::enter
which I think is the blessed case, the invariant that the span is alive duringexit
is upheld.Solution
Remove
clone_span
fromenter
, removetry_close
fromexit
.