Skip to content

Commit 147e660

Browse files
fix(tracer): use active context in get_log_correlation_context (#3532) (#3561)
Formerly only the active span would be considered for get_log_correlation_context. This would break log-trace correlation when tracing across execution boundaries as the context is propagated and activated. (cherry picked from commit 9d67013) Co-authored-by: Kyle Verhoog <[email protected]>
1 parent 13e24ce commit 147e660

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ddtrace/tracer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,18 @@ def get_log_correlation_context(self):
291291
span id of the current active span, as well as the configured service, version, and environment names.
292292
If there is no active span, a dictionary with an empty string for each value will be returned.
293293
"""
294-
span = None
294+
active = None # type: Optional[Union[Context, Span]]
295295
if self.enabled:
296-
span = self.current_span()
296+
active = self.context_provider.active()
297297

298-
if span and span.service:
299-
service = span.service
298+
if isinstance(active, Span) and active.service:
299+
service = active.service
300300
else:
301301
service = config.service
302302

303303
return {
304-
"trace_id": str(span.trace_id) if span else "0",
305-
"span_id": str(span.span_id) if span else "0",
304+
"trace_id": str(active.trace_id) if active else "0",
305+
"span_id": str(active.span_id) if active else "0",
306306
"service": service or "",
307307
"version": config.version or "",
308308
"env": config.env or "",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
``tracer.get_log_correlation_context()``: use active context in addition to
5+
active span. Formerly just the span was used and this would break cross
6+
execution log correlation as a context object is used for the propagation.

tests/tracer/test_correlation_log_context.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ddtrace import Tracer
55
from ddtrace import config
66
from ddtrace import tracer
7+
from ddtrace.context import Context
78
from ddtrace.opentracer.tracer import Tracer as OT_Tracer
89
from tests.utils import override_global_config
910

@@ -71,6 +72,20 @@ def test_get_log_correlation_context(self, global_config):
7172
"version": "test-version",
7273
}
7374

75+
tracer.context_provider.activate(
76+
Context(
77+
span_id=234,
78+
trace_id=4321,
79+
)
80+
)
81+
assert test_tracer.get_log_correlation_context() == {
82+
"span_id": "234",
83+
"trace_id": "4321",
84+
"service": "test-service",
85+
"env": "test-env",
86+
"version": "test-version",
87+
}
88+
7489
def test_get_log_correlation_context_opentracer(self, global_config):
7590
"""Ensure expected DDLogRecord generated via get_correlation_log_record with an opentracing Tracer."""
7691
ot_tracer = OT_Tracer(service_name="test-service")

0 commit comments

Comments
 (0)