2025-12-01
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import inspect
|
||||
import functools
|
||||
import sys
|
||||
|
||||
import sentry_sdk
|
||||
from sentry_sdk.consts import OP, SPANSTATUS
|
||||
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
|
||||
from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK
|
||||
from sentry_sdk.tracing import TransactionSource
|
||||
from sentry_sdk.utils import (
|
||||
event_from_exception,
|
||||
logger,
|
||||
@@ -17,7 +18,6 @@ try:
|
||||
import ray # type: ignore[import-not-found]
|
||||
except ImportError:
|
||||
raise DidNotEnable("Ray not installed.")
|
||||
import functools
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
@@ -42,73 +42,97 @@ def _patch_ray_remote():
|
||||
old_remote = ray.remote
|
||||
|
||||
@functools.wraps(old_remote)
|
||||
def new_remote(f, *args, **kwargs):
|
||||
# type: (Callable[..., Any], *Any, **Any) -> Callable[..., Any]
|
||||
def new_remote(f=None, *args, **kwargs):
|
||||
# type: (Optional[Callable[..., Any]], *Any, **Any) -> Callable[..., Any]
|
||||
|
||||
if inspect.isclass(f):
|
||||
# Ray Actors
|
||||
# (https://docs.ray.io/en/latest/ray-core/actors.html)
|
||||
# are not supported
|
||||
# (Only Ray Tasks are supported)
|
||||
return old_remote(f, *args, *kwargs)
|
||||
return old_remote(f, *args, **kwargs)
|
||||
|
||||
def _f(*f_args, _tracing=None, **f_kwargs):
|
||||
# type: (Any, Optional[dict[str, Any]], Any) -> Any
|
||||
"""
|
||||
Ray Worker
|
||||
"""
|
||||
_check_sentry_initialized()
|
||||
def wrapper(user_f):
|
||||
# type: (Callable[..., Any]) -> Any
|
||||
@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 {},
|
||||
op=OP.QUEUE_TASK_RAY,
|
||||
name=qualname_from_function(f),
|
||||
origin=RayIntegration.origin,
|
||||
source=TRANSACTION_SOURCE_TASK,
|
||||
transaction = sentry_sdk.continue_trace(
|
||||
_sentry_tracing or {},
|
||||
op=OP.QUEUE_TASK_RAY,
|
||||
name=qualname_from_function(user_f),
|
||||
origin=RayIntegration.origin,
|
||||
source=TransactionSource.TASK,
|
||||
)
|
||||
|
||||
with sentry_sdk.start_transaction(transaction) as transaction:
|
||||
try:
|
||||
result = user_f(*f_args, **f_kwargs)
|
||||
transaction.set_status(SPANSTATUS.OK)
|
||||
except Exception:
|
||||
transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
|
||||
exc_info = sys.exc_info()
|
||||
_capture_exception(exc_info)
|
||||
reraise(*exc_info)
|
||||
|
||||
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]
|
||||
|
||||
with sentry_sdk.start_transaction(transaction) as transaction:
|
||||
try:
|
||||
result = f(*f_args, **f_kwargs)
|
||||
transaction.set_status(SPANSTATUS.OK)
|
||||
except Exception:
|
||||
transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
|
||||
exc_info = sys.exc_info()
|
||||
_capture_exception(exc_info)
|
||||
reraise(*exc_info)
|
||||
if f:
|
||||
rv = old_remote(new_func)
|
||||
else:
|
||||
rv = old_remote(*args, **kwargs)(new_func)
|
||||
old_remote_method = rv.remote
|
||||
|
||||
return result
|
||||
def _remote_method_with_header_propagation(*args, **kwargs):
|
||||
# type: (*Any, **Any) -> Any
|
||||
"""
|
||||
Ray Client
|
||||
"""
|
||||
with sentry_sdk.start_span(
|
||||
op=OP.QUEUE_SUBMIT_RAY,
|
||||
name=qualname_from_function(user_f),
|
||||
origin=RayIntegration.origin,
|
||||
) as span:
|
||||
tracing = {
|
||||
k: v
|
||||
for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers()
|
||||
}
|
||||
try:
|
||||
result = old_remote_method(
|
||||
*args, **kwargs, _sentry_tracing=tracing
|
||||
)
|
||||
span.set_status(SPANSTATUS.OK)
|
||||
except Exception:
|
||||
span.set_status(SPANSTATUS.INTERNAL_ERROR)
|
||||
exc_info = sys.exc_info()
|
||||
_capture_exception(exc_info)
|
||||
reraise(*exc_info)
|
||||
|
||||
rv = old_remote(_f, *args, *kwargs)
|
||||
old_remote_method = rv.remote
|
||||
return result
|
||||
|
||||
def _remote_method_with_header_propagation(*args, **kwargs):
|
||||
# type: (*Any, **Any) -> Any
|
||||
"""
|
||||
Ray Client
|
||||
"""
|
||||
with sentry_sdk.start_span(
|
||||
op=OP.QUEUE_SUBMIT_RAY,
|
||||
name=qualname_from_function(f),
|
||||
origin=RayIntegration.origin,
|
||||
) as span:
|
||||
tracing = {
|
||||
k: v
|
||||
for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers()
|
||||
}
|
||||
try:
|
||||
result = old_remote_method(*args, **kwargs, _tracing=tracing)
|
||||
span.set_status(SPANSTATUS.OK)
|
||||
except Exception:
|
||||
span.set_status(SPANSTATUS.INTERNAL_ERROR)
|
||||
exc_info = sys.exc_info()
|
||||
_capture_exception(exc_info)
|
||||
reraise(*exc_info)
|
||||
rv.remote = _remote_method_with_header_propagation
|
||||
|
||||
return result
|
||||
return rv
|
||||
|
||||
rv.remote = _remote_method_with_header_propagation
|
||||
|
||||
return rv
|
||||
if f is not None:
|
||||
return wrapper(f)
|
||||
else:
|
||||
return wrapper
|
||||
|
||||
ray.remote = new_remote
|
||||
|
||||
|
||||
Reference in New Issue
Block a user