Skip to content

Commit 0b68160

Browse files
committed
fix(Ray): Retain the original function name when patching Ray tasks
Without "@functools.wraps" added, Ray exposes Prometheus metrics with all tasks named "new_func"
1 parent d5fb1bb commit 0b68160

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

sentry_sdk/integrations/ray.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import functools
23
import sys
34

45
import sentry_sdk
@@ -17,7 +18,6 @@
1718
import ray # type: ignore[import-not-found]
1819
except ImportError:
1920
raise DidNotEnable("Ray not installed.")
20-
import functools
2121

2222
from typing import TYPE_CHECKING
2323

@@ -54,6 +54,7 @@ def new_remote(f=None, *args, **kwargs):
5454

5555
def wrapper(user_f):
5656
# type: (Callable[..., Any]) -> Any
57+
@functools.wraps(user_f)
5758
def new_func(*f_args, _tracing=None, **f_kwargs):
5859
# type: (Any, Optional[dict[str, Any]], Any) -> Any
5960
_check_sentry_initialized()
@@ -78,6 +79,19 @@ def new_func(*f_args, _tracing=None, **f_kwargs):
7879

7980
return result
8081

82+
# Patching new_func signature to add the _tracing parameter to it
83+
# Ray later inspects the signature and finds the unexpected parameter otherwise
84+
signature = inspect.signature(new_func)
85+
params = list(signature.parameters.values())
86+
params.append(
87+
inspect.Parameter(
88+
"_tracing",
89+
kind=inspect.Parameter.KEYWORD_ONLY,
90+
default=None,
91+
)
92+
)
93+
new_func.__signature__ = signature.replace(parameters=params)
94+
8195
if f:
8296
rv = old_remote(new_func)
8397
else:

0 commit comments

Comments
 (0)