Skip to content

Commit f6e9162

Browse files
authored
Fix ray tests (#3877)
Make sure there is a transaction name
1 parent 60d6333 commit f6e9162

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

sentry_sdk/integrations/ray.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from typing import Any, Optional
2727
from sentry_sdk.utils import ExcInfo
2828

29+
DEFAULT_TRANSACTION_NAME = "unknown Ray function"
30+
2931

3032
def _check_sentry_initialized():
3133
# type: () -> None
@@ -58,18 +60,23 @@ def _f(*f_args, _tracing=None, **f_kwargs):
5860
"""
5961
_check_sentry_initialized()
6062

63+
root_span_name = qualname_from_function(f) or DEFAULT_TRANSACTION_NAME
64+
sentry_sdk.get_current_scope().set_transaction_name(
65+
root_span_name,
66+
source=TRANSACTION_SOURCE_TASK,
67+
)
6168
with sentry_sdk.continue_trace(_tracing or {}):
62-
with sentry_sdk.start_transaction(
69+
with sentry_sdk.start_span(
6370
op=OP.QUEUE_TASK_RAY,
64-
name=qualname_from_function(f) or "unknown Ray function",
71+
name=root_span_name,
6572
origin=RayIntegration.origin,
6673
source=TRANSACTION_SOURCE_TASK,
67-
) as transaction:
74+
) as root_span:
6875
try:
6976
result = f(*f_args, **f_kwargs)
70-
transaction.set_status(SPANSTATUS.OK)
77+
root_span.set_status(SPANSTATUS.OK)
7178
except Exception:
72-
transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
79+
root_span.set_status(SPANSTATUS.INTERNAL_ERROR)
7380
exc_info = sys.exc_info()
7481
_capture_exception(exc_info)
7582
reraise(*exc_info)

tests/integrations/ray/test_ray.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,42 +77,42 @@ def example_task():
7777

7878
return sentry_sdk.get_client().transport.envelopes
7979

80-
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
80+
with sentry_sdk.start_span(op="test", name="ray client root span"):
8181
worker_envelopes = ray.get(example_task.remote())
8282

8383
client_envelope = sentry_sdk.get_client().transport.envelopes[0]
84-
client_transaction = client_envelope.get_transaction_event()
85-
assert client_transaction["transaction"] == "ray test transaction"
86-
assert client_transaction["transaction_info"] == {"source": "custom"}
84+
client_root_span = client_envelope.get_transaction_event()
85+
assert client_root_span["transaction"] == "ray client root span"
86+
assert client_root_span["transaction_info"] == {"source": "custom"}
8787

8888
worker_envelope = worker_envelopes[0]
89-
worker_transaction = worker_envelope.get_transaction_event()
89+
worker_root_span = worker_envelope.get_transaction_event()
9090
assert (
91-
worker_transaction["transaction"]
91+
worker_root_span["transaction"]
9292
== "tests.integrations.ray.test_ray.test_tracing_in_ray_tasks.<locals>.example_task"
9393
)
94-
assert worker_transaction["transaction_info"] == {"source": "task"}
94+
assert worker_root_span["transaction_info"] == {"source": "task"}
9595

96-
(span,) = client_transaction["spans"]
96+
(span,) = client_root_span["spans"]
9797
assert span["op"] == "queue.submit.ray"
9898
assert span["origin"] == "auto.queue.ray"
9999
assert (
100100
span["description"]
101101
== "tests.integrations.ray.test_ray.test_tracing_in_ray_tasks.<locals>.example_task"
102102
)
103-
assert span["parent_span_id"] == client_transaction["contexts"]["trace"]["span_id"]
104-
assert span["trace_id"] == client_transaction["contexts"]["trace"]["trace_id"]
103+
assert span["parent_span_id"] == client_root_span["contexts"]["trace"]["span_id"]
104+
assert span["trace_id"] == client_root_span["contexts"]["trace"]["trace_id"]
105105

106-
(span,) = worker_transaction["spans"]
106+
(span,) = worker_root_span["spans"]
107107
assert span["op"] == "task"
108108
assert span["origin"] == "manual"
109109
assert span["description"] == "example task step"
110-
assert span["parent_span_id"] == worker_transaction["contexts"]["trace"]["span_id"]
111-
assert span["trace_id"] == worker_transaction["contexts"]["trace"]["trace_id"]
110+
assert span["parent_span_id"] == worker_root_span["contexts"]["trace"]["span_id"]
111+
assert span["trace_id"] == worker_root_span["contexts"]["trace"]["trace_id"]
112112

113113
assert (
114-
client_transaction["contexts"]["trace"]["trace_id"]
115-
== worker_transaction["contexts"]["trace"]["trace_id"]
114+
client_root_span["contexts"]["trace"]["trace_id"]
115+
== worker_root_span["contexts"]["trace"]["trace_id"]
116116
)
117117

118118

@@ -132,7 +132,7 @@ def test_errors_in_ray_tasks():
132132
def example_task():
133133
1 / 0
134134

135-
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
135+
with sentry_sdk.start_span(op="test", name="ray client root span"):
136136
with pytest.raises(ZeroDivisionError):
137137
future = example_task.remote()
138138
ray.get(future)
@@ -167,22 +167,24 @@ def __init__(self):
167167
self.n = 0
168168

169169
def increment(self):
170-
with sentry_sdk.start_span(op="task", name="example actor execution"):
170+
with sentry_sdk.start_span(
171+
op="test", name="custom span in actor execution", only_if_parent=True
172+
):
171173
self.n += 1
172174

173175
return sentry_sdk.get_client().transport.envelopes
174176

175-
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
177+
with sentry_sdk.start_span(op="test", name="ray client root span"):
176178
counter = Counter.remote()
177179
worker_envelopes = ray.get(counter.increment.remote())
178180

179181
client_envelope = sentry_sdk.get_client().transport.envelopes[0]
180-
client_transaction = client_envelope.get_transaction_event()
182+
client_root_span = client_envelope.get_transaction_event()
181183

182184
# Spans for submitting the actor task are not created (actors are not supported yet)
183-
assert client_transaction["spans"] == []
185+
assert client_root_span["spans"] == []
184186

185-
# Transaction are not yet created when executing ray actors (actors are not supported yet)
187+
# Root spans are not yet automatically created when executing ray actors (actors are not supported yet)
186188
assert worker_envelopes == []
187189

188190

@@ -204,12 +206,14 @@ def __init__(self):
204206
self.n = 0
205207

206208
def increment(self):
207-
with sentry_sdk.start_span(op="task", name="example actor execution"):
209+
with sentry_sdk.start_span(
210+
op="test", name="custom span in actor execution", only_if_parent=True
211+
):
208212
1 / 0
209213

210214
return sentry_sdk.get_client().transport.envelopes
211215

212-
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
216+
with sentry_sdk.start_span(op="test", name="ray client root span"):
213217
with pytest.raises(ZeroDivisionError):
214218
counter = Counter.remote()
215219
future = counter.increment.remote()

0 commit comments

Comments
 (0)