Skip to content

Commit a1e9f39

Browse files
authored
fix(tracing): ensure trace info is correctly set on subprocess (#12741)
Resolves an issue where trace information, such as span links, baggage, and trace-level tags (ex: sampling decision maker), could be lost when a new process was created while a trace was active. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent e477a8f commit a1e9f39

6 files changed

+14
-13
lines changed

ddtrace/_trace/tracer.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,13 +701,7 @@ def _start_span(
701701
# strong span reference (which will never be finished) is replaced
702702
# with a context representing the span.
703703
if isinstance(child_of, Span):
704-
new_ctx = Context(
705-
sampling_priority=child_of.context.sampling_priority,
706-
span_id=child_of.span_id,
707-
trace_id=child_of.trace_id,
708-
is_remote=False,
709-
)
710-
704+
new_ctx = child_of.context
711705
# If the child_of span was active then activate the new context
712706
# containing it so that the strong span referenced is removed
713707
# from the execution.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: Resolves an issue where trace information, such as span links, baggage, and trace-level tags (ex: sampling decision maker), could be lost when a new process was created while a trace was active.

tests/integration/test_integration_snapshots.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ def test_synchronous_writer():
118118
def test_tracer_trace_across_popen():
119119
"""
120120
When a trace is started in a parent process and a child process is spawned
121-
The trace should be continued in the child process. The fact that
122-
the child span has does not have '_dd.p.dm' shows that sampling was run
123-
before fork automatically.
121+
The trace should be continued in the child process.
124122
"""
125123

126124
def task(tracer):
@@ -140,9 +138,7 @@ def task(tracer):
140138
def test_tracer_trace_across_multiple_popens():
141139
"""
142140
When a trace is started and crosses multiple process boundaries
143-
The trace should be continued in the child processes. The fact that
144-
the child span has does not have '_dd.p.dm' shows that sampling was run
145-
before fork automatically.
141+
The trace should be continued in the child processes.
146142
"""
147143

148144
def task(tracer):

tests/snapshots/tests.integration.test_integration_snapshots.test_tracer_trace_across_multiple_popens.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"type": "",
3333
"error": 0,
3434
"meta": {
35+
"_dd.p.dm": "-0",
3536
"language": "python",
3637
"runtime-id": "60f937fe2fa5494c8aeceab4b18a5174"
3738
},
@@ -54,6 +55,7 @@
5455
"type": "",
5556
"error": 0,
5657
"meta": {
58+
"_dd.p.dm": "-0",
5759
"language": "python",
5860
"runtime-id": "0a98b167e74c408abbb58db3fd26a2e7"
5961
},

tests/snapshots/tests.integration.test_integration_snapshots.test_tracer_trace_across_popen.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"type": "",
3333
"error": 0,
3434
"meta": {
35+
"_dd.p.dm": "-0",
3536
"language": "python",
3637
"runtime-id": "36f1a5c9396e469291422d40f4296160"
3738
},

tests/tracer/test_tracer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,12 +1696,16 @@ def test_fork_manual_span_same_context():
16961696
from ddtrace.trace import tracer
16971697

16981698
span = tracer.trace("test")
1699+
span.context.set_baggage_item("key", "value")
1700+
span.context._meta["_dd.p.dm"] = "-1"
16991701
pid = os.fork()
17001702

17011703
if pid == 0:
17021704
child = tracer.start_span("child", child_of=span)
17031705
assert child.parent_id == span.span_id
17041706
assert child._parent is None
1707+
# Ensure the child context is the same as the parent context
1708+
assert child.context == span.context
17051709
# No more current span strong reference to avoid memory leaks.
17061710
assert tracer.current_span() is None
17071711
child.finish()

0 commit comments

Comments
 (0)