Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions sentry_sdk/integrations/ray.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import functools
import sys

import sentry_sdk
Expand All @@ -17,7 +18,6 @@
import ray # type: ignore[import-not-found]
except ImportError:
raise DidNotEnable("Ray not installed.")
import functools

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -54,12 +54,13 @@ def new_remote(f=None, *args, **kwargs):

def wrapper(user_f):
# type: (Callable[..., Any]) -> Any
def new_func(*f_args, _tracing=None, **f_kwargs):
@functools.wraps(user_f)
def new_func(*f_args, _sentry_tracing=None, **f_kwargs):
# type: (Any, Optional[dict[str, Any]], Any) -> Any
_check_sentry_initialized()

transaction = sentry_sdk.continue_trace(
_tracing or {},
_sentry_tracing or {},
op=OP.QUEUE_TASK_RAY,
name=qualname_from_function(user_f),
origin=RayIntegration.origin,
Expand All @@ -78,6 +79,19 @@ def new_func(*f_args, _tracing=None, **f_kwargs):

return result

# Patching new_func signature to add the _sentry_tracing parameter to it
# Ray later inspects the signature and finds the unexpected parameter otherwise
signature = inspect.signature(new_func)
params = list(signature.parameters.values())
params.append(
inspect.Parameter(
"_sentry_tracing",
kind=inspect.Parameter.KEYWORD_ONLY,
default=None,
)
)
new_func.__signature__ = signature.replace(parameters=params) # type: ignore[attr-defined]

if f:
rv = old_remote(new_func)
else:
Expand All @@ -99,7 +113,9 @@ def _remote_method_with_header_propagation(*args, **kwargs):
for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers()
}
try:
result = old_remote_method(*args, **kwargs, _tracing=tracing)
result = old_remote_method(
*args, **kwargs, _sentry_tracing=tracing
)
span.set_status(SPANSTATUS.OK)
except Exception:
span.set_status(SPANSTATUS.INTERNAL_ERROR)
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/ray/test_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def example_task():
else:
example_task = ray.remote(example_task)

# Function name shouldn't be overwritten by Sentry wrapper
assert example_task._function_name == "tests.integrations.ray.test_ray.example_task"

with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
worker_envelopes = ray.get(example_task.remote())

Expand Down