Skip to content

Commit 18d13db

Browse files
committed
fix: improve trace context extraction from non-recording spans
1 parent dce07f5 commit 18d13db

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/croudtech_gcp_otel/logging.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ def _get_trace_context(self, record):
6262
from opentelemetry import trace
6363

6464
span = trace.get_current_span()
65-
if span and span.is_recording():
66-
ctx = span.get_span_context()
67-
if ctx.is_valid:
68-
return (
69-
format(ctx.trace_id, '032x'),
70-
format(ctx.span_id, '016x'),
71-
ctx.trace_flags.sampled
72-
)
65+
span_context = span.get_span_context() if span else None
66+
67+
# Check if we have a valid, recording span
68+
if span_context and span_context.is_valid:
69+
return (
70+
format(span_context.trace_id, '032x'),
71+
format(span_context.span_id, '016x'),
72+
span_context.trace_flags.sampled if hasattr(span_context.trace_flags, 'sampled') else False
73+
)
7374
except ImportError:
7475
pass
7576
except Exception:

0 commit comments

Comments
 (0)