Skip to content

Commit 5566af1

Browse files
dd-octo-sts[bot]dubloombrettlangdon
authored
fix(ray): error when ray.init() was not called [backport 3.17] (#15116)
Backport 0b92c07 from #15087 to 3.17. Without an explicit `ray.init()` call at the beginning of the file, the ray job would fail because the `ray_trace_ctx` was not injected at the right moment. This PR fixes that Signed-off-by: Louis Tricot <[email protected]> Co-authored-by: Louis Tricot <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent af2b9c0 commit 5566af1

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

ddtrace/contrib/internal/ray/patch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ def traced_submit_task(wrapped, instance, args, kwargs):
152152
# This is done under a lock as multiple task could be submit at the same time
153153
# and thus try to modify the signature as the same time
154154
with instance._inject_lock:
155-
if instance._function_signature is None:
155+
if not getattr(instance._function, "_dd_trace_wrapped", False):
156156
instance._function = _wrap_remote_function_execution(instance._function)
157157
instance._function.__signature__ = _inject_dd_trace_ctx_kwarg(instance._function)
158158
instance._function_signature = extract_signature(instance._function)
159+
instance._function._dd_trace_wrapped = True
159160

160161
with tracer.trace(
161162
"task.submit",
@@ -170,7 +171,11 @@ def traced_submit_task(wrapped, instance, args, kwargs):
170171
if config.ray.trace_args_kwargs:
171172
set_tag_or_truncate(span, RAY_TASK_ARGS, kwargs.get("args", {}))
172173
set_tag_or_truncate(span, RAY_TASK_KWARGS, kwargs.get("kwargs", {}))
173-
_inject_context_in_kwargs(span.context, kwargs)
174+
175+
# Check if signature has the trace context parameter
176+
has_trace_ctx = DD_RAY_TRACE_CTX in inspect.signature(instance._function).parameters
177+
if has_trace_ctx:
178+
_inject_context_in_kwargs(span.context, kwargs)
174179

175180
resp = wrapped(*args, **kwargs)
176181

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
ray: This fix resolves an issue where the tracer raised an error when submitting Ray tasks without explicitly calling ``ray.init()``.

tests/contrib/ray/test_ray.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,22 @@ def increment(self, x, y=1):
278278
current_value = ray.get(counter_actor.increment.remote(1, y=2))
279279

280280
assert current_value == 3, f"Unexpected result: {current_value}"
281+
282+
283+
class TestRayWithoutInit(TracerTestCase):
284+
def tearDown(self):
285+
if ray.is_initialized():
286+
ray.shutdown()
287+
super().tearDown()
288+
289+
@pytest.mark.snapshot(token="tests.contrib.ray.test_ray.test_task_without_init", ignores=RAY_SNAPSHOT_IGNORES)
290+
def test_task_without_init(self):
291+
"""Test that tracing works when Ray auto-initializes without explicit ray.init()"""
292+
293+
@ray.remote
294+
def add_one(x):
295+
return x + 1
296+
297+
futures = [add_one.remote(i) for i in range(2)]
298+
results = ray.get(futures)
299+
assert results == [1, 2], f"Unexpected results: {results}"
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[[
2+
{
3+
"name": "task.submit",
4+
"service": "tests.contrib.ray",
5+
"resource": "tests.contrib.ray.test_ray.add_one.remote",
6+
"trace_id": 0,
7+
"span_id": 1,
8+
"parent_id": 0,
9+
"type": "ray",
10+
"error": 0,
11+
"meta": {
12+
"_dd.p.dm": "-0",
13+
"_dd.p.tid": "69025c6200000000",
14+
"component": "ray",
15+
"language": "python",
16+
"ray.hostname": "docker-desktop",
17+
"ray.task.submit_status": "success",
18+
"runtime-id": "467a8549e29149d3a0a561b9407dc4f6",
19+
"span.kind": "producer"
20+
},
21+
"metrics": {
22+
"_dd.ai_obs.enabled": 1,
23+
"_dd.djm.enabled": 1,
24+
"_dd.filter.kept": 1,
25+
"_dd.measured": 1,
26+
"_dd.top_level": 1,
27+
"_dd.tracer_kr": 1.0,
28+
"_sampling_priority_v1": 2,
29+
"process_id": 605
30+
},
31+
"duration": 3830005877,
32+
"start": 1761762402453522879
33+
}],
34+
[
35+
{
36+
"name": "task.submit",
37+
"service": "tests.contrib.ray",
38+
"resource": "tests.contrib.ray.test_ray.add_one.remote",
39+
"trace_id": 1,
40+
"span_id": 1,
41+
"parent_id": 0,
42+
"type": "ray",
43+
"error": 0,
44+
"meta": {
45+
"_dd.p.dm": "-0",
46+
"_dd.p.tid": "69025c6600000000",
47+
"component": "ray",
48+
"language": "python",
49+
"ray.hostname": "docker-desktop",
50+
"ray.job_id": "01000000",
51+
"ray.node_id": "9221982158053df5fa3e8b7cfe1f487b8aef5fb4b18f9fcfc3a76944",
52+
"ray.task.submit_status": "success",
53+
"ray.worker_id": "01000000ffffffffffffffffffffffffffffffffffffffffffffffff",
54+
"runtime-id": "467a8549e29149d3a0a561b9407dc4f6",
55+
"span.kind": "producer"
56+
},
57+
"metrics": {
58+
"_dd.ai_obs.enabled": 1,
59+
"_dd.djm.enabled": 1,
60+
"_dd.filter.kept": 1,
61+
"_dd.measured": 1,
62+
"_dd.top_level": 1,
63+
"_dd.tracer_kr": 1.0,
64+
"_sampling_priority_v1": 2,
65+
"process_id": 605
66+
},
67+
"duration": 389792,
68+
"start": 1761762406283924423
69+
}]]

0 commit comments

Comments
 (0)