diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index 2039a00b35..b3d96dfab3 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test AI on: push: diff --git a/.github/workflows/test-integrations-aws-lambda.yml b/.github/workflows/test-integrations-aws-lambda.yml index 119545c9f6..daab40a91d 100644 --- a/.github/workflows/test-integrations-aws-lambda.yml +++ b/.github/workflows/test-integrations-aws-lambda.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test AWS Lambda on: push: diff --git a/.github/workflows/test-integrations-cloud-computing.yml b/.github/workflows/test-integrations-cloud-computing.yml index 531303bf52..86ecab6f8e 100644 --- a/.github/workflows/test-integrations-cloud-computing.yml +++ b/.github/workflows/test-integrations-cloud-computing.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Cloud Computing on: push: diff --git a/.github/workflows/test-integrations-common.yml b/.github/workflows/test-integrations-common.yml index a32f300512..52baefd5b1 100644 --- a/.github/workflows/test-integrations-common.yml +++ b/.github/workflows/test-integrations-common.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Common on: push: diff --git a/.github/workflows/test-integrations-data-processing.yml b/.github/workflows/test-integrations-data-processing.yml index 1585adb20e..97fd913c44 100644 --- a/.github/workflows/test-integrations-data-processing.yml +++ b/.github/workflows/test-integrations-data-processing.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Data Processing on: push: @@ -57,10 +59,18 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh "py${{ matrix.python-version }}-celery-latest" + - name: Test dramatiq latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-dramatiq-latest" - name: Test huey latest run: | set -x # print commands that are executed ./scripts/runtox.sh "py${{ matrix.python-version }}-huey-latest" + - name: Test ray latest + run: | + set -x # print commands that are executed + ./scripts/runtox.sh "py${{ matrix.python-version }}-ray-latest" - name: Test rq latest run: | set -x # print commands that are executed @@ -125,10 +135,18 @@ jobs: run: | set -x # print commands that are executed ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-celery" + - name: Test dramatiq pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-dramatiq" - name: Test huey pinned run: | set -x # print commands that are executed ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-huey" + - name: Test ray pinned + run: | + set -x # print commands that are executed + ./scripts/runtox.sh --exclude-latest "py${{ matrix.python-version }}-ray" - name: Test rq pinned run: | set -x # print commands that are executed diff --git a/.github/workflows/test-integrations-databases.yml b/.github/workflows/test-integrations-databases.yml index c547e1a9da..d740912829 100644 --- a/.github/workflows/test-integrations-databases.yml +++ b/.github/workflows/test-integrations-databases.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Databases on: push: diff --git a/.github/workflows/test-integrations-graphql.yml b/.github/workflows/test-integrations-graphql.yml index d5f78aaa89..6a499fa355 100644 --- a/.github/workflows/test-integrations-graphql.yml +++ b/.github/workflows/test-integrations-graphql.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test GraphQL on: push: diff --git a/.github/workflows/test-integrations-miscellaneous.yml b/.github/workflows/test-integrations-miscellaneous.yml index 71ee0a2f1c..f5148fb2c8 100644 --- a/.github/workflows/test-integrations-miscellaneous.yml +++ b/.github/workflows/test-integrations-miscellaneous.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Miscellaneous on: push: diff --git a/.github/workflows/test-integrations-networking.yml b/.github/workflows/test-integrations-networking.yml index 295f6bcffc..6a55ffadd8 100644 --- a/.github/workflows/test-integrations-networking.yml +++ b/.github/workflows/test-integrations-networking.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Networking on: push: diff --git a/.github/workflows/test-integrations-web-frameworks-1.yml b/.github/workflows/test-integrations-web-frameworks-1.yml index 835dd724b3..246248a700 100644 --- a/.github/workflows/test-integrations-web-frameworks-1.yml +++ b/.github/workflows/test-integrations-web-frameworks-1.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Web Frameworks 1 on: push: diff --git a/.github/workflows/test-integrations-web-frameworks-2.yml b/.github/workflows/test-integrations-web-frameworks-2.yml index c56451b751..cfc03a935a 100644 --- a/.github/workflows/test-integrations-web-frameworks-2.yml +++ b/.github/workflows/test-integrations-web-frameworks-2.yml @@ -1,3 +1,5 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py name: Test Web Frameworks 2 on: push: diff --git a/scripts/split-tox-gh-actions/split-tox-gh-actions.py b/scripts/split-tox-gh-actions/split-tox-gh-actions.py index b9f978d850..7ed2505f40 100755 --- a/scripts/split-tox-gh-actions/split-tox-gh-actions.py +++ b/scripts/split-tox-gh-actions/split-tox-gh-actions.py @@ -80,7 +80,9 @@ "arq", "beam", "celery", + "dramatiq", "huey", + "ray", "rq", "spark", ], diff --git a/scripts/split-tox-gh-actions/templates/base.jinja b/scripts/split-tox-gh-actions/templates/base.jinja index 0a27bb0b8d..23f051de42 100644 --- a/scripts/split-tox-gh-actions/templates/base.jinja +++ b/scripts/split-tox-gh-actions/templates/base.jinja @@ -1,3 +1,6 @@ +# Do not edit this file. This file is generated automatically by executing +# python scripts/split-tox-gh-actions/split-tox-gh-actions.py + {% with lowercase_group=group | replace(" ", "_") | lower %} name: Test {{ group }} diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index 6698ee527d..8a3cd715f1 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -5,12 +5,12 @@ from collections.abc import Mapping from datetime import datetime, timezone from importlib import import_module +from typing import cast from sentry_sdk._compat import PY37, check_uwsgi_thread_support from sentry_sdk.utils import ( capture_internal_exceptions, current_stacktrace, - disable_capture_event, format_timestamp, get_sdk_name, get_type_name, @@ -525,10 +525,14 @@ def _prepare_event( # Postprocess the event here so that annotated types do # generally not surface in before_send if event is not None: - event = serialize( - event, - max_request_body_size=self.options.get("max_request_body_size"), - max_value_length=self.options.get("max_value_length"), + event = cast( + "Event", + serialize( + cast("Dict[str, Any]", event), + max_request_body_size=self.options.get("max_request_body_size"), + max_value_length=self.options.get("max_value_length"), + custom_repr=self.options.get("custom_repr"), + ), ) before_send = self.options["before_send"] @@ -726,9 +730,6 @@ def capture_event( :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help. """ - if disable_capture_event.get(False): - return None - if hint is None: hint = {} event_id = event.get("event_id") diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index b50a2843a6..167c503b00 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -465,6 +465,8 @@ class OP: QUEUE_TASK_RQ = "queue.task.rq" QUEUE_SUBMIT_HUEY = "queue.submit.huey" QUEUE_TASK_HUEY = "queue.task.huey" + QUEUE_SUBMIT_RAY = "queue.submit.ray" + QUEUE_TASK_RAY = "queue.task.ray" SUBPROCESS = "subprocess" SUBPROCESS_WAIT = "subprocess.wait" SUBPROCESS_COMMUNICATE = "subprocess.communicate" @@ -539,6 +541,7 @@ def __init__( spotlight=None, # type: Optional[Union[bool, str]] cert_file=None, # type: Optional[str] key_file=None, # type: Optional[str] + custom_repr=None, # type: Optional[Callable[..., Optional[str]]] ): # type: (...) -> None pass diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 6da340f31c..f10b5079a7 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -6,7 +6,7 @@ from sentry_sdk.consts import OP, SPANSTATUS, SPANDATA from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.logging import ignore_logger -from sentry_sdk.sessions import auto_session_tracking_scope +from sentry_sdk.sessions import track_session from sentry_sdk.integrations._wsgi_common import ( _filter_headers, request_body_within_bounds, @@ -105,7 +105,7 @@ async def sentry_app_handle(self, request, *args, **kwargs): weak_request = weakref.ref(request) with sentry_sdk.isolation_scope() as scope: - with auto_session_tracking_scope(scope, session_mode="request"): + with track_session(scope, session_mode="request"): # Scope data will not leak between requests because aiohttp # create a task to wrap each request. scope.generate_propagation_context() diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index c0553cb474..b952da021d 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -19,7 +19,7 @@ _get_request_data, _get_url, ) -from sentry_sdk.sessions import auto_session_tracking_scope +from sentry_sdk.sessions import track_session from sentry_sdk.tracing import ( SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE, @@ -169,7 +169,7 @@ async def _run_app(self, scope, receive, send, asgi_version): _asgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as sentry_scope: - with auto_session_tracking_scope(sentry_scope, session_mode="request"): + with track_session(sentry_scope, session_mode="request"): sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" processor = partial(self.event_processor, asgi_scope=scope) diff --git a/sentry_sdk/integrations/dramatiq.py b/sentry_sdk/integrations/dramatiq.py new file mode 100644 index 0000000000..673c3323e8 --- /dev/null +++ b/sentry_sdk/integrations/dramatiq.py @@ -0,0 +1,167 @@ +import json + +import sentry_sdk +from sentry_sdk.integrations import Integration +from sentry_sdk._types import TYPE_CHECKING +from sentry_sdk.integrations._wsgi_common import request_body_within_bounds +from sentry_sdk.utils import ( + AnnotatedValue, + capture_internal_exceptions, + event_from_exception, +) + +from dramatiq.broker import Broker # type: ignore +from dramatiq.message import Message # type: ignore +from dramatiq.middleware import Middleware, default_middleware # type: ignore +from dramatiq.errors import Retry # type: ignore + +if TYPE_CHECKING: + from typing import Any, Callable, Dict, Optional, Union + from sentry_sdk._types import Event, Hint + + +class DramatiqIntegration(Integration): + """ + Dramatiq integration for Sentry + + Please make sure that you call `sentry_sdk.init` *before* initializing + your broker, as it monkey patches `Broker.__init__`. + + This integration was originally developed and maintained + by https://github.com/jacobsvante and later donated to the Sentry + project. + """ + + identifier = "dramatiq" + + @staticmethod + def setup_once(): + # type: () -> None + _patch_dramatiq_broker() + + +def _patch_dramatiq_broker(): + # type: () -> None + original_broker__init__ = Broker.__init__ + + def sentry_patched_broker__init__(self, *args, **kw): + # type: (Broker, *Any, **Any) -> None + integration = sentry_sdk.get_client().get_integration(DramatiqIntegration) + + try: + middleware = kw.pop("middleware") + except KeyError: + # Unfortunately Broker and StubBroker allows middleware to be + # passed in as positional arguments, whilst RabbitmqBroker and + # RedisBroker does not. + if len(args) == 1: + middleware = args[0] + args = [] # type: ignore + else: + middleware = None + + if middleware is None: + middleware = list(m() for m in default_middleware) + else: + middleware = list(middleware) + + if integration is not None: + middleware = [m for m in middleware if not isinstance(m, SentryMiddleware)] + middleware.insert(0, SentryMiddleware()) + + kw["middleware"] = middleware + original_broker__init__(self, *args, **kw) + + Broker.__init__ = sentry_patched_broker__init__ + + +class SentryMiddleware(Middleware): # type: ignore[misc] + """ + A Dramatiq middleware that automatically captures and sends + exceptions to Sentry. + + This is automatically added to every instantiated broker via the + DramatiqIntegration. + """ + + def before_process_message(self, broker, message): + # type: (Broker, Message) -> None + integration = sentry_sdk.get_client().get_integration(DramatiqIntegration) + if integration is None: + return + + message._scope_manager = sentry_sdk.new_scope() + message._scope_manager.__enter__() + + scope = sentry_sdk.get_current_scope() + scope.transaction = message.actor_name + scope.set_extra("dramatiq_message_id", message.message_id) + scope.add_event_processor(_make_message_event_processor(message, integration)) + + def after_process_message(self, broker, message, *, result=None, exception=None): + # type: (Broker, Message, Any, Optional[Any], Optional[Exception]) -> None + integration = sentry_sdk.get_client().get_integration(DramatiqIntegration) + if integration is None: + return + + actor = broker.get_actor(message.actor_name) + throws = message.options.get("throws") or actor.options.get("throws") + + try: + if ( + exception is not None + and not (throws and isinstance(exception, throws)) + and not isinstance(exception, Retry) + ): + event, hint = event_from_exception( + exception, + client_options=sentry_sdk.get_client().options, + mechanism={ + "type": DramatiqIntegration.identifier, + "handled": False, + }, + ) + sentry_sdk.capture_event(event, hint=hint) + finally: + message._scope_manager.__exit__(None, None, None) + + +def _make_message_event_processor(message, integration): + # type: (Message, DramatiqIntegration) -> Callable[[Event, Hint], Optional[Event]] + + def inner(event, hint): + # type: (Event, Hint) -> Optional[Event] + with capture_internal_exceptions(): + DramatiqMessageExtractor(message).extract_into_event(event) + + return event + + return inner + + +class DramatiqMessageExtractor(object): + def __init__(self, message): + # type: (Message) -> None + self.message_data = dict(message.asdict()) + + def content_length(self): + # type: () -> int + return len(json.dumps(self.message_data)) + + def extract_into_event(self, event): + # type: (Event) -> None + client = sentry_sdk.get_client() + if not client.is_active(): + return + + contexts = event.setdefault("contexts", {}) + request_info = contexts.setdefault("dramatiq", {}) + request_info["type"] = "dramatiq" + + data = None # type: Optional[Union[AnnotatedValue, Dict[str, Any]]] + if not request_body_within_bounds(client, self.content_length()): + data = AnnotatedValue.removed_because_over_size_limit() + else: + data = self.message_data + + request_info["data"] = data diff --git a/sentry_sdk/integrations/pure_eval.py b/sentry_sdk/integrations/pure_eval.py index 9af4831b32..d5325be384 100644 --- a/sentry_sdk/integrations/pure_eval.py +++ b/sentry_sdk/integrations/pure_eval.py @@ -131,7 +131,8 @@ def start(n): atok = source.asttokens() expressions.sort(key=closeness, reverse=True) - return { + vars = { atok.get_text(nodes[0]): value for nodes, value in expressions[: serializer.MAX_DATABAG_BREADTH] } + return serializer.serialize(vars, is_vars=True) diff --git a/sentry_sdk/integrations/ray.py b/sentry_sdk/integrations/ray.py new file mode 100644 index 0000000000..bafd42c8d6 --- /dev/null +++ b/sentry_sdk/integrations/ray.py @@ -0,0 +1,146 @@ +import inspect +import sys + +import sentry_sdk +from sentry_sdk.consts import OP, SPANSTATUS +from sentry_sdk.integrations import DidNotEnable, Integration +from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK +from sentry_sdk.utils import ( + event_from_exception, + logger, + package_version, + qualname_from_function, + reraise, +) + +try: + import ray # type: ignore[import-not-found] +except ImportError: + raise DidNotEnable("Ray not installed.") +import functools + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from collections.abc import Callable + from typing import Any, Optional + from sentry_sdk.utils import ExcInfo + + +def _check_sentry_initialized(): + # type: () -> None + if sentry_sdk.get_client().is_active(): + return + + logger.debug( + "[Tracing] Sentry not initialized in ray cluster worker, performance data will be discarded." + ) + + +def _patch_ray_remote(): + # type: () -> None + old_remote = ray.remote + + @functools.wraps(old_remote) + def new_remote(f, *args, **kwargs): + # type: (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) + + def _f(*f_args, _tracing=None, **f_kwargs): + # type: (Any, Optional[dict[str, Any]], Any) -> Any + """ + Ray Worker + """ + _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, + ) + + 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) + + return result + + rv = old_remote(_f, *args, *kwargs) + old_remote_method = rv.remote + + def _remote_method_with_header_propagation(*args, **kwargs): + # type: (*Any, **Any) -> Any + """ + Ray Client + """ + with sentry_sdk.start_span( + op=OP.QUEUE_SUBMIT_RAY, + description=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) + + return result + + rv.remote = _remote_method_with_header_propagation + + return rv + + ray.remote = new_remote + + +def _capture_exception(exc_info, **kwargs): + # type: (ExcInfo, **Any) -> None + client = sentry_sdk.get_client() + + event, hint = event_from_exception( + exc_info, + client_options=client.options, + mechanism={ + "handled": False, + "type": RayIntegration.identifier, + }, + ) + sentry_sdk.capture_event(event, hint=hint) + + +class RayIntegration(Integration): + identifier = "ray" + origin = f"auto.queue.{identifier}" + + @staticmethod + def setup_once(): + # type: () -> None + version = package_version("ray") + + if version is None: + raise DidNotEnable("Unparsable ray version: {}".format(version)) + + if version < (2, 7, 0): + raise DidNotEnable("Ray 2.7.0 or newer required") + + _patch_ray_remote() diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 1b5c9c7c43..7a95611d78 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -8,9 +8,7 @@ from sentry_sdk.consts import OP from sentry_sdk.scope import should_send_default_pii from sentry_sdk.integrations._wsgi_common import _filter_headers -from sentry_sdk.sessions import ( - auto_session_tracking_scope as auto_session_tracking, -) # When the Hub is removed, this should be renamed (see comment in sentry_sdk/sessions.py) +from sentry_sdk.sessions import track_session from sentry_sdk.scope import use_isolation_scope from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE from sentry_sdk.utils import ( @@ -83,7 +81,7 @@ def __call__(self, environ, start_response): _wsgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as scope: - with auto_session_tracking(scope, session_mode="request"): + with track_session(scope, session_mode="request"): with capture_internal_exceptions(): scope.clear_breadcrumbs() scope._name = "wsgi" diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 4e07e818c9..69037758a2 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -31,6 +31,7 @@ capture_internal_exception, capture_internal_exceptions, ContextVar, + disable_capture_event, event_from_exception, exc_info_from_error, logger, @@ -1130,6 +1131,9 @@ def capture_event(self, event, hint=None, scope=None, **scope_kwargs): :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`). """ + if disable_capture_event.get(False): + return None + scope = self._merge_scopes(scope, scope_kwargs) event_id = self.get_client().capture_event(event=event, hint=hint, scope=scope) @@ -1157,6 +1161,9 @@ def capture_message(self, message, level=None, scope=None, **scope_kwargs): :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`). """ + if disable_capture_event.get(False): + return None + if level is None: level = "info" @@ -1182,6 +1189,9 @@ def capture_exception(self, error=None, scope=None, **scope_kwargs): :returns: An `event_id` if the SDK decided to send the event (see :py:meth:`sentry_sdk.client._Client.capture_event`). """ + if disable_capture_event.get(False): + return None + if error is not None: exc_info = exc_info_from_error(error) else: diff --git a/sentry_sdk/serializer.py b/sentry_sdk/serializer.py index ff243eeadc..7171885f43 100644 --- a/sentry_sdk/serializer.py +++ b/sentry_sdk/serializer.py @@ -25,7 +25,7 @@ from typing import Type from typing import Union - from sentry_sdk._types import NotImplementedType, Event + from sentry_sdk._types import NotImplementedType Span = Dict[str, Any] @@ -95,7 +95,26 @@ def __exit__( def serialize(event, **kwargs): - # type: (Event, **Any) -> Event + # type: (Dict[str, Any], **Any) -> Dict[str, Any] + """ + A very smart serializer that takes a dict and emits a json-friendly dict. + Currently used for serializing the final Event and also prematurely while fetching the stack + local variables for each frame in a stacktrace. + + It works internally with 'databags' which are arbitrary data structures like Mapping, Sequence and Set. + The algorithm itself is a recursive graph walk down the data structures it encounters. + + It has the following responsibilities: + * Trimming databags and keeping them within MAX_DATABAG_BREADTH and MAX_DATABAG_DEPTH. + * Calling safe_repr() on objects appropriately to keep them informative and readable in the final payload. + * Annotating the payload with the _meta field whenever trimming happens. + + :param max_request_body_size: If set to "always", will never trim request bodies. + :param max_value_length: The max length to strip strings to, defaults to sentry_sdk.consts.DEFAULT_MAX_VALUE_LENGTH + :param is_vars: If we're serializing vars early, we want to repr() things that are JSON-serializable to make their type more apparent. For example, it's useful to see the difference between a unicode-string and a bytestring when viewing a stacktrace. + :param custom_repr: A custom repr function that runs before safe_repr on the object to be serialized. If it returns None or throws internally, we will fallback to safe_repr. + + """ memo = Memo() path = [] # type: List[Segment] meta_stack = [] # type: List[Dict[str, Any]] @@ -104,6 +123,18 @@ def serialize(event, **kwargs): kwargs.pop("max_request_body_size", None) == "always" ) # type: bool max_value_length = kwargs.pop("max_value_length", None) # type: Optional[int] + is_vars = kwargs.pop("is_vars", False) + custom_repr = kwargs.pop("custom_repr", None) # type: Callable[..., Optional[str]] + + def _safe_repr_wrapper(value): + # type: (Any) -> str + try: + repr_value = None + if custom_repr is not None: + repr_value = custom_repr(value) + return repr_value or safe_repr(value) + except Exception: + return safe_repr(value) def _annotate(**meta): # type: (**Any) -> None @@ -118,56 +149,17 @@ def _annotate(**meta): meta_stack[-1].setdefault("", {}).update(meta) - def _should_repr_strings(): - # type: () -> Optional[bool] - """ - By default non-serializable objects are going through - safe_repr(). For certain places in the event (local vars) we - want to repr() even things that are JSON-serializable to - make their type more apparent. For example, it's useful to - see the difference between a unicode-string and a bytestring - when viewing a stacktrace. - - For container-types we still don't do anything different. - Generally we just try to make the Sentry UI present exactly - what a pretty-printed repr would look like. - - :returns: `True` if we are somewhere in frame variables, and `False` if - we are in a position where we will never encounter frame variables - when recursing (for example, we're in `event.extra`). `None` if we - are not (yet) in frame variables, but might encounter them when - recursing (e.g. we're in `event.exception`) - """ - try: - p0 = path[0] - if p0 == "stacktrace" and path[1] == "frames" and path[3] == "vars": - return True - - if ( - p0 in ("threads", "exception") - and path[1] == "values" - and path[3] == "stacktrace" - and path[4] == "frames" - and path[6] == "vars" - ): - return True - except IndexError: - return None - - return False - def _is_databag(): # type: () -> Optional[bool] """ A databag is any value that we need to trim. + True for stuff like vars, request bodies, breadcrumbs and extra. - :returns: Works like `_should_repr_strings()`. `True` for "yes", - `False` for :"no", `None` for "maybe soon". + :returns: `True` for "yes", `False` for :"no", `None` for "maybe soon". """ try: - rv = _should_repr_strings() - if rv in (True, None): - return rv + if is_vars: + return True is_request_body = _is_request_body() if is_request_body in (True, None): @@ -253,7 +245,7 @@ def _serialize_node_impl( if isinstance(obj, AnnotatedValue): should_repr_strings = False if should_repr_strings is None: - should_repr_strings = _should_repr_strings() + should_repr_strings = is_vars if is_databag is None: is_databag = _is_databag() @@ -277,7 +269,7 @@ def _serialize_node_impl( _annotate(rem=[["!limit", "x"]]) if is_databag: return _flatten_annotated( - strip_string(safe_repr(obj), max_length=max_value_length) + strip_string(_safe_repr_wrapper(obj), max_length=max_value_length) ) return None @@ -294,7 +286,7 @@ def _serialize_node_impl( if should_repr_strings or ( isinstance(obj, float) and (math.isinf(obj) or math.isnan(obj)) ): - return safe_repr(obj) + return _safe_repr_wrapper(obj) else: return obj @@ -305,7 +297,7 @@ def _serialize_node_impl( return ( str(format_timestamp(obj)) if not should_repr_strings - else safe_repr(obj) + else _safe_repr_wrapper(obj) ) elif isinstance(obj, Mapping): @@ -365,13 +357,13 @@ def _serialize_node_impl( return rv_list if should_repr_strings: - obj = safe_repr(obj) + obj = _safe_repr_wrapper(obj) else: if isinstance(obj, bytes) or isinstance(obj, bytearray): obj = obj.decode("utf-8", "replace") if not isinstance(obj, str): - obj = safe_repr(obj) + obj = _safe_repr_wrapper(obj) is_span_description = ( len(path) == 3 and path[0] == "spans" and path[-1] == "description" @@ -387,7 +379,7 @@ def _serialize_node_impl( disable_capture_event.set(True) try: serialized_event = _serialize_node(event, **kwargs) - if meta_stack and isinstance(serialized_event, dict): + if not is_vars and meta_stack and isinstance(serialized_event, dict): serialized_event["_meta"] = meta_stack[0] return serialized_event diff --git a/sentry_sdk/sessions.py b/sentry_sdk/sessions.py index b14bc43187..66bbdfd5ec 100644 --- a/sentry_sdk/sessions.py +++ b/sentry_sdk/sessions.py @@ -1,5 +1,6 @@ import os import time +import warnings from threading import Thread, Lock from contextlib import contextmanager @@ -21,8 +22,15 @@ def is_auto_session_tracking_enabled(hub=None): # type: (Optional[sentry_sdk.Hub]) -> Union[Any, bool, None] - """Utility function to find out if session tracking is enabled.""" - # TODO: add deprecation warning + """DEPRECATED: Utility function to find out if session tracking is enabled.""" + + # Internal callers should use private _is_auto_session_tracking_enabled, instead. + warnings.warn( + "This function is deprecated and will be removed in the next major release. " + "There is no public API replacement.", + DeprecationWarning, + stacklevel=2, + ) if hub is None: hub = sentry_sdk.Hub.current @@ -39,12 +47,21 @@ def is_auto_session_tracking_enabled(hub=None): @contextmanager def auto_session_tracking(hub=None, session_mode="application"): # type: (Optional[sentry_sdk.Hub], str) -> Generator[None, None, None] - """Starts and stops a session automatically around a block.""" - # TODO: add deprecation warning + """DEPRECATED: Use track_session instead + Starts and stops a session automatically around a block. + """ + warnings.warn( + "This function is deprecated and will be removed in the next major release. " + "Use track_session instead.", + DeprecationWarning, + stacklevel=2, + ) if hub is None: hub = sentry_sdk.Hub.current - should_track = is_auto_session_tracking_enabled(hub) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + should_track = is_auto_session_tracking_enabled(hub) if should_track: hub.start_session(session_mode=session_mode) try: @@ -57,12 +74,26 @@ def auto_session_tracking(hub=None, session_mode="application"): def is_auto_session_tracking_enabled_scope(scope): # type: (sentry_sdk.Scope) -> bool """ - Utility function to find out if session tracking is enabled. + DEPRECATED: Utility function to find out if session tracking is enabled. + """ + + warnings.warn( + "This function is deprecated and will be removed in the next major release. " + "There is no public API replacement.", + DeprecationWarning, + stacklevel=2, + ) - TODO: This uses the new scopes. When the Hub is removed, the function - is_auto_session_tracking_enabled should be removed and this function - should be renamed to is_auto_session_tracking_enabled. + # Internal callers should use private _is_auto_session_tracking_enabled, instead. + return _is_auto_session_tracking_enabled(scope) + + +def _is_auto_session_tracking_enabled(scope): + # type: (sentry_sdk.Scope) -> bool + """ + Utility function to find out if session tracking is enabled. """ + should_track = scope._force_auto_session_tracking if should_track is None: client_options = sentry_sdk.get_client().options @@ -74,14 +105,29 @@ def is_auto_session_tracking_enabled_scope(scope): @contextmanager def auto_session_tracking_scope(scope, session_mode="application"): # type: (sentry_sdk.Scope, str) -> Generator[None, None, None] - """ + """DEPRECATED: This function is a deprecated alias for track_session. Starts and stops a session automatically around a block. + """ + + warnings.warn( + "This function is a deprecated alias for track_session and will be removed in the next major release.", + DeprecationWarning, + stacklevel=2, + ) + + with track_session(scope, session_mode=session_mode): + yield - TODO: This uses the new scopes. When the Hub is removed, the function - auto_session_tracking should be removed and this function - should be renamed to auto_session_tracking. + +@contextmanager +def track_session(scope, session_mode="application"): + # type: (sentry_sdk.Scope, str) -> Generator[None, None, None] """ - should_track = is_auto_session_tracking_enabled_scope(scope) + Start a new session in the provided scope, assuming session tracking is enabled. + This is a no-op context manager if session tracking is not enabled. + """ + + should_track = _is_auto_session_tracking_enabled(scope) if should_track: scope.start_session(session_mode=session_mode) try: diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index 08d2768cde..d731fa2254 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -585,8 +585,9 @@ def serialize_frame( include_local_variables=True, include_source_context=True, max_value_length=None, + custom_repr=None, ): - # type: (FrameType, Optional[int], bool, bool, Optional[int]) -> Dict[str, Any] + # type: (FrameType, Optional[int], bool, bool, Optional[int], Optional[Callable[..., Optional[str]]]) -> Dict[str, Any] f_code = getattr(frame, "f_code", None) if not f_code: abs_path = None @@ -616,7 +617,11 @@ def serialize_frame( ) if include_local_variables: - rv["vars"] = frame.f_locals.copy() + from sentry_sdk.serializer import serialize + + rv["vars"] = serialize( + dict(frame.f_locals), is_vars=True, custom_repr=custom_repr + ) return rv @@ -721,10 +726,12 @@ def single_exception_from_error_tuple( include_local_variables = True include_source_context = True max_value_length = DEFAULT_MAX_VALUE_LENGTH # fallback + custom_repr = None else: include_local_variables = client_options["include_local_variables"] include_source_context = client_options["include_source_context"] max_value_length = client_options["max_value_length"] + custom_repr = client_options.get("custom_repr") frames = [ serialize_frame( @@ -733,6 +740,7 @@ def single_exception_from_error_tuple( include_local_variables=include_local_variables, include_source_context=include_source_context, max_value_length=max_value_length, + custom_repr=custom_repr, ) for tb in iter_stacks(tb) ] diff --git a/tests/integrations/dramatiq/__init__.py b/tests/integrations/dramatiq/__init__.py new file mode 100644 index 0000000000..70bbf21db4 --- /dev/null +++ b/tests/integrations/dramatiq/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("dramatiq") diff --git a/tests/integrations/dramatiq/test_dramatiq.py b/tests/integrations/dramatiq/test_dramatiq.py new file mode 100644 index 0000000000..d7917cbd00 --- /dev/null +++ b/tests/integrations/dramatiq/test_dramatiq.py @@ -0,0 +1,231 @@ +import pytest +import uuid + +import dramatiq +from dramatiq.brokers.stub import StubBroker + +import sentry_sdk +from sentry_sdk.integrations.dramatiq import DramatiqIntegration + + +@pytest.fixture +def broker(sentry_init): + sentry_init(integrations=[DramatiqIntegration()]) + broker = StubBroker() + broker.emit_after("process_boot") + dramatiq.set_broker(broker) + yield broker + broker.flush_all() + broker.close() + + +@pytest.fixture +def worker(broker): + worker = dramatiq.Worker(broker, worker_timeout=100, worker_threads=1) + worker.start() + yield worker + worker.stop() + + +def test_that_a_single_error_is_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + return x / y + + dummy_actor.send(1, 2) + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + exception = event["exception"]["values"][0] + assert exception["type"] == "ZeroDivisionError" + + +def test_that_actor_name_is_set_as_transaction(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + return x / y + + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + assert event["transaction"] == "dummy_actor" + + +def test_that_dramatiq_message_id_is_set_as_extra(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + sentry_sdk.capture_message("hi") + return x / y + + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + event_message, event_error = events + assert "dramatiq_message_id" in event_message["extra"] + assert "dramatiq_message_id" in event_error["extra"] + assert ( + event_message["extra"]["dramatiq_message_id"] + == event_error["extra"]["dramatiq_message_id"] + ) + msg_ids = [e["extra"]["dramatiq_message_id"] for e in events] + assert all(uuid.UUID(msg_id) and isinstance(msg_id, str) for msg_id in msg_ids) + + +def test_that_local_variables_are_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + foo = 42 # noqa + return x / y + + dummy_actor.send(1, 2) + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + exception = event["exception"]["values"][0] + assert exception["stacktrace"]["frames"][-1]["vars"] == { + "x": "1", + "y": "0", + "foo": "42", + } + + +def test_that_messages_are_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(): + sentry_sdk.capture_message("hi") + + dummy_actor.send() + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + assert event["message"] == "hi" + assert event["level"] == "info" + assert event["transaction"] == "dummy_actor" + + +def test_that_sub_actor_errors_are_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + sub_actor.send(x, y) + + @dramatiq.actor(max_retries=0) + def sub_actor(x, y): + return x / y + + dummy_actor.send(1, 2) + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + assert event["transaction"] == "sub_actor" + + exception = event["exception"]["values"][0] + assert exception["type"] == "ZeroDivisionError" + + +def test_that_multiple_errors_are_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + return x / y + + dummy_actor.send(1, 0) + broker.join(dummy_actor.queue_name) + worker.join() + + dummy_actor.send(1, None) + broker.join(dummy_actor.queue_name) + worker.join() + + event1, event2 = events + + assert event1["transaction"] == "dummy_actor" + exception = event1["exception"]["values"][0] + assert exception["type"] == "ZeroDivisionError" + + assert event2["transaction"] == "dummy_actor" + exception = event2["exception"]["values"][0] + assert exception["type"] == "TypeError" + + +def test_that_message_data_is_added_as_request(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=0) + def dummy_actor(x, y): + return x / y + + dummy_actor.send_with_options( + args=( + 1, + 0, + ), + max_retries=0, + ) + broker.join(dummy_actor.queue_name) + worker.join() + + (event,) = events + + assert event["transaction"] == "dummy_actor" + request_data = event["contexts"]["dramatiq"]["data"] + assert request_data["queue_name"] == "default" + assert request_data["actor_name"] == "dummy_actor" + assert request_data["args"] == [1, 0] + assert request_data["kwargs"] == {} + assert request_data["options"]["max_retries"] == 0 + assert uuid.UUID(request_data["message_id"]) + assert isinstance(request_data["message_timestamp"], int) + + +def test_that_expected_exceptions_are_not_captured(broker, worker, capture_events): + events = capture_events() + + class ExpectedException(Exception): + pass + + @dramatiq.actor(max_retries=0, throws=ExpectedException) + def dummy_actor(): + raise ExpectedException + + dummy_actor.send() + broker.join(dummy_actor.queue_name) + worker.join() + + assert events == [] + + +def test_that_retry_exceptions_are_not_captured(broker, worker, capture_events): + events = capture_events() + + @dramatiq.actor(max_retries=2) + def dummy_actor(): + raise dramatiq.errors.Retry("Retrying", delay=100) + + dummy_actor.send() + broker.join(dummy_actor.queue_name) + worker.join() + + assert events == [] diff --git a/tests/integrations/ray/__init__.py b/tests/integrations/ray/__init__.py new file mode 100644 index 0000000000..92f6d93906 --- /dev/null +++ b/tests/integrations/ray/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.importorskip("ray") diff --git a/tests/integrations/ray/test_ray.py b/tests/integrations/ray/test_ray.py new file mode 100644 index 0000000000..83d8b04b67 --- /dev/null +++ b/tests/integrations/ray/test_ray.py @@ -0,0 +1,205 @@ +import json +import os +import pytest + +import ray + +import sentry_sdk +from sentry_sdk.envelope import Envelope +from sentry_sdk.integrations.ray import RayIntegration +from tests.conftest import TestTransport + + +class RayTestTransport(TestTransport): + def __init__(self): + self.envelopes = [] + super().__init__() + + def capture_envelope(self, envelope: Envelope) -> None: + self.envelopes.append(envelope) + + +class RayLoggingTransport(TestTransport): + def __init__(self): + super().__init__() + + def capture_envelope(self, envelope: Envelope) -> None: + print(envelope.serialize().decode("utf-8", "replace")) + + +def setup_sentry_with_logging_transport(): + setup_sentry(transport=RayLoggingTransport()) + + +def setup_sentry(transport=None): + sentry_sdk.init( + integrations=[RayIntegration()], + transport=RayTestTransport() if transport is None else transport, + traces_sample_rate=1.0, + ) + + +@pytest.mark.forked +def test_ray_tracing(): + setup_sentry() + + ray.init( + runtime_env={ + "worker_process_setup_hook": setup_sentry, + "working_dir": "./", + } + ) + + @ray.remote + def example_task(): + with sentry_sdk.start_span(op="task", description="example task step"): + ... + + return sentry_sdk.get_client().transport.envelopes + + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + worker_envelopes = ray.get(example_task.remote()) + + client_envelope = sentry_sdk.get_client().transport.envelopes[0] + client_transaction = client_envelope.get_transaction_event() + worker_envelope = worker_envelopes[0] + worker_transaction = worker_envelope.get_transaction_event() + + assert ( + client_transaction["contexts"]["trace"]["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + ) + + for span in client_transaction["spans"]: + assert ( + span["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + ) + + for span in worker_transaction["spans"]: + assert ( + span["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + ) + + +@pytest.mark.forked +def test_ray_spans(): + setup_sentry() + + ray.init( + runtime_env={ + "worker_process_setup_hook": setup_sentry, + "working_dir": "./", + } + ) + + @ray.remote + def example_task(): + return sentry_sdk.get_client().transport.envelopes + + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + worker_envelopes = ray.get(example_task.remote()) + + client_envelope = sentry_sdk.get_client().transport.envelopes[0] + client_transaction = client_envelope.get_transaction_event() + worker_envelope = worker_envelopes[0] + worker_transaction = worker_envelope.get_transaction_event() + + for span in client_transaction["spans"]: + assert span["op"] == "queue.submit.ray" + assert span["origin"] == "auto.queue.ray" + + for span in worker_transaction["spans"]: + assert span["op"] == "queue.task.ray" + assert span["origin"] == "auto.queue.ray" + + +@pytest.mark.forked +def test_ray_errors(): + setup_sentry_with_logging_transport() + + ray.init( + runtime_env={ + "worker_process_setup_hook": setup_sentry_with_logging_transport, + "working_dir": "./", + } + ) + + @ray.remote + def example_task(): + 1 / 0 + + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + with pytest.raises(ZeroDivisionError): + future = example_task.remote() + ray.get(future) + + job_id = future.job_id().hex() + + # Read the worker log output containing the error + log_dir = "/tmp/ray/session_latest/logs/" + log_file = [ + f + for f in os.listdir(log_dir) + if "worker" in f and job_id in f and f.endswith(".out") + ][0] + with open(os.path.join(log_dir, log_file), "r") as file: + lines = file.readlines() + # parse error object from log line + error = json.loads(lines[4][:-1]) + + assert error["level"] == "error" + assert ( + error["transaction"] + == "tests.integrations.ray.test_ray.test_ray_errors..example_task" + ) # its in the worker, not the client thus not "ray test transaction" + assert error["exception"]["values"][0]["mechanism"]["type"] == "ray" + assert not error["exception"]["values"][0]["mechanism"]["handled"] + + +@pytest.mark.forked +def test_ray_actor(): + setup_sentry() + + ray.init( + runtime_env={ + "worker_process_setup_hook": setup_sentry, + "working_dir": "./", + } + ) + + @ray.remote + class Counter(object): + def __init__(self): + self.n = 0 + + def increment(self): + with sentry_sdk.start_span(op="task", description="example task step"): + self.n += 1 + + return sentry_sdk.get_client().transport.envelopes + + with sentry_sdk.start_transaction(op="task", name="ray test transaction"): + counter = Counter.remote() + worker_envelopes = ray.get(counter.increment.remote()) + + # Currently no transactions/spans are captured in actors + assert worker_envelopes == [] + + client_envelope = sentry_sdk.get_client().transport.envelopes[0] + client_transaction = client_envelope.get_transaction_event() + + assert ( + client_transaction["contexts"]["trace"]["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + ) + + for span in client_transaction["spans"]: + assert ( + span["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + == client_transaction["contexts"]["trace"]["trace_id"] + ) diff --git a/tests/test_client.py b/tests/test_client.py index f6c2cec05c..d56bab0b1c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -944,6 +944,39 @@ def __repr__(self): assert frame["vars"]["environ"] == {"a": ""} +def test_custom_repr_on_vars(sentry_init, capture_events): + class Foo: + pass + + class Fail: + pass + + def custom_repr(value): + if isinstance(value, Foo): + return "custom repr" + elif isinstance(value, Fail): + raise ValueError("oops") + else: + return None + + sentry_init(custom_repr=custom_repr) + events = capture_events() + + try: + my_vars = {"foo": Foo(), "fail": Fail(), "normal": 42} + 1 / 0 + except ZeroDivisionError: + capture_exception() + + (event,) = events + (exception,) = event["exception"]["values"] + (frame,) = exception["stacktrace"]["frames"] + my_vars = frame["vars"]["my_vars"] + assert my_vars["foo"] == "custom repr" + assert my_vars["normal"] == "42" + assert "Fail object" in my_vars["fail"] + + @pytest.mark.parametrize( "dsn", [ diff --git a/tests/test_scrubber.py b/tests/test_scrubber.py index 2c4bd3aa90..5034121b83 100644 --- a/tests/test_scrubber.py +++ b/tests/test_scrubber.py @@ -187,3 +187,20 @@ def test_recursive_event_scrubber(sentry_init, capture_events): (event,) = events assert event["extra"]["deep"]["deeper"][0]["deepest"]["password"] == "'[Filtered]'" + + +def test_recursive_scrubber_does_not_override_original(sentry_init, capture_events): + sentry_init(event_scrubber=EventScrubber(recursive=True)) + events = capture_events() + + data = {"csrf": "secret"} + try: + raise RuntimeError("An error") + except Exception: + capture_exception() + + (event,) = events + frames = event["exception"]["values"][0]["stacktrace"]["frames"] + (frame,) = frames + assert data["csrf"] == "secret" + assert frame["vars"]["data"]["csrf"] == "[Filtered]" diff --git a/tests/test_serializer.py b/tests/test_serializer.py index a3ead112a7..2f158097bd 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -114,6 +114,31 @@ def test_custom_mapping_doesnt_mess_with_mock(extra_normalizer): assert len(m.mock_calls) == 0 +def test_custom_repr(extra_normalizer): + class Foo: + pass + + def custom_repr(value): + if isinstance(value, Foo): + return "custom" + else: + return value + + result = extra_normalizer({"foo": Foo(), "string": "abc"}, custom_repr=custom_repr) + assert result == {"foo": "custom", "string": "abc"} + + +def test_custom_repr_graceful_fallback_to_safe_repr(extra_normalizer): + class Foo: + pass + + def custom_repr(value): + raise ValueError("oops") + + result = extra_normalizer({"foo": Foo()}, custom_repr=custom_repr) + assert "Foo object" in result["foo"] + + def test_trim_databag_breadth(body_normalizer): data = { "key{}".format(i): "value{}".format(i) for i in range(MAX_DATABAG_BREADTH + 10) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index c10b9262ce..11f0314dda 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -1,7 +1,7 @@ from unittest import mock import sentry_sdk -from sentry_sdk.sessions import auto_session_tracking +from sentry_sdk.sessions import auto_session_tracking, track_session def sorted_aggregates(item): @@ -50,10 +50,51 @@ def test_aggregates(sentry_init, capture_envelopes): ) envelopes = capture_envelopes() + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + try: + scope.set_user({"id": "42"}) + raise Exception("all is wrong") + except Exception: + sentry_sdk.capture_exception() + + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass + + sentry_sdk.get_isolation_scope().start_session(session_mode="request") + sentry_sdk.get_isolation_scope().end_session() + sentry_sdk.flush() + + assert len(envelopes) == 2 + assert envelopes[0].get_event() is not None + + sess = envelopes[1] + assert len(sess.items) == 1 + sess_event = sess.items[0].payload.json + assert sess_event["attrs"] == { + "release": "fun-release", + "environment": "not-fun-env", + } + + aggregates = sorted_aggregates(sess_event) + assert len(aggregates) == 1 + assert aggregates[0]["exited"] == 2 + assert aggregates[0]["errored"] == 1 + + +def test_aggregates_deprecated( + sentry_init, capture_envelopes, suppress_deprecation_warnings +): + sentry_init( + release="fun-release", + environment="not-fun-env", + ) + envelopes = capture_envelopes() + with auto_session_tracking(session_mode="request"): with sentry_sdk.new_scope() as scope: try: - scope = sentry_sdk.get_current_scope() scope.set_user({"id": "42"}) raise Exception("all is wrong") except Exception: @@ -91,6 +132,39 @@ def test_aggregates_explicitly_disabled_session_tracking_request_mode( ) envelopes = capture_envelopes() + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + try: + raise Exception("all is wrong") + except Exception: + sentry_sdk.capture_exception() + + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass + + sentry_sdk.get_isolation_scope().start_session(session_mode="request") + sentry_sdk.get_isolation_scope().end_session() + sentry_sdk.flush() + + sess = envelopes[1] + assert len(sess.items) == 1 + sess_event = sess.items[0].payload.json + + aggregates = sorted_aggregates(sess_event) + assert len(aggregates) == 1 + assert aggregates[0]["exited"] == 1 + assert "errored" not in aggregates[0] + + +def test_aggregates_explicitly_disabled_session_tracking_request_mode_deprecated( + sentry_init, capture_envelopes, suppress_deprecation_warnings +): + sentry_init( + release="fun-release", environment="not-fun-env", auto_session_tracking=False + ) + envelopes = capture_envelopes() + with auto_session_tracking(session_mode="request"): with sentry_sdk.new_scope(): try: @@ -121,6 +195,35 @@ def test_no_thread_on_shutdown_no_errors(sentry_init): environment="not-fun-env", ) + # make it seem like the interpreter is shutting down + with mock.patch( + "threading.Thread.start", + side_effect=RuntimeError("can't create new thread at interpreter shutdown"), + ): + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + try: + raise Exception("all is wrong") + except Exception: + sentry_sdk.capture_exception() + + with sentry_sdk.isolation_scope() as scope: + with track_session(scope, session_mode="request"): + pass + + sentry_sdk.get_isolation_scope().start_session(session_mode="request") + sentry_sdk.get_isolation_scope().end_session() + sentry_sdk.flush() + + +def test_no_thread_on_shutdown_no_errors_deprecated( + sentry_init, suppress_deprecation_warnings +): + sentry_init( + release="fun-release", + environment="not-fun-env", + ) + # make it seem like the interpreter is shutting down with mock.patch( "threading.Thread.start", diff --git a/tox.ini b/tox.ini index 3acf70bb6f..fcab3ad1ed 100644 --- a/tox.ini +++ b/tox.ini @@ -108,6 +108,12 @@ envlist = {py3.10,py3.11,py3.12}-django-v{5.0,5.1} {py3.10,py3.11,py3.12}-django-latest + # dramatiq + {py3.6,py3.9}-dramatiq-v{1.13} + {py3.7,py3.10,py3.11}-dramatiq-v{1.15} + {py3.8,py3.11,py3.12}-dramatiq-v{1.17} + {py3.8,py3.11,py3.12}-dramatiq-latest + # Falcon {py3.6,py3.7}-falcon-v{1,1.4,2} {py3.6,py3.11,py3.12}-falcon-v{3} @@ -204,6 +210,10 @@ envlist = {py3.8,py3.11,py3.12}-quart-v{0.19} {py3.8,py3.11,py3.12}-quart-latest + # Ray + {py3.10,py3.11}-ray-v{2.34} + {py3.10,py3.11}-ray-latest + # Redis {py3.6,py3.8}-redis-v{3} {py3.7,py3.8,py3.11}-redis-v{4} @@ -407,6 +417,12 @@ deps = django-v5.1: Django==5.1rc1 django-latest: Django + # dramatiq + dramatiq-v1.13: dramatiq>=1.13,<1.14 + dramatiq-v1.15: dramatiq>=1.15,<1.16 + dramatiq-v1.17: dramatiq>=1.17,<1.18 + dramatiq-latest: dramatiq + # Falcon falcon-v1.4: falcon~=1.4.0 falcon-v1: falcon~=1.0 @@ -543,6 +559,10 @@ deps = pyramid-v2.0: pyramid~=2.0.0 pyramid-latest: pyramid + # Ray + ray-v2.34: ray~=2.34.0 + ray-latest: ray + # Quart quart: quart-auth quart: pytest-asyncio @@ -683,6 +703,7 @@ setenv = cohere: TESTPATH=tests/integrations/cohere cloud_resource_context: TESTPATH=tests/integrations/cloud_resource_context django: TESTPATH=tests/integrations/django + dramatiq: TESTPATH=tests/integrations/dramatiq falcon: TESTPATH=tests/integrations/falcon fastapi: TESTPATH=tests/integrations/fastapi flask: TESTPATH=tests/integrations/flask @@ -703,6 +724,7 @@ setenv = pymongo: TESTPATH=tests/integrations/pymongo pyramid: TESTPATH=tests/integrations/pyramid quart: TESTPATH=tests/integrations/quart + ray: TESTPATH=tests/integrations/ray redis: TESTPATH=tests/integrations/redis redis_py_cluster_legacy: TESTPATH=tests/integrations/redis_py_cluster_legacy requests: TESTPATH=tests/integrations/requests