Open lily-mara opened 3 years ago
This is critical imho... I did a shallow research on how this works. It looks like this can be fixed by explicitly setting otel context when starting a span, and then reset context to the previous value on span exit. I've checked and this working, however, there is a problem I wasn't able to overcome.
Code for tracing-opentelemetry::OpenTelemetryLayer
:
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
// ...
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
// ...
let ctx_guard = extensions
.get_mut::<otel::SpanBuilder>()
.map(|builder| self.tracer.sampled_context(builder).attach());
extensions.insert(ctx_guard);
}
fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) {
// ...
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
// ...
extensions.remove::<Option<opentelemetry::ContextGuard>>();
}
In order to be able to keep context active and reset later, we need to store ContextGuard
as long as span active.
I tried to use extensions
for this. However, extensions
methods require Send + Sync
and ContextGuard
explicitly forbids Send
because it relies on thread locals...
I can trick compiler by using a wrapped type with unsafe impl
, but I don't know why exactly ExtensionsMut
requires these, so can be unsafe indeed :)
Perhaps with a help of tracing-opentelemetry
contributor we can fix this relatively easily
Bug Report
Version
Platform
Linux DESKTOP-8SQJE0D 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Crates
tracing-opentelemetry
Description