Merge branch 'pratik/otel-phase1b-telemetry-infra' into pratik/otel-phase1c-rpc-integration

This commit is contained in:
Pratik Mankawde
2026-07-21 21:07:24 +01:00

View File

@@ -298,9 +298,15 @@ SpanGuard::linkedSpan(std::string_view name, SpanContext const& linkCtx)
SpanContext
SpanGuard::captureContext() const
{
if (!impl_)
if (!impl_ || !impl_->span)
return {};
auto ctx = opentelemetry::context::RuntimeContext::GetCurrent();
// Capture THIS guard's own span, not the thread's ambient span. A
// SpanGuard is unscoped (never pushed onto the thread-local stack), so
// RuntimeContext::GetCurrent() would return an unrelated ambient span.
// Building the context from impl_->span makes captureContext() correct
// for every caller regardless of scoping, and lets childSpan(name, ctx)
// parent explicitly to this span across threads.
auto ctx = opentelemetry::context::Context{}.SetValue(otel_trace::kSpanKey, impl_->span);
return SpanContext(std::make_shared<SpanContext::Impl>(std::move(ctx)));
}