-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
ddtrace.context.Context object is not serializable, meaning it cannot be pickled/shared between processes. This breaks the example usage we have in our documentation for passing context through to other threads. e.g. Process(target=_target, args=(ctx, )) This fix added __getstate__ and __setstate__ methods to Context class to have pickle ignore the RLock which cannot be serialized. Co-authored-by: Munir Abdinur <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]> (cherry picked from commit 96e6bca) Co-authored-by: Brett Langdon <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e9ae959
commit 40111cb
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/fix-context-serializable-2c9768cbe4738b73.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
fixes: | ||
- | | ||
tracing: make ``ddtrace.context.Context`` serializable which fixes distributed tracing across processes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
|
||
from tests.utils import snapshot | ||
|
||
from .test_integration import AGENT_VERSION | ||
|
||
|
||
pytestmark = pytest.mark.skipif(AGENT_VERSION != "testagent", reason="Tests only compatible with a testagent") | ||
|
||
|
||
@snapshot() | ||
def test_context_multiprocess(run_python_code_in_subprocess): | ||
# Testing example from our docs: | ||
# https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#tracing-across-processes | ||
code = """ | ||
from multiprocessing import Process | ||
import time | ||
from ddtrace import tracer | ||
def _target(ctx): | ||
tracer.context_provider.activate(ctx) | ||
with tracer.trace("proc"): | ||
time.sleep(0.1) | ||
tracer.shutdown() | ||
def main(): | ||
with tracer.trace("work"): | ||
proc = Process(target=_target, args=(tracer.current_trace_context(), )) | ||
proc.start() | ||
time.sleep(0.25) | ||
proc.join() | ||
if __name__ == "__main__": | ||
main() | ||
""" | ||
|
||
stdout, stderr, status, _ = run_python_code_in_subprocess(code=code) | ||
assert status == 0, (stdout, stderr) | ||
assert stdout == b"", stderr | ||
assert stderr == b"", stdout |
42 changes: 42 additions & 0 deletions
42
tests/snapshots/tests.integration.test_context_snapshots.test_context_multiprocess.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[[ | ||
{ | ||
"name": "work", | ||
"service": null, | ||
"resource": "work", | ||
"trace_id": 0, | ||
"span_id": 1, | ||
"parent_id": 0, | ||
"meta": { | ||
"_dd.p.dm": "-0", | ||
"runtime-id": "f706b29e0e8049178c7f1e0ac8d01ab7" | ||
}, | ||
"metrics": { | ||
"_dd.agent_psr": 1.0, | ||
"_dd.top_level": 1, | ||
"_dd.tracer_kr": 1.0, | ||
"_sampling_priority_v1": 1, | ||
"system.pid": 25193 | ||
}, | ||
"duration": 259538000, | ||
"start": 1667237294717521000 | ||
}, | ||
{ | ||
"name": "proc", | ||
"service": null, | ||
"resource": "proc", | ||
"trace_id": 0, | ||
"span_id": 2, | ||
"parent_id": 1, | ||
"meta": { | ||
"_dd.p.dm": "-0", | ||
"runtime-id": "38ca0ac0547f4097b2e030ebff1064c7" | ||
}, | ||
"metrics": { | ||
"_dd.top_level": 1, | ||
"_dd.tracer_kr": 1.0, | ||
"_sampling_priority_v1": 1, | ||
"system.pid": 25194 | ||
}, | ||
"duration": 100317000, | ||
"start": 1667237294727339000 | ||
}]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters