11import json
22import os
33import pytest
4+ import shutil
5+ import uuid
46
57import ray
68
@@ -26,9 +28,6 @@ def capture_envelope(self, envelope: Envelope) -> None:
2628
2729
2830class RayLoggingTransport (TestTransport ):
29- def __init__ (self ):
30- super ().__init__ ()
31-
3231 def capture_envelope (self , envelope : Envelope ) -> None :
3332 print (envelope .serialize ().decode ("utf-8" , "replace" ))
3433
@@ -45,8 +44,18 @@ def setup_sentry(transport=None):
4544 )
4645
4746
48- def read_error_from_log (job_id ):
49- log_dir = "/tmp/ray/session_latest/logs/"
47+ def read_error_from_log (job_id , ray_temp_dir ):
48+ # Find the actual session directory that Ray created
49+ session_dirs = [d for d in os .listdir (ray_temp_dir ) if d .startswith ("session_" )]
50+ if not session_dirs :
51+ raise FileNotFoundError (f"No session directory found in { ray_temp_dir } " )
52+
53+ session_dir = os .path .join (ray_temp_dir , session_dirs [0 ])
54+ log_dir = os .path .join (session_dir , "logs" )
55+
56+ if not os .path .exists (log_dir ):
57+ raise FileNotFoundError (f"No logs directory found at { log_dir } " )
58+
5059 log_file = [
5160 f
5261 for f in os .listdir (log_dir )
@@ -132,33 +141,44 @@ def example_task():
132141def test_errors_in_ray_tasks ():
133142 setup_sentry_with_logging_transport ()
134143
135- ray .init (
136- runtime_env = {
137- "worker_process_setup_hook" : setup_sentry_with_logging_transport ,
138- "working_dir" : "./" ,
139- }
140- )
141-
142- # Setup ray task
143- @ray .remote
144- def example_task ():
145- 1 / 0
146-
147- with sentry_sdk .start_transaction (op = "task" , name = "ray test transaction" ):
148- with pytest .raises (ZeroDivisionError ):
149- future = example_task .remote ()
150- ray .get (future )
151-
152- job_id = future .job_id ().hex ()
153- error = read_error_from_log (job_id )
154-
155- assert error ["level" ] == "error"
156- assert (
157- error ["transaction" ]
158- == "tests.integrations.ray.test_ray.test_errors_in_ray_tasks.<locals>.example_task"
159- )
160- assert error ["exception" ]["values" ][0 ]["mechanism" ]["type" ] == "ray"
161- assert not error ["exception" ]["values" ][0 ]["mechanism" ]["handled" ]
144+ # Create a short temp directory to avoid Unix socket path length limits
145+ ray_temp_dir = os .path .join ("/tmp" , f"ray_test_{ uuid .uuid4 ().hex [:8 ]} " )
146+ os .makedirs (ray_temp_dir , exist_ok = True )
147+
148+ try :
149+ ray .init (
150+ runtime_env = {
151+ "worker_process_setup_hook" : setup_sentry_with_logging_transport ,
152+ "working_dir" : "./" ,
153+ },
154+ _temp_dir = ray_temp_dir ,
155+ )
156+
157+ # Setup ray task
158+ @ray .remote
159+ def example_task ():
160+ 1 / 0
161+
162+ with sentry_sdk .start_transaction (op = "task" , name = "ray test transaction" ):
163+ with pytest .raises (ZeroDivisionError ):
164+ future = example_task .remote ()
165+ ray .get (future )
166+
167+ job_id = future .job_id ().hex ()
168+ error = read_error_from_log (job_id , ray_temp_dir )
169+
170+ assert error ["level" ] == "error"
171+ assert (
172+ error ["transaction" ]
173+ == "tests.integrations.ray.test_ray.test_errors_in_ray_tasks.<locals>.example_task"
174+ )
175+ assert error ["exception" ]["values" ][0 ]["mechanism" ]["type" ] == "ray"
176+ assert not error ["exception" ]["values" ][0 ]["mechanism" ]["handled" ]
177+
178+ finally :
179+ # Clean up the temporary directory
180+ if os .path .exists (ray_temp_dir ):
181+ shutil .rmtree (ray_temp_dir , ignore_errors = True )
162182
163183
164184def test_tracing_in_ray_actors ():
@@ -200,33 +220,44 @@ def increment(self):
200220def test_errors_in_ray_actors ():
201221 setup_sentry_with_logging_transport ()
202222
203- ray .init (
204- runtime_env = {
205- "worker_process_setup_hook" : setup_sentry_with_logging_transport ,
206- "working_dir" : "./" ,
207- }
208- )
209-
210- # Setup ray actor
211- @ray .remote
212- class Counter :
213- def __init__ (self ):
214- self .n = 0
215-
216- def increment (self ):
217- with sentry_sdk .start_span (op = "task" , name = "example actor execution" ):
218- 1 / 0
219-
220- return sentry_sdk .get_client ().transport .envelopes
221-
222- with sentry_sdk .start_transaction (op = "task" , name = "ray test transaction" ):
223- with pytest .raises (ZeroDivisionError ):
224- counter = Counter .remote ()
225- future = counter .increment .remote ()
226- ray .get (future )
227-
228- job_id = future .job_id ().hex ()
229- error = read_error_from_log (job_id )
230-
231- # We do not capture errors in ray actors yet
232- assert error is None
223+ # Create a short temp directory to avoid Unix socket path length limits
224+ ray_temp_dir = os .path .join ("/tmp" , f"ray_test_{ uuid .uuid4 ().hex [:8 ]} " )
225+ os .makedirs (ray_temp_dir , exist_ok = True )
226+
227+ try :
228+ ray .init (
229+ runtime_env = {
230+ "worker_process_setup_hook" : setup_sentry_with_logging_transport ,
231+ "working_dir" : "./" ,
232+ },
233+ _temp_dir = ray_temp_dir ,
234+ )
235+
236+ # Setup ray actor
237+ @ray .remote
238+ class Counter :
239+ def __init__ (self ):
240+ self .n = 0
241+
242+ def increment (self ):
243+ with sentry_sdk .start_span (op = "task" , name = "example actor execution" ):
244+ 1 / 0
245+
246+ return sentry_sdk .get_client ().transport .envelopes
247+
248+ with sentry_sdk .start_transaction (op = "task" , name = "ray test transaction" ):
249+ with pytest .raises (ZeroDivisionError ):
250+ counter = Counter .remote ()
251+ future = counter .increment .remote ()
252+ ray .get (future )
253+
254+ job_id = future .job_id ().hex ()
255+ error = read_error_from_log (job_id , ray_temp_dir )
256+
257+ # We do not capture errors in ray actors yet
258+ assert error is None
259+
260+ finally :
261+ # Clean up the temporary directory
262+ if os .path .exists (ray_temp_dir ):
263+ shutil .rmtree (ray_temp_dir , ignore_errors = True )
0 commit comments