From c2dfbcc3c3de1c32de516ec4268a602cb42e0694 Mon Sep 17 00:00:00 2001 From: saber solooki Date: Wed, 6 Nov 2024 18:10:35 +0100 Subject: [PATCH 01/22] Fix(Arq): fix integration with Worker settings as a dict (#3742) --- sentry_sdk/integrations/arq.py | 11 +++ tests/integrations/arq/test_arq.py | 113 +++++++++++++++++++++++++---- 2 files changed, 110 insertions(+), 14 deletions(-) diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index 4640204725..d568714fe2 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -198,6 +198,17 @@ def _sentry_create_worker(*args, **kwargs): # type: (*Any, **Any) -> Worker settings_cls = args[0] + if isinstance(settings_cls, dict): + if "functions" in settings_cls: + settings_cls["functions"] = [ + _get_arq_function(func) for func in settings_cls["functions"] + ] + if "cron_jobs" in settings_cls: + settings_cls["cron_jobs"] = [ + _get_arq_cron_job(cron_job) + for cron_job in settings_cls["cron_jobs"] + ] + if hasattr(settings_cls, "functions"): settings_cls.functions = [ _get_arq_function(func) for func in settings_cls.functions diff --git a/tests/integrations/arq/test_arq.py b/tests/integrations/arq/test_arq.py index cd4cad67b8..e74395e26c 100644 --- a/tests/integrations/arq/test_arq.py +++ b/tests/integrations/arq/test_arq.py @@ -83,14 +83,65 @@ class WorkerSettings: return inner +@pytest.fixture +def init_arq_with_dict_settings(sentry_init): + def inner( + cls_functions=None, + cls_cron_jobs=None, + kw_functions=None, + kw_cron_jobs=None, + allow_abort_jobs_=False, + ): + cls_functions = cls_functions or [] + cls_cron_jobs = cls_cron_jobs or [] + + kwargs = {} + if kw_functions is not None: + kwargs["functions"] = kw_functions + if kw_cron_jobs is not None: + kwargs["cron_jobs"] = kw_cron_jobs + + sentry_init( + integrations=[ArqIntegration()], + traces_sample_rate=1.0, + send_default_pii=True, + ) + + server = FakeRedis() + pool = ArqRedis(pool_or_conn=server.connection_pool) + + worker_settings = { + "functions": cls_functions, + "cron_jobs": cls_cron_jobs, + "redis_pool": pool, + "allow_abort_jobs": allow_abort_jobs_, + } + + if not worker_settings["functions"]: + del worker_settings["functions"] + if not worker_settings["cron_jobs"]: + del worker_settings["cron_jobs"] + + worker = arq.worker.create_worker(worker_settings, **kwargs) + + return pool, worker + + return inner + + @pytest.mark.asyncio -async def test_job_result(init_arq): +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) +async def test_job_result(init_arq_settings, request): async def increase(ctx, num): return num + 1 + init_fixture_method = request.getfixturevalue(init_arq_settings) + increase.__qualname__ = increase.__name__ - pool, worker = init_arq([increase]) + pool, worker = init_fixture_method([increase]) job = await pool.enqueue_job("increase", 3) @@ -105,14 +156,19 @@ async def increase(ctx, num): @pytest.mark.asyncio -async def test_job_retry(capture_events, init_arq): +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) +async def test_job_retry(capture_events, init_arq_settings, request): async def retry_job(ctx): if ctx["job_try"] < 2: raise arq.worker.Retry + init_fixture_method = request.getfixturevalue(init_arq_settings) + retry_job.__qualname__ = retry_job.__name__ - pool, worker = init_arq([retry_job]) + pool, worker = init_fixture_method([retry_job]) job = await pool.enqueue_job("retry_job") @@ -139,11 +195,18 @@ async def retry_job(ctx): "source", [("cls_functions", "cls_cron_jobs"), ("kw_functions", "kw_cron_jobs")] ) @pytest.mark.parametrize("job_fails", [True, False], ids=["error", "success"]) +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) @pytest.mark.asyncio -async def test_job_transaction(capture_events, init_arq, source, job_fails): +async def test_job_transaction( + capture_events, init_arq_settings, source, job_fails, request +): async def division(_, a, b=0): return a / b + init_fixture_method = request.getfixturevalue(init_arq_settings) + division.__qualname__ = division.__name__ cron_func = async_partial(division, a=1, b=int(not job_fails)) @@ -152,7 +215,9 @@ async def division(_, a, b=0): cron_job = cron(cron_func, minute=0, run_at_startup=True) functions_key, cron_jobs_key = source - pool, worker = init_arq(**{functions_key: [division], cron_jobs_key: [cron_job]}) + pool, worker = init_fixture_method( + **{functions_key: [division], cron_jobs_key: [cron_job]} + ) events = capture_events() @@ -213,12 +278,17 @@ async def division(_, a, b=0): @pytest.mark.parametrize("source", ["cls_functions", "kw_functions"]) +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) @pytest.mark.asyncio -async def test_enqueue_job(capture_events, init_arq, source): +async def test_enqueue_job(capture_events, init_arq_settings, source, request): async def dummy_job(_): pass - pool, _ = init_arq(**{source: [dummy_job]}) + init_fixture_method = request.getfixturevalue(init_arq_settings) + + pool, _ = init_fixture_method(**{source: [dummy_job]}) events = capture_events() @@ -236,13 +306,18 @@ async def dummy_job(_): @pytest.mark.asyncio -async def test_execute_job_without_integration(init_arq): +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) +async def test_execute_job_without_integration(init_arq_settings, request): async def dummy_job(_ctx): pass + init_fixture_method = request.getfixturevalue(init_arq_settings) + dummy_job.__qualname__ = dummy_job.__name__ - pool, worker = init_arq([dummy_job]) + pool, worker = init_fixture_method([dummy_job]) # remove the integration to trigger the edge case get_client().integrations.pop("arq") @@ -254,12 +329,17 @@ async def dummy_job(_ctx): @pytest.mark.parametrize("source", ["cls_functions", "kw_functions"]) +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) @pytest.mark.asyncio -async def test_span_origin_producer(capture_events, init_arq, source): +async def test_span_origin_producer(capture_events, init_arq_settings, source, request): async def dummy_job(_): pass - pool, _ = init_arq(**{source: [dummy_job]}) + init_fixture_method = request.getfixturevalue(init_arq_settings) + + pool, _ = init_fixture_method(**{source: [dummy_job]}) events = capture_events() @@ -272,13 +352,18 @@ async def dummy_job(_): @pytest.mark.asyncio -async def test_span_origin_consumer(capture_events, init_arq): +@pytest.mark.parametrize( + "init_arq_settings", ["init_arq", "init_arq_with_dict_settings"] +) +async def test_span_origin_consumer(capture_events, init_arq_settings, request): async def job(ctx): pass + init_fixture_method = request.getfixturevalue(init_arq_settings) + job.__qualname__ = job.__name__ - pool, worker = init_arq([job]) + pool, worker = init_fixture_method([job]) job = await pool.enqueue_job("retry_job") From 200d0cdde8eed2caa89b91db8b17baabe983d2de Mon Sep 17 00:00:00 2001 From: Guilherme Martins Crocetti <24530683+gmcrocetti@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:19:03 -0300 Subject: [PATCH 02/22] Handle parameter `stack_info` for the `LoggingIntegration` Add capability for the logging integration to use the parameter 'stack_info' (added in Python 3.2). When set to True the stack trace will be retrieved and properly handled. Fixes #2804 --- sentry_sdk/integrations/logging.py | 2 +- tests/integrations/logging/test_logging.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 5d23440ad1..b792510d6c 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -202,7 +202,7 @@ def _emit(self, record): client_options=client_options, mechanism={"type": "logging", "handled": True}, ) - elif record.exc_info and record.exc_info[0] is None: + elif (record.exc_info and record.exc_info[0] is None) or record.stack_info: event = {} hint = {} with capture_internal_exceptions(): diff --git a/tests/integrations/logging/test_logging.py b/tests/integrations/logging/test_logging.py index 02eb26a04d..8c325bc86c 100644 --- a/tests/integrations/logging/test_logging.py +++ b/tests/integrations/logging/test_logging.py @@ -77,11 +77,18 @@ def test_logging_extra_data_integer_keys(sentry_init, capture_events): assert event["extra"] == {"1": 1} -def test_logging_stack(sentry_init, capture_events): +@pytest.mark.parametrize( + "enable_stack_trace_kwarg", + ( + pytest.param({"exc_info": True}, id="exc_info"), + pytest.param({"stack_info": True}, id="stack_info"), + ), +) +def test_logging_stack_trace(sentry_init, capture_events, enable_stack_trace_kwarg): sentry_init(integrations=[LoggingIntegration()], default_integrations=False) events = capture_events() - logger.error("first", exc_info=True) + logger.error("first", **enable_stack_trace_kwarg) logger.error("second") ( From d42422674379afd90ac5039e4fbac13281178ff2 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Tue, 12 Nov 2024 09:16:11 +0100 Subject: [PATCH 03/22] ref(init): Deprecate `sentry_sdk.init` context manager (#3729) It is possible to use the return value of `sentry_sdk.init` as a context manager; however, this functionality has not been maintained for a long time, and it does not seem to be documented anywhere. So, we are deprecating this functionality, and we will remove it in the next major release. Closes #3282 --- sentry_sdk/_init_implementation.py | 21 +++++++++++++++++++++ tests/test_api.py | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/sentry_sdk/_init_implementation.py b/sentry_sdk/_init_implementation.py index 256a69ee83..eb02b3d11e 100644 --- a/sentry_sdk/_init_implementation.py +++ b/sentry_sdk/_init_implementation.py @@ -1,3 +1,5 @@ +import warnings + from typing import TYPE_CHECKING import sentry_sdk @@ -9,16 +11,35 @@ class _InitGuard: + _CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE = ( + "Using the return value of sentry_sdk.init as a context manager " + "and manually calling the __enter__ and __exit__ methods on the " + "return value are deprecated. We are no longer maintaining this " + "functionality, and we will remove it in the next major release." + ) + def __init__(self, client): # type: (sentry_sdk.Client) -> None self._client = client def __enter__(self): # type: () -> _InitGuard + warnings.warn( + self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE, + stacklevel=2, + category=DeprecationWarning, + ) + return self def __exit__(self, exc_type, exc_value, tb): # type: (Any, Any, Any) -> None + warnings.warn( + self._CONTEXT_MANAGER_DEPRECATION_WARNING_MESSAGE, + stacklevel=2, + category=DeprecationWarning, + ) + c = self._client if c is not None: c.close() diff --git a/tests/test_api.py b/tests/test_api.py index ae194af7fd..3b2a9c8fb7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,7 @@ import pytest from unittest import mock +import sentry_sdk from sentry_sdk import ( capture_exception, continue_trace, @@ -195,3 +196,19 @@ def test_push_scope_deprecation(): with pytest.warns(DeprecationWarning): with push_scope(): ... + + +def test_init_context_manager_deprecation(): + with pytest.warns(DeprecationWarning): + with sentry_sdk.init(): + ... + + +def test_init_enter_deprecation(): + with pytest.warns(DeprecationWarning): + sentry_sdk.init().__enter__() + + +def test_init_exit_deprecation(): + with pytest.warns(DeprecationWarning): + sentry_sdk.init().__exit__(None, None, None) From 417be9ffe5e2c72e459646dc7ec14399f78c015e Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Tue, 12 Nov 2024 13:28:51 +0000 Subject: [PATCH 04/22] feat(spotlight): Inject Spotlight button on Django (#3751) This patch expands the `SpotlightMiddleware` for Django and injects the Spotlight button to all HTML responses when Spotlight is enabled and running. It requires Spotlight 2.6.0 to work this way. Ref: getsentry/spotlight#543 --- sentry_sdk/spotlight.py | 159 ++++++++++++++++++++++++++++++++-------- 1 file changed, 130 insertions(+), 29 deletions(-) diff --git a/sentry_sdk/spotlight.py b/sentry_sdk/spotlight.py index e7e90f9822..806ba5a09e 100644 --- a/sentry_sdk/spotlight.py +++ b/sentry_sdk/spotlight.py @@ -5,8 +5,9 @@ import urllib.request import urllib.error import urllib3 +import sys -from itertools import chain +from itertools import chain, product from typing import TYPE_CHECKING @@ -15,11 +16,19 @@ from typing import Callable from typing import Dict from typing import Optional + from typing import Self -from sentry_sdk.utils import logger, env_to_bool, capture_internal_exceptions +from sentry_sdk.utils import ( + logger as sentry_logger, + env_to_bool, + capture_internal_exceptions, +) from sentry_sdk.envelope import Envelope +logger = logging.getLogger("spotlight") + + DEFAULT_SPOTLIGHT_URL = "http://localhost:8969/stream" DJANGO_SPOTLIGHT_MIDDLEWARE_PATH = "sentry_sdk.spotlight.SpotlightMiddleware" @@ -34,7 +43,7 @@ def __init__(self, url): def capture_envelope(self, envelope): # type: (Envelope) -> None if self.tries > 3: - logger.warning( + sentry_logger.warning( "Too many errors sending to Spotlight, stop sending events there." ) return @@ -52,50 +61,137 @@ def capture_envelope(self, envelope): req.close() except Exception as e: self.tries += 1 - logger.warning(str(e)) + sentry_logger.warning(str(e)) try: - from django.http import HttpResponseServerError + from django.utils.deprecation import MiddlewareMixin + from django.http import HttpResponseServerError, HttpResponse, HttpRequest from django.conf import settings - class SpotlightMiddleware: - def __init__(self, get_response): - # type: (Any, Callable[..., Any]) -> None - self.get_response = get_response - - def __call__(self, request): - # type: (Any, Any) -> Any - return self.get_response(request) + SPOTLIGHT_JS_ENTRY_PATH = "/assets/main.js" + SPOTLIGHT_JS_SNIPPET_PATTERN = ( + '' + ) + SPOTLIGHT_ERROR_PAGE_SNIPPET = ( + '\n' + '\n' + ) + CHARSET_PREFIX = "charset=" + BODY_TAG_NAME = "body" + BODY_CLOSE_TAG_POSSIBILITIES = tuple( + "".format("".join(chars)) + for chars in product(*zip(BODY_TAG_NAME.upper(), BODY_TAG_NAME.lower())) + ) + + class SpotlightMiddleware(MiddlewareMixin): # type: ignore[misc] + _spotlight_script = None # type: Optional[str] - def process_exception(self, _request, exception): - # type: (Any, Any, Exception) -> Optional[HttpResponseServerError] - if not settings.DEBUG: - return None + def __init__(self, get_response): + # type: (Self, Callable[..., HttpResponse]) -> None + super().__init__(get_response) import sentry_sdk.api - spotlight_client = sentry_sdk.api.get_client().spotlight + self.sentry_sdk = sentry_sdk.api + + spotlight_client = self.sentry_sdk.get_client().spotlight if spotlight_client is None: + sentry_logger.warning( + "Cannot find Spotlight client from SpotlightMiddleware, disabling the middleware." + ) return None - # Spotlight URL has a trailing `/stream` part at the end so split it off - spotlight_url = spotlight_client.url.rsplit("/", 1)[0] + self._spotlight_url = urllib.parse.urljoin(spotlight_client.url, "../") + + @property + def spotlight_script(self): + # type: (Self) -> Optional[str] + if self._spotlight_script is None: + try: + spotlight_js_url = urllib.parse.urljoin( + self._spotlight_url, SPOTLIGHT_JS_ENTRY_PATH + ) + req = urllib.request.Request( + spotlight_js_url, + method="HEAD", + ) + urllib.request.urlopen(req) + self._spotlight_script = SPOTLIGHT_JS_SNIPPET_PATTERN.format( + spotlight_js_url + ) + except urllib.error.URLError as err: + sentry_logger.debug( + "Cannot get Spotlight JS to inject at %s. SpotlightMiddleware will not be very useful.", + spotlight_js_url, + exc_info=err, + ) + + return self._spotlight_script + + def process_response(self, _request, response): + # type: (Self, HttpRequest, HttpResponse) -> Optional[HttpResponse] + content_type_header = tuple( + p.strip() + for p in response.headers.get("Content-Type", "").lower().split(";") + ) + content_type = content_type_header[0] + if len(content_type_header) > 1 and content_type_header[1].startswith( + CHARSET_PREFIX + ): + encoding = content_type_header[1][len(CHARSET_PREFIX) :] + else: + encoding = "utf-8" + + if ( + self.spotlight_script is not None + and not response.streaming + and content_type == "text/html" + ): + content_length = len(response.content) + injection = self.spotlight_script.encode(encoding) + injection_site = next( + ( + idx + for idx in ( + response.content.rfind(body_variant.encode(encoding)) + for body_variant in BODY_CLOSE_TAG_POSSIBILITIES + ) + if idx > -1 + ), + content_length, + ) + + # This approach works even when we don't have a `` tag + response.content = ( + response.content[:injection_site] + + injection + + response.content[injection_site:] + ) + + if response.has_header("Content-Length"): + response.headers["Content-Length"] = content_length + len(injection) + + return response + + def process_exception(self, _request, exception): + # type: (Self, HttpRequest, Exception) -> Optional[HttpResponseServerError] + if not settings.DEBUG: + return None try: - spotlight = urllib.request.urlopen(spotlight_url).read().decode("utf-8") + spotlight = ( + urllib.request.urlopen(self._spotlight_url).read().decode("utf-8") + ) except urllib.error.URLError: return None else: - event_id = sentry_sdk.api.capture_exception(exception) + event_id = self.sentry_sdk.capture_exception(exception) return HttpResponseServerError( spotlight.replace( "", - ( - f'' - ''.format( - event_id=event_id - ) + SPOTLIGHT_ERROR_PAGE_SNIPPET.format( + spotlight_url=self._spotlight_url, event_id=event_id ), ) ) @@ -106,6 +202,10 @@ def process_exception(self, _request, exception): def setup_spotlight(options): # type: (Dict[str, Any]) -> Optional[SpotlightClient] + _handler = logging.StreamHandler(sys.stderr) + _handler.setFormatter(logging.Formatter(" [spotlight] %(levelname)s: %(message)s")) + logger.addHandler(_handler) + logger.setLevel(logging.INFO) url = options.get("spotlight") @@ -119,6 +219,7 @@ def setup_spotlight(options): settings is not None and settings.DEBUG and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_ON_ERROR", "1")) + and env_to_bool(os.environ.get("SENTRY_SPOTLIGHT_MIDDLEWARE", "1")) ): with capture_internal_exceptions(): middleware = settings.MIDDLEWARE @@ -126,9 +227,9 @@ def setup_spotlight(options): settings.MIDDLEWARE = type(middleware)( chain(middleware, (DJANGO_SPOTLIGHT_MIDDLEWARE_PATH,)) ) - logging.info("Enabled Spotlight integration for Django") + logger.info("Enabled Spotlight integration for Django") client = SpotlightClient(url) - logging.info("Enabled Spotlight at %s", url) + logger.info("Enabled Spotlight using sidecar at %s", url) return client From c2361a32d58eb38465e41c967788cae991a4e510 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 13 Nov 2024 13:50:01 +0100 Subject: [PATCH 05/22] Fix aws lambda tests (by reducing event size) (#3770) Our AWS Lambda tests rely on outputting our events as JSON to stdout and parsing this output. AWS Lambda limits the amount of stdout it returns. So by reducing the size of the events we can fix the tests, that where broken by printing to much data to stdout so the output is truncated and can not be parsed into actual JSON structures again. --- tests/integrations/aws_lambda/test_aws.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/integrations/aws_lambda/test_aws.py b/tests/integrations/aws_lambda/test_aws.py index 75dc930da5..e229812336 100644 --- a/tests/integrations/aws_lambda/test_aws.py +++ b/tests/integrations/aws_lambda/test_aws.py @@ -98,7 +98,7 @@ def truncate_data(data): elif key == "cloudwatch logs": for cloudwatch_key in data["extra"]["cloudwatch logs"].keys(): if cloudwatch_key in ["url", "log_group", "log_stream"]: - cleaned_data["extra"].setdefault("cloudwatch logs", {})[cloudwatch_key] = data["extra"]["cloudwatch logs"][cloudwatch_key] + cleaned_data["extra"].setdefault("cloudwatch logs", {})[cloudwatch_key] = data["extra"]["cloudwatch logs"][cloudwatch_key].split("=")[0] if data.get("level") is not None: cleaned_data["level"] = data.get("level") @@ -228,7 +228,7 @@ def test_handler(event, context): assert event["extra"]["lambda"]["function_name"].startswith("test_") logs_url = event["extra"]["cloudwatch logs"]["url"] - assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region=") + assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region") assert not re.search("(=;|=$)", logs_url) assert event["extra"]["cloudwatch logs"]["log_group"].startswith( "/aws/lambda/test_" @@ -370,7 +370,7 @@ def test_handler(event, context): assert event["extra"]["lambda"]["function_name"].startswith("test_") logs_url = event["extra"]["cloudwatch logs"]["url"] - assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region=") + assert logs_url.startswith("https://console.aws.amazon.com/cloudwatch/home?region") assert not re.search("(=;|=$)", logs_url) assert event["extra"]["cloudwatch logs"]["log_group"].startswith( "/aws/lambda/test_" @@ -462,11 +462,11 @@ def test_handler(event, context): "X-Forwarded-Proto": "https" }, "httpMethod": "GET", - "path": "/path1", + "path": "/1", "queryStringParameters": { - "done": "false" + "done": "f" }, - "dog": "Maisey" + "d": "D1" }, { "headers": { @@ -474,11 +474,11 @@ def test_handler(event, context): "X-Forwarded-Proto": "http" }, "httpMethod": "POST", - "path": "/path2", + "path": "/2", "queryStringParameters": { - "done": "true" + "done": "t" }, - "dog": "Charlie" + "d": "D2" } ] """, @@ -538,9 +538,9 @@ def test_handler(event, context): request_data = { "headers": {"Host": "x1.io", "X-Forwarded-Proto": "https"}, "method": "GET", - "url": "https://x1.io/path1", + "url": "https://x1.io/1", "query_string": { - "done": "false", + "done": "f", }, } else: From 4bec4a4729b64525ef55947fd4042e0d62ef72cc Mon Sep 17 00:00:00 2001 From: matt-codecov <137832199+matt-codecov@users.noreply.github.com> Date: Wed, 13 Nov 2024 05:30:58 -0800 Subject: [PATCH 06/22] feat: introduce rust_tracing integration (#3717) Introduce a new integration that allows traces to descend into code in Rust native extensions by hooking into Rust's popular `tracing` framework. it relies on the Rust native extension using [`pyo3-python-tracing-subscriber`](https://crates.io/crates/pyo3-python-tracing-subscriber), a crate i recently published under Sentry, to expose a way for the Python SDK to hook into `tracing`. in this screenshot, the transaction was started in Python but the rest of the span tree reflects the structure and performance of a naive fibonacci generator in Rust: https://github.com/user-attachments/assets/ae2caff6-1842-45d0-a604-2f3b6305f330 --------- Co-authored-by: Anton Pirker --- sentry_sdk/integrations/rust_tracing.py | 274 +++++++++++ tests/integrations/rust_tracing/__init__.py | 0 .../rust_tracing/test_rust_tracing.py | 450 ++++++++++++++++++ 3 files changed, 724 insertions(+) create mode 100644 sentry_sdk/integrations/rust_tracing.py create mode 100644 tests/integrations/rust_tracing/__init__.py create mode 100644 tests/integrations/rust_tracing/test_rust_tracing.py diff --git a/sentry_sdk/integrations/rust_tracing.py b/sentry_sdk/integrations/rust_tracing.py new file mode 100644 index 0000000000..121bf082b8 --- /dev/null +++ b/sentry_sdk/integrations/rust_tracing.py @@ -0,0 +1,274 @@ +""" +This integration ingests tracing data from native extensions written in Rust. + +Using it requires additional setup on the Rust side to accept a +`RustTracingLayer` Python object and register it with the `tracing-subscriber` +using an adapter from the `pyo3-python-tracing-subscriber` crate. For example: +```rust +#[pyfunction] +pub fn initialize_tracing(py_impl: Bound<'_, PyAny>) { + tracing_subscriber::registry() + .with(pyo3_python_tracing_subscriber::PythonCallbackLayerBridge::new(py_impl)) + .init(); +} +``` + +Usage in Python would then look like: +``` +sentry_sdk.init( + dsn=sentry_dsn, + integrations=[ + RustTracingIntegration( + "demo_rust_extension", + demo_rust_extension.initialize_tracing, + event_type_mapping=event_type_mapping, + ) + ], +) +``` + +Each native extension requires its own integration. +""" + +import json +from enum import Enum, auto +from typing import Any, Callable, Dict, Tuple, Optional + +import sentry_sdk +from sentry_sdk.integrations import Integration +from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.tracing import Span as SentrySpan +from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE + +TraceState = Optional[Tuple[Optional[SentrySpan], SentrySpan]] + + +class RustTracingLevel(Enum): + Trace: str = "TRACE" + Debug: str = "DEBUG" + Info: str = "INFO" + Warn: str = "WARN" + Error: str = "ERROR" + + +class EventTypeMapping(Enum): + Ignore = auto() + Exc = auto() + Breadcrumb = auto() + Event = auto() + + +def tracing_level_to_sentry_level(level): + # type: (str) -> sentry_sdk._types.LogLevelStr + level = RustTracingLevel(level) + if level in (RustTracingLevel.Trace, RustTracingLevel.Debug): + return "debug" + elif level == RustTracingLevel.Info: + return "info" + elif level == RustTracingLevel.Warn: + return "warning" + elif level == RustTracingLevel.Error: + return "error" + else: + # Better this than crashing + return "info" + + +def extract_contexts(event: Dict[str, Any]) -> Dict[str, Any]: + metadata = event.get("metadata", {}) + contexts = {} + + location = {} + for field in ["module_path", "file", "line"]: + if field in metadata: + location[field] = metadata[field] + if len(location) > 0: + contexts["rust_tracing_location"] = location + + fields = {} + for field in metadata.get("fields", []): + fields[field] = event.get(field) + if len(fields) > 0: + contexts["rust_tracing_fields"] = fields + + return contexts + + +def process_event(event: Dict[str, Any]) -> None: + metadata = event.get("metadata", {}) + + logger = metadata.get("target") + level = tracing_level_to_sentry_level(metadata.get("level")) + message = event.get("message") # type: sentry_sdk._types.Any + contexts = extract_contexts(event) + + sentry_event = { + "logger": logger, + "level": level, + "message": message, + "contexts": contexts, + } # type: sentry_sdk._types.Event + + sentry_sdk.capture_event(sentry_event) + + +def process_exception(event: Dict[str, Any]) -> None: + process_event(event) + + +def process_breadcrumb(event: Dict[str, Any]) -> None: + level = tracing_level_to_sentry_level(event.get("metadata", {}).get("level")) + message = event.get("message") + + sentry_sdk.add_breadcrumb(level=level, message=message) + + +def default_span_filter(metadata: Dict[str, Any]) -> bool: + return RustTracingLevel(metadata.get("level")) in ( + RustTracingLevel.Error, + RustTracingLevel.Warn, + RustTracingLevel.Info, + ) + + +def default_event_type_mapping(metadata: Dict[str, Any]) -> EventTypeMapping: + level = RustTracingLevel(metadata.get("level")) + if level == RustTracingLevel.Error: + return EventTypeMapping.Exc + elif level in (RustTracingLevel.Warn, RustTracingLevel.Info): + return EventTypeMapping.Breadcrumb + elif level in (RustTracingLevel.Debug, RustTracingLevel.Trace): + return EventTypeMapping.Ignore + else: + return EventTypeMapping.Ignore + + +class RustTracingLayer: + def __init__( + self, + origin: str, + event_type_mapping: Callable[ + [Dict[str, Any]], EventTypeMapping + ] = default_event_type_mapping, + span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter, + send_sensitive_data: Optional[bool] = None, + ): + self.origin = origin + self.event_type_mapping = event_type_mapping + self.span_filter = span_filter + self.send_sensitive_data = send_sensitive_data + + def on_event(self, event: str, _span_state: TraceState) -> None: + deserialized_event = json.loads(event) + metadata = deserialized_event.get("metadata", {}) + + event_type = self.event_type_mapping(metadata) + if event_type == EventTypeMapping.Ignore: + return + elif event_type == EventTypeMapping.Exc: + process_exception(deserialized_event) + elif event_type == EventTypeMapping.Breadcrumb: + process_breadcrumb(deserialized_event) + elif event_type == EventTypeMapping.Event: + process_event(deserialized_event) + + def on_new_span(self, attrs: str, span_id: str) -> TraceState: + attrs = json.loads(attrs) + metadata = attrs.get("metadata", {}) + + if not self.span_filter(metadata): + return None + + module_path = metadata.get("module_path") + name = metadata.get("name") + message = attrs.get("message") + + if message is not None: + sentry_span_name = message + elif module_path is not None and name is not None: + sentry_span_name = f"{module_path}::{name}" # noqa: E231 + elif name is not None: + sentry_span_name = name + else: + sentry_span_name = "" + + kwargs = { + "op": "function", + "name": sentry_span_name, + "origin": self.origin, + } + + scope = sentry_sdk.get_current_scope() + parent_sentry_span = scope.span + if parent_sentry_span: + sentry_span = parent_sentry_span.start_child(**kwargs) + else: + sentry_span = scope.start_span(**kwargs) + + fields = metadata.get("fields", []) + for field in fields: + sentry_span.set_data(field, attrs.get(field)) + + scope.span = sentry_span + return (parent_sentry_span, sentry_span) + + def on_close(self, span_id: str, span_state: TraceState) -> None: + if span_state is None: + return + + parent_sentry_span, sentry_span = span_state + sentry_span.finish() + sentry_sdk.get_current_scope().span = parent_sentry_span + + def on_record(self, span_id: str, values: str, span_state: TraceState) -> None: + if span_state is None: + return + _parent_sentry_span, sentry_span = span_state + + send_sensitive_data = ( + should_send_default_pii() + if self.send_sensitive_data is None + else self.send_sensitive_data + ) + + deserialized_values = json.loads(values) + for key, value in deserialized_values.items(): + if send_sensitive_data: + sentry_span.set_data(key, value) + else: + sentry_span.set_data(key, SENSITIVE_DATA_SUBSTITUTE) + + +class RustTracingIntegration(Integration): + """ + Ingests tracing data from a Rust native extension's `tracing` instrumentation. + + If a project uses more than one Rust native extension, each one will need + its own instance of `RustTracingIntegration` with an initializer function + specific to that extension. + + Since all of the setup for this integration requires instance-specific state + which is not available in `setup_once()`, setup instead happens in `__init__()`. + """ + + def __init__( + self, + identifier: str, + initializer: Callable[[RustTracingLayer], None], + event_type_mapping: Callable[ + [Dict[str, Any]], EventTypeMapping + ] = default_event_type_mapping, + span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter, + send_sensitive_data: Optional[bool] = None, + ): + self.identifier = identifier + origin = f"auto.function.rust_tracing.{identifier}" + self.tracing_layer = RustTracingLayer( + origin, event_type_mapping, span_filter, send_sensitive_data + ) + + initializer(self.tracing_layer) + + @staticmethod + def setup_once() -> None: + pass diff --git a/tests/integrations/rust_tracing/__init__.py b/tests/integrations/rust_tracing/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/integrations/rust_tracing/test_rust_tracing.py b/tests/integrations/rust_tracing/test_rust_tracing.py new file mode 100644 index 0000000000..b1fad1a7f7 --- /dev/null +++ b/tests/integrations/rust_tracing/test_rust_tracing.py @@ -0,0 +1,450 @@ +import pytest + +from string import Template +from typing import Dict + +import sentry_sdk +from sentry_sdk.integrations.rust_tracing import ( + RustTracingIntegration, + RustTracingLayer, + RustTracingLevel, + EventTypeMapping, +) +from sentry_sdk import start_transaction, capture_message + + +def _test_event_type_mapping(metadata: Dict[str, object]) -> EventTypeMapping: + level = RustTracingLevel(metadata.get("level")) + if level == RustTracingLevel.Error: + return EventTypeMapping.Exc + elif level in (RustTracingLevel.Warn, RustTracingLevel.Info): + return EventTypeMapping.Breadcrumb + elif level == RustTracingLevel.Debug: + return EventTypeMapping.Event + elif level == RustTracingLevel.Trace: + return EventTypeMapping.Ignore + else: + return EventTypeMapping.Ignore + + +class FakeRustTracing: + # Parameters: `level`, `index` + span_template = Template( + """{"index":$index,"is_root":false,"metadata":{"fields":["index","use_memoized","version"],"file":"src/lib.rs","is_event":false,"is_span":true,"level":"$level","line":40,"module_path":"_bindings","name":"fibonacci","target":"_bindings"},"parent":null,"use_memoized":true}""" + ) + + # Parameters: `level`, `index` + event_template = Template( + """{"message":"Getting the ${index}th fibonacci number","metadata":{"fields":["message"],"file":"src/lib.rs","is_event":true,"is_span":false,"level":"$level","line":23,"module_path":"_bindings","name":"event src/lib.rs:23","target":"_bindings"}}""" + ) + + def __init__(self): + self.spans = {} + + def set_layer_impl(self, layer: RustTracingLayer): + self.layer = layer + + def new_span(self, level: RustTracingLevel, span_id: int, index_arg: int = 10): + span_attrs = self.span_template.substitute(level=level.value, index=index_arg) + state = self.layer.on_new_span(span_attrs, str(span_id)) + self.spans[span_id] = state + + def close_span(self, span_id: int): + state = self.spans.pop(span_id) + self.layer.on_close(str(span_id), state) + + def event(self, level: RustTracingLevel, span_id: int, index_arg: int = 10): + event = self.event_template.substitute(level=level.value, index=index_arg) + state = self.spans[span_id] + self.layer.on_event(event, state) + + def record(self, span_id: int): + state = self.spans[span_id] + self.layer.on_record(str(span_id), """{"version": "memoized"}""", state) + + +def test_on_new_span_on_close(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_new_span_on_close", rust_tracing.set_layer_impl + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + sentry_first_rust_span = sentry_sdk.get_current_span() + _, rust_first_rust_span = rust_tracing.spans[3] + + assert sentry_first_rust_span == rust_first_rust_span + + rust_tracing.close_span(3) + assert sentry_sdk.get_current_span() != sentry_first_rust_span + + (event,) = events + assert len(event["spans"]) == 1 + + # Ensure the span metadata is wired up + span = event["spans"][0] + assert span["op"] == "function" + assert span["origin"] == "auto.function.rust_tracing.test_on_new_span_on_close" + assert span["description"] == "_bindings::fibonacci" + + # Ensure the span was opened/closed appropriately + assert span["start_timestamp"] is not None + assert span["timestamp"] is not None + + # Ensure the extra data from Rust is hooked up + data = span["data"] + assert data["use_memoized"] + assert data["index"] == 10 + assert data["version"] is None + + +def test_nested_on_new_span_on_close(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_nested_on_new_span_on_close", rust_tracing.set_layer_impl + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + with start_transaction(): + original_sentry_span = sentry_sdk.get_current_span() + + rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10) + sentry_first_rust_span = sentry_sdk.get_current_span() + _, rust_first_rust_span = rust_tracing.spans[3] + + # Use a different `index_arg` value for the inner span to help + # distinguish the two at the end of the test + rust_tracing.new_span(RustTracingLevel.Info, 5, index_arg=9) + sentry_second_rust_span = sentry_sdk.get_current_span() + rust_parent_span, rust_second_rust_span = rust_tracing.spans[5] + + assert rust_second_rust_span == sentry_second_rust_span + assert rust_parent_span == sentry_first_rust_span + assert rust_parent_span == rust_first_rust_span + assert rust_parent_span != rust_second_rust_span + + rust_tracing.close_span(5) + + # Ensure the current sentry span was moved back to the parent + sentry_span_after_close = sentry_sdk.get_current_span() + assert sentry_span_after_close == sentry_first_rust_span + + rust_tracing.close_span(3) + + assert sentry_sdk.get_current_span() == original_sentry_span + + (event,) = events + assert len(event["spans"]) == 2 + + # Ensure the span metadata is wired up for all spans + first_span, second_span = event["spans"] + assert first_span["op"] == "function" + assert ( + first_span["origin"] + == "auto.function.rust_tracing.test_nested_on_new_span_on_close" + ) + assert first_span["description"] == "_bindings::fibonacci" + assert second_span["op"] == "function" + assert ( + second_span["origin"] + == "auto.function.rust_tracing.test_nested_on_new_span_on_close" + ) + assert second_span["description"] == "_bindings::fibonacci" + + # Ensure the spans were opened/closed appropriately + assert first_span["start_timestamp"] is not None + assert first_span["timestamp"] is not None + assert second_span["start_timestamp"] is not None + assert second_span["timestamp"] is not None + + # Ensure the extra data from Rust is hooked up in both spans + first_span_data = first_span["data"] + assert first_span_data["use_memoized"] + assert first_span_data["index"] == 10 + assert first_span_data["version"] is None + + second_span_data = second_span["data"] + assert second_span_data["use_memoized"] + assert second_span_data["index"] == 9 + assert second_span_data["version"] is None + + +def test_on_new_span_without_transaction(sentry_init): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_new_span_without_transaction", rust_tracing.set_layer_impl + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + assert sentry_sdk.get_current_span() is None + + # Should still create a span hierarchy, it just will not be under a txn + rust_tracing.new_span(RustTracingLevel.Info, 3) + current_span = sentry_sdk.get_current_span() + assert current_span is not None + assert current_span.containing_transaction is None + + +def test_on_event_exception(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_event_exception", + rust_tracing.set_layer_impl, + event_type_mapping=_test_event_type_mapping, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + sentry_sdk.get_isolation_scope().clear_breadcrumbs() + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + # Mapped to Exception + rust_tracing.event(RustTracingLevel.Error, 3) + + rust_tracing.close_span(3) + + assert len(events) == 2 + exc, _tx = events + assert exc["level"] == "error" + assert exc["logger"] == "_bindings" + assert exc["message"] == "Getting the 10th fibonacci number" + assert exc["breadcrumbs"]["values"] == [] + + location_context = exc["contexts"]["rust_tracing_location"] + assert location_context["module_path"] == "_bindings" + assert location_context["file"] == "src/lib.rs" + assert location_context["line"] == 23 + + field_context = exc["contexts"]["rust_tracing_fields"] + assert field_context["message"] == "Getting the 10th fibonacci number" + + +def test_on_event_breadcrumb(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_event_breadcrumb", + rust_tracing.set_layer_impl, + event_type_mapping=_test_event_type_mapping, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + sentry_sdk.get_isolation_scope().clear_breadcrumbs() + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + # Mapped to Breadcrumb + rust_tracing.event(RustTracingLevel.Info, 3) + + rust_tracing.close_span(3) + capture_message("test message") + + assert len(events) == 2 + message, _tx = events + + breadcrumbs = message["breadcrumbs"]["values"] + assert len(breadcrumbs) == 1 + assert breadcrumbs[0]["level"] == "info" + assert breadcrumbs[0]["message"] == "Getting the 10th fibonacci number" + assert breadcrumbs[0]["type"] == "default" + + +def test_on_event_event(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_event_event", + rust_tracing.set_layer_impl, + event_type_mapping=_test_event_type_mapping, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + sentry_sdk.get_isolation_scope().clear_breadcrumbs() + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + # Mapped to Event + rust_tracing.event(RustTracingLevel.Debug, 3) + + rust_tracing.close_span(3) + + assert len(events) == 2 + event, _tx = events + + assert event["logger"] == "_bindings" + assert event["level"] == "debug" + assert event["message"] == "Getting the 10th fibonacci number" + assert event["breadcrumbs"]["values"] == [] + + location_context = event["contexts"]["rust_tracing_location"] + assert location_context["module_path"] == "_bindings" + assert location_context["file"] == "src/lib.rs" + assert location_context["line"] == 23 + + field_context = event["contexts"]["rust_tracing_fields"] + assert field_context["message"] == "Getting the 10th fibonacci number" + + +def test_on_event_ignored(sentry_init, capture_events): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_on_event_ignored", + rust_tracing.set_layer_impl, + event_type_mapping=_test_event_type_mapping, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + sentry_sdk.get_isolation_scope().clear_breadcrumbs() + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + # Ignored + rust_tracing.event(RustTracingLevel.Trace, 3) + + rust_tracing.close_span(3) + + assert len(events) == 1 + (tx,) = events + assert tx["type"] == "transaction" + assert "message" not in tx + + +def test_span_filter(sentry_init, capture_events): + def span_filter(metadata: Dict[str, object]) -> bool: + return RustTracingLevel(metadata.get("level")) in ( + RustTracingLevel.Error, + RustTracingLevel.Warn, + RustTracingLevel.Info, + RustTracingLevel.Debug, + ) + + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_span_filter", rust_tracing.set_layer_impl, span_filter=span_filter + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + events = capture_events() + with start_transaction(): + original_sentry_span = sentry_sdk.get_current_span() + + # Span is not ignored + rust_tracing.new_span(RustTracingLevel.Info, 3, index_arg=10) + info_span = sentry_sdk.get_current_span() + + # Span is ignored, current span should remain the same + rust_tracing.new_span(RustTracingLevel.Trace, 5, index_arg=9) + assert sentry_sdk.get_current_span() == info_span + + # Closing the filtered span should leave the current span alone + rust_tracing.close_span(5) + assert sentry_sdk.get_current_span() == info_span + + rust_tracing.close_span(3) + assert sentry_sdk.get_current_span() == original_sentry_span + + (event,) = events + assert len(event["spans"]) == 1 + # The ignored span has index == 9 + assert event["spans"][0]["data"]["index"] == 10 + + +def test_record(sentry_init): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_record", + initializer=rust_tracing.set_layer_impl, + send_sensitive_data=True, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + span_before_record = sentry_sdk.get_current_span().to_json() + assert span_before_record["data"]["version"] is None + + rust_tracing.record(3) + + span_after_record = sentry_sdk.get_current_span().to_json() + assert span_after_record["data"]["version"] == "memoized" + + +def test_record_in_ignored_span(sentry_init): + def span_filter(metadata: Dict[str, object]) -> bool: + # Just ignore Trace + return RustTracingLevel(metadata.get("level")) != RustTracingLevel.Trace + + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_record_in_ignored_span", + rust_tracing.set_layer_impl, + span_filter=span_filter, + ) + sentry_init(integrations=[integration], traces_sample_rate=1.0) + + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + span_before_record = sentry_sdk.get_current_span().to_json() + assert span_before_record["data"]["version"] is None + + rust_tracing.new_span(RustTracingLevel.Trace, 5) + rust_tracing.record(5) + + # `on_record()` should not do anything to the current Sentry span if the associated Rust span was ignored + span_after_record = sentry_sdk.get_current_span().to_json() + assert span_after_record["data"]["version"] is None + + +@pytest.mark.parametrize( + "send_default_pii, send_sensitive_data, sensitive_data_expected", + [ + (True, True, True), + (True, False, False), + (True, None, True), + (False, True, True), + (False, False, False), + (False, None, False), + ], +) +def test_sensitive_data( + sentry_init, send_default_pii, send_sensitive_data, sensitive_data_expected +): + rust_tracing = FakeRustTracing() + integration = RustTracingIntegration( + "test_record", + initializer=rust_tracing.set_layer_impl, + send_sensitive_data=send_sensitive_data, + ) + + sentry_init( + integrations=[integration], + traces_sample_rate=1.0, + send_default_pii=send_default_pii, + ) + with start_transaction(): + rust_tracing.new_span(RustTracingLevel.Info, 3) + + span_before_record = sentry_sdk.get_current_span().to_json() + assert span_before_record["data"]["version"] is None + + rust_tracing.record(3) + + span_after_record = sentry_sdk.get_current_span().to_json() + + if sensitive_data_expected: + assert span_after_record["data"]["version"] == "memoized" + else: + assert span_after_record["data"]["version"] == "[Filtered]" From da0b086333e03292da97993cf3e718fa1e9937a5 Mon Sep 17 00:00:00 2001 From: matt-codecov <137832199+matt-codecov@users.noreply.github.com> Date: Thu, 14 Nov 2024 23:55:56 -0800 Subject: [PATCH 07/22] fix: include_tracing_fields arg to control unvetted data in rust_tracing integration (#3780) Rename `send_sensitive_data` flag to `include_tracing_fields`. the data in question is generally data the user expects `tracing` to record or data they explicitly passed into a log statement to be recorded, so if we call it "sensitive" they may think we are referring to something else also, apply the same condition to both `on_record()` and `on_new_span()`. both callbacks set the same fields, so they should either both be redacted or both be allowed. previously only `on_record()` had the condition applied. Co-authored-by: Anton Pirker --- sentry_sdk/integrations/rust_tracing.py | 34 ++++++++----- .../rust_tracing/test_rust_tracing.py | 49 ++++++++++++++----- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/sentry_sdk/integrations/rust_tracing.py b/sentry_sdk/integrations/rust_tracing.py index 121bf082b8..ae52c850c3 100644 --- a/sentry_sdk/integrations/rust_tracing.py +++ b/sentry_sdk/integrations/rust_tracing.py @@ -151,12 +151,25 @@ def __init__( [Dict[str, Any]], EventTypeMapping ] = default_event_type_mapping, span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter, - send_sensitive_data: Optional[bool] = None, + include_tracing_fields: Optional[bool] = None, ): self.origin = origin self.event_type_mapping = event_type_mapping self.span_filter = span_filter - self.send_sensitive_data = send_sensitive_data + self.include_tracing_fields = include_tracing_fields + + def _include_tracing_fields(self) -> bool: + """ + By default, the values of tracing fields are not included in case they + contain PII. A user may override that by passing `True` for the + `include_tracing_fields` keyword argument of this integration or by + setting `send_default_pii` to `True` in their Sentry client options. + """ + return ( + should_send_default_pii() + if self.include_tracing_fields is None + else self.include_tracing_fields + ) def on_event(self, event: str, _span_state: TraceState) -> None: deserialized_event = json.loads(event) @@ -207,7 +220,10 @@ def on_new_span(self, attrs: str, span_id: str) -> TraceState: fields = metadata.get("fields", []) for field in fields: - sentry_span.set_data(field, attrs.get(field)) + if self._include_tracing_fields(): + sentry_span.set_data(field, attrs.get(field)) + else: + sentry_span.set_data(field, SENSITIVE_DATA_SUBSTITUTE) scope.span = sentry_span return (parent_sentry_span, sentry_span) @@ -225,15 +241,9 @@ def on_record(self, span_id: str, values: str, span_state: TraceState) -> None: return _parent_sentry_span, sentry_span = span_state - send_sensitive_data = ( - should_send_default_pii() - if self.send_sensitive_data is None - else self.send_sensitive_data - ) - deserialized_values = json.loads(values) for key, value in deserialized_values.items(): - if send_sensitive_data: + if self._include_tracing_fields(): sentry_span.set_data(key, value) else: sentry_span.set_data(key, SENSITIVE_DATA_SUBSTITUTE) @@ -259,12 +269,12 @@ def __init__( [Dict[str, Any]], EventTypeMapping ] = default_event_type_mapping, span_filter: Callable[[Dict[str, Any]], bool] = default_span_filter, - send_sensitive_data: Optional[bool] = None, + include_tracing_fields: Optional[bool] = None, ): self.identifier = identifier origin = f"auto.function.rust_tracing.{identifier}" self.tracing_layer = RustTracingLayer( - origin, event_type_mapping, span_filter, send_sensitive_data + origin, event_type_mapping, span_filter, include_tracing_fields ) initializer(self.tracing_layer) diff --git a/tests/integrations/rust_tracing/test_rust_tracing.py b/tests/integrations/rust_tracing/test_rust_tracing.py index b1fad1a7f7..893fc86966 100644 --- a/tests/integrations/rust_tracing/test_rust_tracing.py +++ b/tests/integrations/rust_tracing/test_rust_tracing.py @@ -1,3 +1,4 @@ +from unittest import mock import pytest from string import Template @@ -66,7 +67,9 @@ def record(self, span_id: int): def test_on_new_span_on_close(sentry_init, capture_events): rust_tracing = FakeRustTracing() integration = RustTracingIntegration( - "test_on_new_span_on_close", rust_tracing.set_layer_impl + "test_on_new_span_on_close", + initializer=rust_tracing.set_layer_impl, + include_tracing_fields=True, ) sentry_init(integrations=[integration], traces_sample_rate=1.0) @@ -105,7 +108,9 @@ def test_on_new_span_on_close(sentry_init, capture_events): def test_nested_on_new_span_on_close(sentry_init, capture_events): rust_tracing = FakeRustTracing() integration = RustTracingIntegration( - "test_nested_on_new_span_on_close", rust_tracing.set_layer_impl + "test_nested_on_new_span_on_close", + initializer=rust_tracing.set_layer_impl, + include_tracing_fields=True, ) sentry_init(integrations=[integration], traces_sample_rate=1.0) @@ -331,7 +336,10 @@ def span_filter(metadata: Dict[str, object]) -> bool: rust_tracing = FakeRustTracing() integration = RustTracingIntegration( - "test_span_filter", rust_tracing.set_layer_impl, span_filter=span_filter + "test_span_filter", + initializer=rust_tracing.set_layer_impl, + span_filter=span_filter, + include_tracing_fields=True, ) sentry_init(integrations=[integration], traces_sample_rate=1.0) @@ -365,7 +373,7 @@ def test_record(sentry_init): integration = RustTracingIntegration( "test_record", initializer=rust_tracing.set_layer_impl, - send_sensitive_data=True, + include_tracing_fields=True, ) sentry_init(integrations=[integration], traces_sample_rate=1.0) @@ -391,6 +399,7 @@ def span_filter(metadata: Dict[str, object]) -> bool: "test_record_in_ignored_span", rust_tracing.set_layer_impl, span_filter=span_filter, + include_tracing_fields=True, ) sentry_init(integrations=[integration], traces_sample_rate=1.0) @@ -409,7 +418,7 @@ def span_filter(metadata: Dict[str, object]) -> bool: @pytest.mark.parametrize( - "send_default_pii, send_sensitive_data, sensitive_data_expected", + "send_default_pii, include_tracing_fields, tracing_fields_expected", [ (True, True, True), (True, False, False), @@ -419,14 +428,14 @@ def span_filter(metadata: Dict[str, object]) -> bool: (False, None, False), ], ) -def test_sensitive_data( - sentry_init, send_default_pii, send_sensitive_data, sensitive_data_expected +def test_include_tracing_fields( + sentry_init, send_default_pii, include_tracing_fields, tracing_fields_expected ): rust_tracing = FakeRustTracing() integration = RustTracingIntegration( "test_record", initializer=rust_tracing.set_layer_impl, - send_sensitive_data=send_sensitive_data, + include_tracing_fields=include_tracing_fields, ) sentry_init( @@ -438,13 +447,29 @@ def test_sensitive_data( rust_tracing.new_span(RustTracingLevel.Info, 3) span_before_record = sentry_sdk.get_current_span().to_json() - assert span_before_record["data"]["version"] is None + if tracing_fields_expected: + assert span_before_record["data"]["version"] is None + else: + assert span_before_record["data"]["version"] == "[Filtered]" rust_tracing.record(3) span_after_record = sentry_sdk.get_current_span().to_json() - if sensitive_data_expected: - assert span_after_record["data"]["version"] == "memoized" + if tracing_fields_expected: + assert span_after_record["data"] == { + "thread.id": mock.ANY, + "thread.name": mock.ANY, + "use_memoized": True, + "version": "memoized", + "index": 10, + } + else: - assert span_after_record["data"]["version"] == "[Filtered]" + assert span_after_record["data"] == { + "thread.id": mock.ANY, + "thread.name": mock.ANY, + "use_memoized": "[Filtered]", + "version": "[Filtered]", + "index": "[Filtered]", + } From a82651928148a9fc1a9b903ecd0cc6e1f6d551d9 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 18 Nov 2024 09:30:01 +0100 Subject: [PATCH 08/22] tests: Test with pyspark prerelease (#3760) --- tox.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tox.ini b/tox.ini index f3a7ba4ea0..6acff6b8e8 100644 --- a/tox.ini +++ b/tox.ini @@ -647,6 +647,8 @@ deps = spark-v3.1: pyspark~=3.1.0 spark-v3.3: pyspark~=3.3.0 spark-v3.5: pyspark~=3.5.0 + # TODO: update to ~=4.0.0 once stable is out + spark-v4.0: pyspark==4.0.0.dev2 spark-latest: pyspark # Starlette From ec2d929e9f2b4cdcbbb13a3685c9d420ce47289b Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 18 Nov 2024 10:00:47 +0100 Subject: [PATCH 09/22] Make sentry-sdk[pure-eval] installable with pip==24.0 (#3757) --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7ac4b56fde..29a40c6663 100644 --- a/setup.py +++ b/setup.py @@ -70,7 +70,7 @@ def get_file_text(file_name): "openfeature": ["openfeature-sdk>=0.7.1"], "opentelemetry": ["opentelemetry-distro>=0.35b0"], "opentelemetry-experimental": ["opentelemetry-distro"], - "pure_eval": ["pure_eval", "executing", "asttokens"], + "pure-eval": ["pure_eval", "executing", "asttokens"], "pymongo": ["pymongo>=3.1"], "pyspark": ["pyspark>=2.4.4"], "quart": ["quart>=0.16.1", "blinker>=1.1"], From 955108e5642d74d9d95535c2a1f263fcbbc62c92 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Tue, 19 Nov 2024 08:55:21 +0000 Subject: [PATCH 10/22] feat(spotlight): Auto enable cache_spans for Spotlight on DEBUG (#3791) This patch enables `cache_spans` in Django integration automatically when Spotlight is enabled and `DEBUG` is set in Django settings. --- sentry_sdk/integrations/django/caching.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index 4bd7cb7236..39d1679183 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -132,10 +132,22 @@ def _get_address_port(settings): return address, int(port) if port is not None else None -def patch_caching(): - # type: () -> None +def should_enable_cache_spans(): + # type: () -> bool from sentry_sdk.integrations.django import DjangoIntegration + client = sentry_sdk.get_client() + integration = client.get_integration(DjangoIntegration) + from django.conf import settings + + return integration is not None and ( + (client.spotlight is not None and settings.DEBUG is True) + or integration.cache_spans is True + ) + + +def patch_caching(): + # type: () -> None if not hasattr(CacheHandler, "_sentry_patched"): if DJANGO_VERSION < (3, 2): original_get_item = CacheHandler.__getitem__ @@ -145,8 +157,7 @@ def sentry_get_item(self, alias): # type: (CacheHandler, str) -> Any cache = original_get_item(self, alias) - integration = sentry_sdk.get_client().get_integration(DjangoIntegration) - if integration is not None and integration.cache_spans: + if should_enable_cache_spans(): from django.conf import settings address, port = _get_address_port( @@ -168,8 +179,7 @@ def sentry_create_connection(self, alias): # type: (CacheHandler, str) -> Any cache = original_create_connection(self, alias) - integration = sentry_sdk.get_client().get_integration(DjangoIntegration) - if integration is not None and integration.cache_spans: + if should_enable_cache_spans(): address, port = _get_address_port(self.settings[alias or "default"]) _patch_cache(cache, address, port) From 1bd744dbb854508fc287862f4d17cc99501e3150 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Nov 2024 10:49:25 +0100 Subject: [PATCH 11/22] build(deps): bump codecov/codecov-action from 4.6.0 to 5.0.2 (#3792) * build(deps): bump codecov/codecov-action from 4.6.0 to 5.0.2 Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4.6.0 to 5.0.2. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v4.6.0...v5.0.2) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Ivana Kellyer --- .github/workflows/test-integrations-ai.yml | 4 ++-- .github/workflows/test-integrations-aws-lambda.yml | 2 +- .github/workflows/test-integrations-cloud-computing.yml | 4 ++-- .github/workflows/test-integrations-common.yml | 2 +- .github/workflows/test-integrations-data-processing.yml | 4 ++-- .github/workflows/test-integrations-databases.yml | 4 ++-- .github/workflows/test-integrations-graphql.yml | 4 ++-- .github/workflows/test-integrations-miscellaneous.yml | 4 ++-- .github/workflows/test-integrations-networking.yml | 4 ++-- .github/workflows/test-integrations-web-frameworks-1.yml | 4 ++-- .github/workflows/test-integrations-web-frameworks-2.yml | 4 ++-- scripts/split-tox-gh-actions/templates/test_group.jinja | 2 +- 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test-integrations-ai.yml b/.github/workflows/test-integrations-ai.yml index dd230a6461..c7cf4a1d85 100644 --- a/.github/workflows/test-integrations-ai.yml +++ b/.github/workflows/test-integrations-ai.yml @@ -78,7 +78,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -150,7 +150,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-aws-lambda.yml b/.github/workflows/test-integrations-aws-lambda.yml index c9837c08d0..d85d1d4a8e 100644 --- a/.github/workflows/test-integrations-aws-lambda.yml +++ b/.github/workflows/test-integrations-aws-lambda.yml @@ -97,7 +97,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-cloud-computing.yml b/.github/workflows/test-integrations-cloud-computing.yml index 3217811539..9013a02af3 100644 --- a/.github/workflows/test-integrations-cloud-computing.yml +++ b/.github/workflows/test-integrations-cloud-computing.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-common.yml b/.github/workflows/test-integrations-common.yml index 912eb3b18c..6983a079ef 100644 --- a/.github/workflows/test-integrations-common.yml +++ b/.github/workflows/test-integrations-common.yml @@ -62,7 +62,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-data-processing.yml b/.github/workflows/test-integrations-data-processing.yml index 128463a66a..6ad3d707fe 100644 --- a/.github/workflows/test-integrations-data-processing.yml +++ b/.github/workflows/test-integrations-data-processing.yml @@ -92,7 +92,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -178,7 +178,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-databases.yml b/.github/workflows/test-integrations-databases.yml index 2cdcd9d3b9..045f942b9c 100644 --- a/.github/workflows/test-integrations-databases.yml +++ b/.github/workflows/test-integrations-databases.yml @@ -101,7 +101,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -196,7 +196,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-graphql.yml b/.github/workflows/test-integrations-graphql.yml index 522dc2acc1..57d14cff10 100644 --- a/.github/workflows/test-integrations-graphql.yml +++ b/.github/workflows/test-integrations-graphql.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-miscellaneous.yml b/.github/workflows/test-integrations-miscellaneous.yml index 03d6559108..ebb486b6b6 100644 --- a/.github/workflows/test-integrations-miscellaneous.yml +++ b/.github/workflows/test-integrations-miscellaneous.yml @@ -86,7 +86,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -166,7 +166,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-networking.yml b/.github/workflows/test-integrations-networking.yml index 31342151e9..2c9a788954 100644 --- a/.github/workflows/test-integrations-networking.yml +++ b/.github/workflows/test-integrations-networking.yml @@ -74,7 +74,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -142,7 +142,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-web-frameworks-1.yml b/.github/workflows/test-integrations-web-frameworks-1.yml index 706feb385f..d4a9aff6f1 100644 --- a/.github/workflows/test-integrations-web-frameworks-1.yml +++ b/.github/workflows/test-integrations-web-frameworks-1.yml @@ -92,7 +92,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -178,7 +178,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/.github/workflows/test-integrations-web-frameworks-2.yml b/.github/workflows/test-integrations-web-frameworks-2.yml index f700952e00..f0cdcc4510 100644 --- a/.github/workflows/test-integrations-web-frameworks-2.yml +++ b/.github/workflows/test-integrations-web-frameworks-2.yml @@ -98,7 +98,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml @@ -190,7 +190,7 @@ jobs: coverage xml - name: Upload coverage to Codecov if: ${{ !cancelled() }} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: ${{ secrets.CODECOV_TOKEN }} files: coverage.xml diff --git a/scripts/split-tox-gh-actions/templates/test_group.jinja b/scripts/split-tox-gh-actions/templates/test_group.jinja index 9055070c72..4560a7d42d 100644 --- a/scripts/split-tox-gh-actions/templates/test_group.jinja +++ b/scripts/split-tox-gh-actions/templates/test_group.jinja @@ -92,7 +92,7 @@ - name: Upload coverage to Codecov if: {% raw %}${{ !cancelled() }}{% endraw %} - uses: codecov/codecov-action@v4.6.0 + uses: codecov/codecov-action@v5.0.2 with: token: {% raw %}${{ secrets.CODECOV_TOKEN }}{% endraw %} files: coverage.xml From d894fc232055ea06ac2ba1431519849e97973423 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 19 Nov 2024 15:29:12 +0100 Subject: [PATCH 12/22] Shorten CI workflow names (#3805) Getting around a GH UI issue where you can't see the whole name of the test that failed --- ...-aws-lambda.yml => test-integrations-aws.yml} | 12 ++++++------ ...computing.yml => test-integrations-cloud.yml} | 16 ++++++++-------- ...s-databases.yml => test-integrations-dbs.yml} | 16 ++++++++-------- ...cellaneous.yml => test-integrations-misc.yml} | 16 ++++++++-------- ...working.yml => test-integrations-network.yml} | 16 ++++++++-------- ...rocessing.yml => test-integrations-tasks.yml} | 16 ++++++++-------- ...meworks-1.yml => test-integrations-web-1.yml} | 16 ++++++++-------- ...meworks-2.yml => test-integrations-web-2.yml} | 16 ++++++++-------- .../split-tox-gh-actions/split-tox-gh-actions.py | 16 ++++++++-------- 9 files changed, 70 insertions(+), 70 deletions(-) rename .github/workflows/{test-integrations-aws-lambda.yml => test-integrations-aws.yml} (94%) rename .github/workflows/{test-integrations-cloud-computing.yml => test-integrations-cloud.yml} (93%) rename .github/workflows/{test-integrations-databases.yml => test-integrations-dbs.yml} (96%) rename .github/workflows/{test-integrations-miscellaneous.yml => test-integrations-misc.yml} (95%) rename .github/workflows/{test-integrations-networking.yml => test-integrations-network.yml} (94%) rename .github/workflows/{test-integrations-data-processing.yml => test-integrations-tasks.yml} (95%) rename .github/workflows/{test-integrations-web-frameworks-1.yml => test-integrations-web-1.yml} (94%) rename .github/workflows/{test-integrations-web-frameworks-2.yml => test-integrations-web-2.yml} (95%) diff --git a/.github/workflows/test-integrations-aws-lambda.yml b/.github/workflows/test-integrations-aws.yml similarity index 94% rename from .github/workflows/test-integrations-aws-lambda.yml rename to .github/workflows/test-integrations-aws.yml index d85d1d4a8e..67c0ec31c7 100644 --- a/.github/workflows/test-integrations-aws-lambda.yml +++ b/.github/workflows/test-integrations-aws.yml @@ -1,6 +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 -name: Test AWS Lambda +name: Test AWS on: push: branches: @@ -52,8 +52,8 @@ jobs: - name: Check permissions on repo branch if: github.event_name == 'push' run: true - test-aws_lambda-pinned: - name: AWS Lambda (pinned) + test-aws-pinned: + name: AWS (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -112,13 +112,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned AWS Lambda tests passed - needs: test-aws_lambda-pinned + name: All pinned AWS tests passed + needs: test-aws-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-aws_lambda-pinned.result, 'failure') || contains(needs.test-aws_lambda-pinned.result, 'skipped') + if: contains(needs.test-aws-pinned.result, 'failure') || contains(needs.test-aws-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-cloud-computing.yml b/.github/workflows/test-integrations-cloud.yml similarity index 93% rename from .github/workflows/test-integrations-cloud-computing.yml rename to .github/workflows/test-integrations-cloud.yml index 9013a02af3..62d67200a5 100644 --- a/.github/workflows/test-integrations-cloud-computing.yml +++ b/.github/workflows/test-integrations-cloud.yml @@ -1,6 +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 -name: Test Cloud Computing +name: Test Cloud on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-cloud_computing-latest: - name: Cloud Computing (latest) + test-cloud-latest: + name: Cloud (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -88,8 +88,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-cloud_computing-pinned: - name: Cloud Computing (pinned) + test-cloud-pinned: + name: Cloud (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -157,13 +157,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Cloud Computing tests passed - needs: test-cloud_computing-pinned + name: All pinned Cloud tests passed + needs: test-cloud-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-cloud_computing-pinned.result, 'failure') || contains(needs.test-cloud_computing-pinned.result, 'skipped') + if: contains(needs.test-cloud-pinned.result, 'failure') || contains(needs.test-cloud-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-databases.yml b/.github/workflows/test-integrations-dbs.yml similarity index 96% rename from .github/workflows/test-integrations-databases.yml rename to .github/workflows/test-integrations-dbs.yml index 045f942b9c..1612dfb432 100644 --- a/.github/workflows/test-integrations-databases.yml +++ b/.github/workflows/test-integrations-dbs.yml @@ -1,6 +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 -name: Test Databases +name: Test DBs on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-databases-latest: - name: Databases (latest) + test-dbs-latest: + name: DBs (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -115,8 +115,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-databases-pinned: - name: Databases (pinned) + test-dbs-pinned: + name: DBs (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -211,13 +211,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Databases tests passed - needs: test-databases-pinned + name: All pinned DBs tests passed + needs: test-dbs-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-databases-pinned.result, 'failure') || contains(needs.test-databases-pinned.result, 'skipped') + if: contains(needs.test-dbs-pinned.result, 'failure') || contains(needs.test-dbs-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-miscellaneous.yml b/.github/workflows/test-integrations-misc.yml similarity index 95% rename from .github/workflows/test-integrations-miscellaneous.yml rename to .github/workflows/test-integrations-misc.yml index ebb486b6b6..5f2baa5759 100644 --- a/.github/workflows/test-integrations-miscellaneous.yml +++ b/.github/workflows/test-integrations-misc.yml @@ -1,6 +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 -name: Test Miscellaneous +name: Test Misc on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-miscellaneous-latest: - name: Miscellaneous (latest) + test-misc-latest: + name: Misc (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -100,8 +100,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-miscellaneous-pinned: - name: Miscellaneous (pinned) + test-misc-pinned: + name: Misc (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -181,13 +181,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Miscellaneous tests passed - needs: test-miscellaneous-pinned + name: All pinned Misc tests passed + needs: test-misc-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-miscellaneous-pinned.result, 'failure') || contains(needs.test-miscellaneous-pinned.result, 'skipped') + if: contains(needs.test-misc-pinned.result, 'failure') || contains(needs.test-misc-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-networking.yml b/.github/workflows/test-integrations-network.yml similarity index 94% rename from .github/workflows/test-integrations-networking.yml rename to .github/workflows/test-integrations-network.yml index 2c9a788954..7c1c343aac 100644 --- a/.github/workflows/test-integrations-networking.yml +++ b/.github/workflows/test-integrations-network.yml @@ -1,6 +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 -name: Test Networking +name: Test Network on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-networking-latest: - name: Networking (latest) + test-network-latest: + name: Network (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -88,8 +88,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-networking-pinned: - name: Networking (pinned) + test-network-pinned: + name: Network (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -157,13 +157,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Networking tests passed - needs: test-networking-pinned + name: All pinned Network tests passed + needs: test-network-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-networking-pinned.result, 'failure') || contains(needs.test-networking-pinned.result, 'skipped') + if: contains(needs.test-network-pinned.result, 'failure') || contains(needs.test-network-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-data-processing.yml b/.github/workflows/test-integrations-tasks.yml similarity index 95% rename from .github/workflows/test-integrations-data-processing.yml rename to .github/workflows/test-integrations-tasks.yml index 6ad3d707fe..1c4259ac05 100644 --- a/.github/workflows/test-integrations-data-processing.yml +++ b/.github/workflows/test-integrations-tasks.yml @@ -1,6 +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 -name: Test Data Processing +name: Test Tasks on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-data_processing-latest: - name: Data Processing (latest) + test-tasks-latest: + name: Tasks (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -106,8 +106,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-data_processing-pinned: - name: Data Processing (pinned) + test-tasks-pinned: + name: Tasks (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -193,13 +193,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Data Processing tests passed - needs: test-data_processing-pinned + name: All pinned Tasks tests passed + needs: test-tasks-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-data_processing-pinned.result, 'failure') || contains(needs.test-data_processing-pinned.result, 'skipped') + if: contains(needs.test-tasks-pinned.result, 'failure') || contains(needs.test-tasks-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-web-frameworks-1.yml b/.github/workflows/test-integrations-web-1.yml similarity index 94% rename from .github/workflows/test-integrations-web-frameworks-1.yml rename to .github/workflows/test-integrations-web-1.yml index d4a9aff6f1..6a6a01e8ff 100644 --- a/.github/workflows/test-integrations-web-frameworks-1.yml +++ b/.github/workflows/test-integrations-web-1.yml @@ -1,6 +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 -name: Test Web Frameworks 1 +name: Test Web 1 on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-web_frameworks_1-latest: - name: Web Frameworks 1 (latest) + test-web_1-latest: + name: Web 1 (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -106,8 +106,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-web_frameworks_1-pinned: - name: Web Frameworks 1 (pinned) + test-web_1-pinned: + name: Web 1 (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -193,13 +193,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Web Frameworks 1 tests passed - needs: test-web_frameworks_1-pinned + name: All pinned Web 1 tests passed + needs: test-web_1-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-web_frameworks_1-pinned.result, 'failure') || contains(needs.test-web_frameworks_1-pinned.result, 'skipped') + if: contains(needs.test-web_1-pinned.result, 'failure') || contains(needs.test-web_1-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 diff --git a/.github/workflows/test-integrations-web-frameworks-2.yml b/.github/workflows/test-integrations-web-2.yml similarity index 95% rename from .github/workflows/test-integrations-web-frameworks-2.yml rename to .github/workflows/test-integrations-web-2.yml index f0cdcc4510..11cfc20612 100644 --- a/.github/workflows/test-integrations-web-frameworks-2.yml +++ b/.github/workflows/test-integrations-web-2.yml @@ -1,6 +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 -name: Test Web Frameworks 2 +name: Test Web 2 on: push: branches: @@ -20,8 +20,8 @@ env: CACHED_BUILD_PATHS: | ${{ github.workspace }}/dist-serverless jobs: - test-web_frameworks_2-latest: - name: Web Frameworks 2 (latest) + test-web_2-latest: + name: Web 2 (latest) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -112,8 +112,8 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} files: .junitxml verbose: true - test-web_frameworks_2-pinned: - name: Web Frameworks 2 (pinned) + test-web_2-pinned: + name: Web 2 (pinned) timeout-minutes: 30 runs-on: ${{ matrix.os }} strategy: @@ -205,13 +205,13 @@ jobs: files: .junitxml verbose: true check_required_tests: - name: All pinned Web Frameworks 2 tests passed - needs: test-web_frameworks_2-pinned + name: All pinned Web 2 tests passed + needs: test-web_2-pinned # Always run this, even if a dependent job failed if: always() runs-on: ubuntu-20.04 steps: - name: Check for failures - if: contains(needs.test-web_frameworks_2-pinned.result, 'failure') || contains(needs.test-web_frameworks_2-pinned.result, 'skipped') + if: contains(needs.test-web_2-pinned.result, 'failure') || contains(needs.test-web_2-pinned.result, 'skipped') run: | echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1 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 c0bf2a7a09..c4b8f3e5e5 100755 --- a/scripts/split-tox-gh-actions/split-tox-gh-actions.py +++ b/scripts/split-tox-gh-actions/split-tox-gh-actions.py @@ -65,18 +65,18 @@ "openai", "huggingface_hub", ], - "AWS Lambda": [ + "AWS": [ # this is separate from Cloud Computing because only this one test suite # needs to run with access to GitHub secrets "aws_lambda", ], - "Cloud Computing": [ + "Cloud": [ "boto3", "chalice", "cloud_resource_context", "gcp", ], - "Data Processing": [ + "Tasks": [ "arq", "beam", "celery", @@ -86,7 +86,7 @@ "rq", "spark", ], - "Databases": [ + "DBs": [ "asyncpg", "clickhouse_driver", "pymongo", @@ -100,19 +100,19 @@ "graphene", "strawberry", ], - "Networking": [ + "Network": [ "gevent", "grpc", "httpx", "requests", ], - "Web Frameworks 1": [ + "Web 1": [ "django", "flask", "starlette", "fastapi", ], - "Web Frameworks 2": [ + "Web 2": [ "aiohttp", "asgi", "bottle", @@ -124,7 +124,7 @@ "starlite", "tornado", ], - "Miscellaneous": [ + "Misc": [ "launchdarkly", "loguru", "openfeature", From 01146bd3adeb220bcf6cdd7ca634d2d2bc83b18f Mon Sep 17 00:00:00 2001 From: sourceful-rob <84452928+sourceful-rob@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:27:13 +0000 Subject: [PATCH 13/22] fix(openai): Use name instead of description (#3807) Update the arguments in the start_span function. Specifically, changing the deprecated "description" to "name". This was causing a deprecation warning when running tests. --- sentry_sdk/integrations/openai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/openai.py b/sentry_sdk/integrations/openai.py index e6ac36f3cb..61d335b170 100644 --- a/sentry_sdk/integrations/openai.py +++ b/sentry_sdk/integrations/openai.py @@ -137,7 +137,7 @@ def _new_chat_completion_common(f, *args, **kwargs): span = sentry_sdk.start_span( op=consts.OP.OPENAI_CHAT_COMPLETIONS_CREATE, - description="Chat Completion", + name="Chat Completion", origin=OpenAIIntegration.origin, ) span.__enter__() From 3e2885322a633398d62e8f1dae6315eefec35a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Figea?= <59359380+malkovro@users.noreply.github.com> Date: Tue, 19 Nov 2024 16:51:29 +0100 Subject: [PATCH 14/22] fix(integrations): Check retries_left before capturing exception (#3803) Since rq/rq#1964 the job status is set to Failed before the handler decides whether to capture or not the exception while handle_job_failure has not yet been called so the job is not yet re-scheduled leading to all exceptions getting captured in RQ version >= 2.0. Related to #1076 Fixes #3707 --- sentry_sdk/integrations/rq.py | 10 +++++++--- tests/integrations/rq/test_rq.py | 5 ----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/rq.py b/sentry_sdk/integrations/rq.py index c0df1c5e53..462f3ad30a 100644 --- a/sentry_sdk/integrations/rq.py +++ b/sentry_sdk/integrations/rq.py @@ -90,9 +90,13 @@ def sentry_patched_perform_job(self, job, *args, **kwargs): def sentry_patched_handle_exception(self, job, *exc_info, **kwargs): # type: (Worker, Any, *Any, **Any) -> Any - # Note, the order of the `or` here is important, - # because calling `job.is_failed` will change `_status`. - if job._status == JobStatus.FAILED or job.is_failed: + retry = ( + hasattr(job, "retries_left") + and job.retries_left + and job.retries_left > 0 + ) + failed = job._status == JobStatus.FAILED or job.is_failed + if failed and not retry: _capture_exception(exc_info) return old_handle_exception(self, job, *exc_info, **kwargs) diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index ffd6f458e1..e445b588be 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -254,11 +254,6 @@ def test_traces_sampler_gets_correct_values_in_sampling_context( @pytest.mark.skipif( parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required" ) -@pytest.mark.skipif( - parse_version(rq.__version__) >= (2,), - reason="Test broke in RQ 2.0. Investigate and fix. " - "See https://github.com/getsentry/sentry-python/issues/3707.", -) def test_job_with_retries(sentry_init, capture_events): sentry_init(integrations=[RqIntegration()]) events = capture_events() From aa6e8fd05ca5812213c96cdaf125ab3ae23726f8 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:32:32 +0100 Subject: [PATCH 15/22] fix(falcon): Don't exhaust request body stream (#3768) Only read the cached `request._media`, since reading `request.media` will exhaust the `request.bounded_stream` if it has not been read before. Note that this means that we will now only send the JSON request body to Sentry if the Falcon request handler reads the JSON data. Fixes #3761 Co-authored-by: Anton Pirker --- sentry_sdk/integrations/falcon.py | 44 ++++++++++++----------- tests/integrations/falcon/test_falcon.py | 45 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 00ac106e15..ce771d16e7 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -43,6 +43,12 @@ FALCON3 = False +_FALCON_UNSET = None # type: Optional[object] +if FALCON3: # falcon.request._UNSET is only available in Falcon 3.0+ + with capture_internal_exceptions(): + from falcon.request import _UNSET as _FALCON_UNSET # type: ignore[import-not-found, no-redef] + + class FalconRequestExtractor(RequestExtractor): def env(self): # type: () -> Dict[str, Any] @@ -73,27 +79,23 @@ def raw_data(self): else: return None - if FALCON3: - - def json(self): - # type: () -> Optional[Dict[str, Any]] - try: - return self.request.media - except falcon.errors.HTTPBadRequest: - return None - - else: - - def json(self): - # type: () -> Optional[Dict[str, Any]] - try: - return self.request.media - except falcon.errors.HTTPBadRequest: - # NOTE(jmagnusson): We return `falcon.Request._media` here because - # falcon 1.4 doesn't do proper type checking in - # `falcon.Request.media`. This has been fixed in 2.0. - # Relevant code: https://github.com/falconry/falcon/blob/1.4.1/falcon/request.py#L953 - return self.request._media + def json(self): + # type: () -> Optional[Dict[str, Any]] + # fallback to cached_media = None if self.request._media is not available + cached_media = None + with capture_internal_exceptions(): + # self.request._media is the cached self.request.media + # value. It is only available if self.request.media + # has already been accessed. Therefore, reading + # self.request._media will not exhaust the raw request + # stream (self.request.bounded_stream) because it has + # already been read if self.request._media is set. + cached_media = self.request._media + + if cached_media is not _FALCON_UNSET: + return cached_media + + return None class SentryFalconMiddleware: diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py index 0607d3fdeb..51a1d94334 100644 --- a/tests/integrations/falcon/test_falcon.py +++ b/tests/integrations/falcon/test_falcon.py @@ -460,3 +460,48 @@ def test_span_origin(sentry_init, capture_events, make_client): (_, event) = events assert event["contexts"]["trace"]["origin"] == "auto.http.falcon" + + +def test_falcon_request_media(sentry_init): + # test_passed stores whether the test has passed. + test_passed = False + + # test_failure_reason stores the reason why the test failed + # if test_passed is False. The value is meaningless when + # test_passed is True. + test_failure_reason = "test endpoint did not get called" + + class SentryCaptureMiddleware: + def process_request(self, _req, _resp): + # This capture message forces Falcon event processors to run + # before the request handler runs + sentry_sdk.capture_message("Processing request") + + class RequestMediaResource: + def on_post(self, req, _): + nonlocal test_passed, test_failure_reason + raw_data = req.bounded_stream.read() + + # If the raw_data is empty, the request body stream + # has been exhausted by the SDK. Test should fail in + # this case. + test_passed = raw_data != b"" + test_failure_reason = "request body has been read" + + sentry_init(integrations=[FalconIntegration()]) + + try: + app_class = falcon.App # Falcon ≥3.0 + except AttributeError: + app_class = falcon.API # Falcon <3.0 + + app = app_class(middleware=[SentryCaptureMiddleware()]) + app.add_route("/read_body", RequestMediaResource()) + + client = falcon.testing.TestClient(app) + + client.simulate_post("/read_body", json={"foo": "bar"}) + + # Check that simulate_post actually calls the resource, and + # that the SDK does not exhaust the request body stream. + assert test_passed, test_failure_reason From e9ec6c1812b3c4c0bebdfb736869c1f6a226dc71 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Thu, 21 Nov 2024 12:46:47 +0100 Subject: [PATCH 16/22] test(gcp): Only run GCP tests when they should (#3721) GCP tests have been running in our common test suite, including on Python versions other than 3.7 (the only version which supports the GCP integration), even though we have a separate `py3.7-gcp` tox environment for these tests. The tests take a long time, so only executing in the appropriate `tox` environment should speed up CI time. Co-authored-by: Anton Pirker --- tests/integrations/gcp/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/integrations/gcp/__init__.py diff --git a/tests/integrations/gcp/__init__.py b/tests/integrations/gcp/__init__.py new file mode 100644 index 0000000000..eaf1ba89bb --- /dev/null +++ b/tests/integrations/gcp/__init__.py @@ -0,0 +1,6 @@ +import pytest +import os + + +if "gcp" not in os.environ.get("TOX_ENV_NAME", ""): + pytest.skip("GCP tests only run in GCP environment", allow_module_level=True) From bd50c386527f0d014e2e3c5dea274f6836e713e6 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:00:16 +0100 Subject: [PATCH 17/22] fix(httpx): Prevent Sentry baggage duplication (#3728) Sentry baggage will get added to an HTTPX request multiple times if the same request is repeated. To prevent this from occurring, we can strip any existing Sentry baggage before adding Sentry baggage to the request. Fixes #3709 --------- Co-authored-by: Ivana Kellyer Co-authored-by: Anton Pirker --- sentry_sdk/integrations/httpx.py | 29 +++++++++++++++++++++++------ sentry_sdk/tracing_utils.py | 15 +++++++++++++++ tests/test_tracing_utils.py | 23 ++++++++++++++++++++++- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/httpx.py b/sentry_sdk/integrations/httpx.py index 6f80b93f4d..2ddd44489f 100644 --- a/sentry_sdk/integrations/httpx.py +++ b/sentry_sdk/integrations/httpx.py @@ -2,7 +2,7 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.tracing import BAGGAGE_HEADER_NAME -from sentry_sdk.tracing_utils import should_propagate_trace +from sentry_sdk.tracing_utils import Baggage, should_propagate_trace from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, capture_internal_exceptions, @@ -14,6 +14,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: + from collections.abc import MutableMapping from typing import Any @@ -76,11 +77,9 @@ def send(self, request, **kwargs): key=key, value=value, url=request.url ) ) - if key == BAGGAGE_HEADER_NAME and request.headers.get( - BAGGAGE_HEADER_NAME - ): - # do not overwrite any existing baggage, just append to it - request.headers[key] += "," + value + + if key == BAGGAGE_HEADER_NAME: + _add_sentry_baggage_to_headers(request.headers, value) else: request.headers[key] = value @@ -148,3 +147,21 @@ async def send(self, request, **kwargs): return rv AsyncClient.send = send + + +def _add_sentry_baggage_to_headers(headers, sentry_baggage): + # type: (MutableMapping[str, str], str) -> None + """Add the Sentry baggage to the headers. + + This function directly mutates the provided headers. The provided sentry_baggage + is appended to the existing baggage. If the baggage already contains Sentry items, + they are stripped out first. + """ + existing_baggage = headers.get(BAGGAGE_HEADER_NAME, "") + stripped_existing_baggage = Baggage.strip_sentry_baggage(existing_baggage) + + separator = "," if len(stripped_existing_baggage) > 0 else "" + + headers[BAGGAGE_HEADER_NAME] = ( + stripped_existing_baggage + separator + sentry_baggage + ) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 150e73661e..0459563776 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -609,6 +609,21 @@ def serialize(self, include_third_party=False): return ",".join(items) + @staticmethod + def strip_sentry_baggage(header): + # type: (str) -> str + """Remove Sentry baggage from the given header. + + Given a Baggage header, return a new Baggage header with all Sentry baggage items removed. + """ + return ",".join( + ( + item + for item in header.split(",") + if not Baggage.SENTRY_PREFIX_REGEX.match(item.strip()) + ) + ) + def should_propagate_trace(client, url): # type: (sentry_sdk.client.BaseClient, str) -> bool diff --git a/tests/test_tracing_utils.py b/tests/test_tracing_utils.py index 239e631156..5c1f70516d 100644 --- a/tests/test_tracing_utils.py +++ b/tests/test_tracing_utils.py @@ -1,7 +1,7 @@ from dataclasses import asdict, dataclass from typing import Optional, List -from sentry_sdk.tracing_utils import _should_be_included +from sentry_sdk.tracing_utils import _should_be_included, Baggage import pytest @@ -94,3 +94,24 @@ def test_should_be_included(test_case, expected): kwargs = asdict(test_case) kwargs.pop("id") assert _should_be_included(**kwargs) == expected + + +@pytest.mark.parametrize( + ("header", "expected"), + ( + ("", ""), + ("foo=bar", "foo=bar"), + (" foo=bar, baz = qux ", " foo=bar, baz = qux "), + ("sentry-trace_id=123", ""), + (" sentry-trace_id = 123 ", ""), + ("sentry-trace_id=123,sentry-public_key=456", ""), + ("foo=bar,sentry-trace_id=123", "foo=bar"), + ("foo=bar,sentry-trace_id=123,baz=qux", "foo=bar,baz=qux"), + ( + "foo=bar,sentry-trace_id=123,baz=qux,sentry-public_key=456", + "foo=bar,baz=qux", + ), + ), +) +def test_strip_sentry_baggage(header, expected): + assert Baggage.strip_sentry_baggage(header) == expected From 295dd8d50fc161c79db7249d228f87d79bb5bd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20Nouvertn=C3=A9?= Date: Thu, 21 Nov 2024 13:02:49 +0100 Subject: [PATCH 18/22] Auto enable Litestar integration (#3540) Auto enable the Litestar integration added in #3358. --------- Co-authored-by: Ivana Kellyer Co-authored-by: Anton Pirker --- sentry_sdk/integrations/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index 32528246af..12336a939b 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -95,6 +95,7 @@ def iter_default_integrations(with_auto_enabling_integrations): "sentry_sdk.integrations.huey.HueyIntegration", "sentry_sdk.integrations.huggingface_hub.HuggingfaceHubIntegration", "sentry_sdk.integrations.langchain.LangchainIntegration", + "sentry_sdk.integrations.litestar.LitestarIntegration", "sentry_sdk.integrations.loguru.LoguruIntegration", "sentry_sdk.integrations.openai.OpenAIIntegration", "sentry_sdk.integrations.pymongo.PyMongoIntegration", From 8fe5bb4b1946874f61bfc09dcce327e20bb24519 Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Thu, 21 Nov 2024 15:20:56 +0000 Subject: [PATCH 19/22] feat: Send PII to Spotlight when no DSN is set (#3804) * feat: Send PII to Spotlight when no DSN is set Quick fix for getsentry/spotlight#543 until we implement a global scrubber that only scrubs events sent to the clound thorugh the DSN. * add tests fix bugs * Make scrubber initialization more explicit * Refactored to not change the default value of send_default_pii * Add test to show that there is now no way to opt out of sending PII to spotlight. * Revert "Refactored to not change the default value of send_default_pii" This reverts commit 15cf625859852b0a51c70f8126ad92af6d947d48. * Revert "Add test to show that there is now no way to opt out of sending PII to spotlight." This reverts commit de7f39818af78a1012a8fcea6bbd80f20c6b0eb3. --------- Co-authored-by: Anton Pirker --- sentry_sdk/client.py | 12 ++++++++++-- sentry_sdk/consts.py | 3 ++- tests/test_scope.py | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sentry_sdk/client.py b/sentry_sdk/client.py index b1e7868031..db2cc19110 100644 --- a/sentry_sdk/client.py +++ b/sentry_sdk/client.py @@ -128,7 +128,11 @@ def _get_options(*args, **kwargs): rv["traces_sample_rate"] = 1.0 if rv["event_scrubber"] is None: - rv["event_scrubber"] = EventScrubber(send_default_pii=rv["send_default_pii"]) + rv["event_scrubber"] = EventScrubber( + send_default_pii=( + False if rv["send_default_pii"] is None else rv["send_default_pii"] + ) + ) if rv["socket_options"] and not isinstance(rv["socket_options"], list): logger.warning( @@ -451,7 +455,11 @@ def should_send_default_pii(self): Returns whether the client should send default PII (Personally Identifiable Information) data to Sentry. """ - return self.options.get("send_default_pii", False) + result = self.options.get("send_default_pii") + if result is None: + result = not self.options["dsn"] and self.spotlight is not None + + return result @property def dsn(self): diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index ae32294d05..bb2a73337e 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -489,6 +489,7 @@ class OP: # This type exists to trick mypy and PyCharm into thinking `init` and `Client` # take these arguments (even though they take opaque **kwargs) class ClientConstructor: + def __init__( self, dsn=None, # type: Optional[str] @@ -506,7 +507,7 @@ def __init__( transport=None, # type: Optional[Union[sentry_sdk.transport.Transport, Type[sentry_sdk.transport.Transport], Callable[[Event], None]]] transport_queue_size=DEFAULT_QUEUE_SIZE, # type: int sample_rate=1.0, # type: float - send_default_pii=False, # type: bool + send_default_pii=None, # type: Optional[bool] http_proxy=None, # type: Optional[str] https_proxy=None, # type: Optional[str] ignore_errors=[], # type: Sequence[Union[type, str]] # noqa: B006 diff --git a/tests/test_scope.py b/tests/test_scope.py index 0dfa155d11..374a354446 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -811,6 +811,24 @@ def test_should_send_default_pii_false(sentry_init): assert should_send_default_pii() is False +def test_should_send_default_pii_default_false(sentry_init): + sentry_init() + + assert should_send_default_pii() is False + + +def test_should_send_default_pii_false_with_dsn_and_spotlight(sentry_init): + sentry_init(dsn="http://key@localhost/1", spotlight=True) + + assert should_send_default_pii() is False + + +def test_should_send_default_pii_true_without_dsn_and_spotlight(sentry_init): + sentry_init(spotlight=True) + + assert should_send_default_pii() is True + + def test_set_tags(): scope = Scope() scope.set_tags({"tag1": "value1", "tag2": "value2"}) From c83e7428f44263e6d62ab88cb61034e7f438b2b4 Mon Sep 17 00:00:00 2001 From: getsentry-bot Date: Thu, 21 Nov 2024 15:22:15 +0000 Subject: [PATCH 20/22] release: 2.19.0 --- CHANGELOG.md | 26 ++++++++++++++++++++++++++ docs/conf.py | 2 +- sentry_sdk/consts.py | 2 +- setup.py | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c47d0e0458..dab245e15a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,31 @@ # Changelog +## 2.19.0 + +### Various fixes & improvements + +- feat: Send PII to Spotlight when no DSN is set (#3804) by @BYK +- Auto enable Litestar integration (#3540) by @provinzkraut +- fix(httpx): Prevent Sentry baggage duplication (#3728) by @szokeasaurusrex +- test(gcp): Only run GCP tests when they should (#3721) by @szokeasaurusrex +- fix(falcon): Don't exhaust request body stream (#3768) by @szokeasaurusrex +- fix(integrations): Check retries_left before capturing exception (#3803) by @malkovro +- fix(openai): Use name instead of description (#3807) by @sourceful-rob +- Shorten CI workflow names (#3805) by @sentrivana +- build(deps): bump codecov/codecov-action from 4.6.0 to 5.0.2 (#3792) by @dependabot +- feat(spotlight): Auto enable cache_spans for Spotlight on DEBUG (#3791) by @BYK +- Make sentry-sdk[pure-eval] installable with pip==24.0 (#3757) by @sentrivana +- tests: Test with pyspark prerelease (#3760) by @sentrivana +- fix: include_tracing_fields arg to control unvetted data in rust_tracing integration (#3780) by @matt-codecov +- feat: introduce rust_tracing integration (#3717) by @matt-codecov +- Fix aws lambda tests (by reducing event size) (#3770) by @antonpirker +- feat(spotlight): Inject Spotlight button on Django (#3751) by @BYK +- ref(init): Deprecate `sentry_sdk.init` context manager (#3729) by @szokeasaurusrex +- Handle parameter `stack_info` for the `LoggingIntegration` (#3745) by @gmcrocetti +- Fix(Arq): fix integration with Worker settings as a dict (#3742) by @saber-solooki +- feat(spotlight): Add info logs when Sentry is enabled (#3735) by @BYK +- build(deps): bump actions/checkout from 4.2.1 to 4.2.2 (#3691) by @dependabot + ## 2.18.0 ### Various fixes & improvements diff --git a/docs/conf.py b/docs/conf.py index 6d33e5809a..55d5295381 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -31,7 +31,7 @@ copyright = "2019-{}, Sentry Team and Contributors".format(datetime.now().year) author = "Sentry Team and Contributors" -release = "2.18.0" +release = "2.19.0" version = ".".join(release.split(".")[:2]) # The short X.Y version. diff --git a/sentry_sdk/consts.py b/sentry_sdk/consts.py index bb2a73337e..488743b579 100644 --- a/sentry_sdk/consts.py +++ b/sentry_sdk/consts.py @@ -576,4 +576,4 @@ def _get_default_options(): del _get_default_options -VERSION = "2.18.0" +VERSION = "2.19.0" diff --git a/setup.py b/setup.py index 29a40c6663..fda3daa229 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def get_file_text(file_name): setup( name="sentry-sdk", - version="2.18.0", + version="2.19.0", author="Sentry Team and Contributors", author_email="hello@sentry.io", url="https://github.com/getsentry/sentry-python", From 039c220bcb5208b278bc1cd0b08611bdac26b895 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 21 Nov 2024 16:31:18 +0100 Subject: [PATCH 21/22] Updated changelog --- CHANGELOG.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dab245e15a..dbb35eb1eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,26 +4,26 @@ ### Various fixes & improvements -- feat: Send PII to Spotlight when no DSN is set (#3804) by @BYK +- New: introduce `rust_tracing` integration. See https://docs.sentry.io/platforms/python/integrations/rust_tracing/ (#3717) by @matt-codecov - Auto enable Litestar integration (#3540) by @provinzkraut +- Deprecate `sentry_sdk.init` context manager (#3729) by @szokeasaurusrex +- feat(spotlight): Send PII to Spotlight when no DSN is set (#3804) by @BYK +- feat(spotlight): Add info logs when Sentry is enabled (#3735) by @BYK +- feat(spotlight): Inject Spotlight button on Django (#3751) by @BYK +- feat(spotlight): Auto enable cache_spans for Spotlight on DEBUG (#3791) by @BYK +- fix(logging): Handle parameter `stack_info` for the `LoggingIntegration` (#3745) by @gmcrocetti +- fix(pure-eval): Make sentry-sdk[pure-eval] installable with pip==24.0 (#3757) by @sentrivana +- fix(rust_tracing): include_tracing_fields arg to control unvetted data in rust_tracing integration (#3780) by @matt-codecov +- fix(aws) Fix aws lambda tests (by reducing event size) (#3770) by @antonpirker +- fix(arq): fix integration with Worker settings as a dict (#3742) by @saber-solooki - fix(httpx): Prevent Sentry baggage duplication (#3728) by @szokeasaurusrex -- test(gcp): Only run GCP tests when they should (#3721) by @szokeasaurusrex - fix(falcon): Don't exhaust request body stream (#3768) by @szokeasaurusrex -- fix(integrations): Check retries_left before capturing exception (#3803) by @malkovro +- fix(integrations): Check `retries_left` before capturing exception (#3803) by @malkovro - fix(openai): Use name instead of description (#3807) by @sourceful-rob -- Shorten CI workflow names (#3805) by @sentrivana +- test(gcp): Only run GCP tests when they should (#3721) by @szokeasaurusrex +- chore: Shorten CI workflow names (#3805) by @sentrivana +- chore: Test with pyspark prerelease (#3760) by @sentrivana - build(deps): bump codecov/codecov-action from 4.6.0 to 5.0.2 (#3792) by @dependabot -- feat(spotlight): Auto enable cache_spans for Spotlight on DEBUG (#3791) by @BYK -- Make sentry-sdk[pure-eval] installable with pip==24.0 (#3757) by @sentrivana -- tests: Test with pyspark prerelease (#3760) by @sentrivana -- fix: include_tracing_fields arg to control unvetted data in rust_tracing integration (#3780) by @matt-codecov -- feat: introduce rust_tracing integration (#3717) by @matt-codecov -- Fix aws lambda tests (by reducing event size) (#3770) by @antonpirker -- feat(spotlight): Inject Spotlight button on Django (#3751) by @BYK -- ref(init): Deprecate `sentry_sdk.init` context manager (#3729) by @szokeasaurusrex -- Handle parameter `stack_info` for the `LoggingIntegration` (#3745) by @gmcrocetti -- Fix(Arq): fix integration with Worker settings as a dict (#3742) by @saber-solooki -- feat(spotlight): Add info logs when Sentry is enabled (#3735) by @BYK - build(deps): bump actions/checkout from 4.2.1 to 4.2.2 (#3691) by @dependabot ## 2.18.0 From da206237473aeb38d911d9cd86f40bd928a2a350 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Mon, 25 Nov 2024 10:04:43 +0100 Subject: [PATCH 22/22] Fix spans for streaming responses in WSGI based frameworks (#3798) Fixes spans in streaming responses when using WSGI based frameworks. Only close the transaction once the response was consumed. This way all the spans created during creation of the response will be recorded with the transaction: - The transaction stays open until all the streaming blocks are sent to the client. (because of this I had to update the tests, to make sure the tests, consume the response, because the Werkzeug test client (used by Flask and Django and our Strawberry tests) will not close the WSGI response) - A maximum runtime of 5 minutes for transactions is enforced. (like Javascript does it) - When using a generator to generate the streaming response, it uses the correct scopes to have correct parent-child relationship of spans created in the generator. People having Sentry in a streaming application will: - See an increase in their transaction duration to up to 5 minutes - Get the correct span tree for streaming responses generated by a generator Fixes #3736 --- sentry_sdk/integrations/wsgi.py | 135 ++++++++++++------ sentry_sdk/tracing_utils.py | 18 +++ tests/integrations/django/test_basic.py | 46 +++--- tests/integrations/flask/test_flask.py | 22 ++- .../strawberry/test_strawberry.py | 43 ++++-- tests/integrations/wsgi/test_wsgi.py | 79 ++++++++++ 6 files changed, 270 insertions(+), 73 deletions(-) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 50deae10c5..751735f462 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -1,19 +1,19 @@ import sys from functools import partial +from threading import Timer import sentry_sdk from sentry_sdk._werkzeug import get_host, _get_headers from sentry_sdk.api import continue_trace from sentry_sdk.consts import OP -from sentry_sdk.scope import should_send_default_pii +from sentry_sdk.scope import should_send_default_pii, use_isolation_scope, use_scope from sentry_sdk.integrations._wsgi_common import ( DEFAULT_HTTP_METHODS_TO_CAPTURE, _filter_headers, - nullcontext, ) 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.tracing_utils import finish_running_transaction from sentry_sdk.utils import ( ContextVar, capture_internal_exceptions, @@ -46,6 +46,9 @@ def __call__(self, status, response_headers, exc_info=None): # type: ignore pass +MAX_TRANSACTION_DURATION_SECONDS = 5 * 60 + + _wsgi_middleware_applied = ContextVar("sentry_wsgi_middleware_applied") @@ -98,6 +101,7 @@ def __call__(self, environ, start_response): _wsgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as scope: + current_scope = sentry_sdk.get_current_scope() with track_session(scope, session_mode="request"): with capture_internal_exceptions(): scope.clear_breadcrumbs() @@ -109,6 +113,7 @@ def __call__(self, environ, start_response): ) method = environ.get("REQUEST_METHOD", "").upper() + transaction = None if method in self.http_methods_to_capture: transaction = continue_trace( @@ -119,27 +124,43 @@ def __call__(self, environ, start_response): origin=self.span_origin, ) - with ( + timer = None + if transaction is not None: sentry_sdk.start_transaction( transaction, custom_sampling_context={"wsgi_environ": environ}, + ).__enter__() + timer = Timer( + MAX_TRANSACTION_DURATION_SECONDS, + _finish_long_running_transaction, + args=(current_scope, scope), ) - if transaction is not None - else nullcontext() - ): - try: - response = self.app( - environ, - partial( - _sentry_start_response, start_response, transaction - ), - ) - except BaseException: - reraise(*_capture_exception()) + timer.start() + + try: + response = self.app( + environ, + partial( + _sentry_start_response, + start_response, + transaction, + ), + ) + except BaseException: + exc_info = sys.exc_info() + _capture_exception(exc_info) + finish_running_transaction(current_scope, exc_info, timer) + reraise(*exc_info) + finally: _wsgi_middleware_applied.set(False) - return _ScopedResponse(scope, response) + return _ScopedResponse( + response=response, + current_scope=current_scope, + isolation_scope=scope, + timer=timer, + ) def _sentry_start_response( # type: ignore @@ -201,13 +222,13 @@ def get_client_ip(environ): return environ.get("REMOTE_ADDR") -def _capture_exception(): - # type: () -> ExcInfo +def _capture_exception(exc_info=None): + # type: (Optional[ExcInfo]) -> ExcInfo """ Captures the current exception and sends it to Sentry. Returns the ExcInfo tuple to it can be reraised afterwards. """ - exc_info = sys.exc_info() + exc_info = exc_info or sys.exc_info() e = exc_info[1] # SystemExit(0) is the only uncaught exception that is expected behavior @@ -225,7 +246,7 @@ def _capture_exception(): class _ScopedResponse: """ - Users a separate scope for each response chunk. + Use separate scopes for each response chunk. This will make WSGI apps more tolerant against: - WSGI servers streaming responses from a different thread/from @@ -234,37 +255,54 @@ class _ScopedResponse: - WSGI servers streaming responses interleaved from the same thread """ - __slots__ = ("_response", "_scope") + __slots__ = ("_response", "_current_scope", "_isolation_scope", "_timer") - def __init__(self, scope, response): - # type: (sentry_sdk.scope.Scope, Iterator[bytes]) -> None - self._scope = scope + def __init__( + self, + response, # type: Iterator[bytes] + current_scope, # type: sentry_sdk.scope.Scope + isolation_scope, # type: sentry_sdk.scope.Scope + timer=None, # type: Optional[Timer] + ): + # type: (...) -> None self._response = response + self._current_scope = current_scope + self._isolation_scope = isolation_scope + self._timer = timer def __iter__(self): # type: () -> Iterator[bytes] iterator = iter(self._response) - while True: - with use_isolation_scope(self._scope): - try: - chunk = next(iterator) - except StopIteration: - break - except BaseException: - reraise(*_capture_exception()) + try: + while True: + with use_isolation_scope(self._isolation_scope): + with use_scope(self._current_scope): + try: + chunk = next(iterator) + except StopIteration: + break + except BaseException: + reraise(*_capture_exception()) + + yield chunk - yield chunk + finally: + with use_isolation_scope(self._isolation_scope): + with use_scope(self._current_scope): + finish_running_transaction(timer=self._timer) def close(self): # type: () -> None - with use_isolation_scope(self._scope): - try: - self._response.close() # type: ignore - except AttributeError: - pass - except BaseException: - reraise(*_capture_exception()) + with use_isolation_scope(self._isolation_scope): + with use_scope(self._current_scope): + try: + finish_running_transaction(timer=self._timer) + self._response.close() # type: ignore + except AttributeError: + pass + except BaseException: + reraise(*_capture_exception()) def _make_wsgi_event_processor(environ, use_x_forwarded_for): @@ -308,3 +346,18 @@ def event_processor(event, hint): return event return event_processor + + +def _finish_long_running_transaction(current_scope, isolation_scope): + # type: (sentry_sdk.scope.Scope, sentry_sdk.scope.Scope) -> None + """ + Make sure we don't keep transactions open for too long. + Triggered after MAX_TRANSACTION_DURATION_SECONDS have passed. + """ + try: + with use_isolation_scope(isolation_scope): + with use_scope(current_scope): + finish_running_transaction() + except AttributeError: + # transaction is not there anymore + pass diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 0459563776..969e0812e4 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -36,6 +36,9 @@ from types import FrameType + from sentry_sdk._types import ExcInfo + from threading import Timer + SENTRY_TRACE_REGEX = re.compile( "^[ \t]*" # whitespace @@ -739,3 +742,18 @@ def get_current_span(scope=None): if TYPE_CHECKING: from sentry_sdk.tracing import Span + + +def finish_running_transaction(scope=None, exc_info=None, timer=None): + # type: (Optional[sentry_sdk.Scope], Optional[ExcInfo], Optional[Timer]) -> None + if timer is not None: + timer.cancel() + + current_scope = scope or sentry_sdk.get_current_scope() + if current_scope.transaction is not None and hasattr( + current_scope.transaction, "_context_manager_state" + ): + if exc_info is not None: + current_scope.transaction.__exit__(*exc_info) + else: + current_scope.transaction.__exit__(None, None, None) diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 0e3f700105..243431fdf5 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -51,7 +51,7 @@ def test_view_exceptions(sentry_init, client, capture_exceptions, capture_events sentry_init(integrations=[DjangoIntegration()], send_default_pii=True) exceptions = capture_exceptions() events = capture_events() - client.get(reverse("view_exc")) + unpack_werkzeug_response(client.get(reverse("view_exc"))) (error,) = exceptions assert isinstance(error, ZeroDivisionError) @@ -72,7 +72,9 @@ def test_ensures_x_forwarded_header_is_honored_in_sdk_when_enabled_in_django( sentry_init(integrations=[DjangoIntegration()], send_default_pii=True) exceptions = capture_exceptions() events = capture_events() - client.get(reverse("view_exc"), headers={"X_FORWARDED_HOST": "example.com"}) + unpack_werkzeug_response( + client.get(reverse("view_exc"), headers={"X_FORWARDED_HOST": "example.com"}) + ) (error,) = exceptions assert isinstance(error, ZeroDivisionError) @@ -91,7 +93,9 @@ def test_ensures_x_forwarded_header_is_not_honored_when_unenabled_in_django( sentry_init(integrations=[DjangoIntegration()], send_default_pii=True) exceptions = capture_exceptions() events = capture_events() - client.get(reverse("view_exc"), headers={"X_FORWARDED_HOST": "example.com"}) + unpack_werkzeug_response( + client.get(reverse("view_exc"), headers={"X_FORWARDED_HOST": "example.com"}) + ) (error,) = exceptions assert isinstance(error, ZeroDivisionError) @@ -103,7 +107,7 @@ def test_ensures_x_forwarded_header_is_not_honored_when_unenabled_in_django( def test_middleware_exceptions(sentry_init, client, capture_exceptions): sentry_init(integrations=[DjangoIntegration()], send_default_pii=True) exceptions = capture_exceptions() - client.get(reverse("middleware_exc")) + unpack_werkzeug_response(client.get(reverse("middleware_exc"))) (error,) = exceptions assert isinstance(error, ZeroDivisionError) @@ -157,7 +161,7 @@ def test_has_trace_if_performance_enabled(sentry_init, client, capture_events): traces_sample_rate=1.0, ) events = capture_events() - client.head(reverse("view_exc_with_msg")) + unpack_werkzeug_response(client.head(reverse("view_exc_with_msg"))) (msg_event, error_event, transaction_event) = events @@ -213,8 +217,10 @@ def test_trace_from_headers_if_performance_enabled(sentry_init, client, capture_ trace_id = "582b43a4192642f0b136d5159a501701" sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1) - client.head( - reverse("view_exc_with_msg"), headers={"sentry-trace": sentry_trace_header} + unpack_werkzeug_response( + client.head( + reverse("view_exc_with_msg"), headers={"sentry-trace": sentry_trace_header} + ) ) (msg_event, error_event, transaction_event) = events @@ -928,7 +934,7 @@ def test_render_spans(sentry_init, client, capture_events, render_span_tree): for url, expected_line in views_tests: events = capture_events() - client.get(url) + unpack_werkzeug_response(client.get(url)) transaction = events[0] assert expected_line in render_span_tree(transaction) @@ -967,7 +973,7 @@ def test_middleware_spans(sentry_init, client, capture_events, render_span_tree) ) events = capture_events() - client.get(reverse("message")) + unpack_werkzeug_response(client.get(reverse("message"))) message, transaction = events @@ -984,7 +990,7 @@ def test_middleware_spans_disabled(sentry_init, client, capture_events): ) events = capture_events() - client.get(reverse("message")) + unpack_werkzeug_response(client.get(reverse("message"))) message, transaction = events @@ -1008,7 +1014,7 @@ def test_signals_spans(sentry_init, client, capture_events, render_span_tree): ) events = capture_events() - client.get(reverse("message")) + unpack_werkzeug_response(client.get(reverse("message"))) message, transaction = events @@ -1031,7 +1037,7 @@ def test_signals_spans_disabled(sentry_init, client, capture_events): ) events = capture_events() - client.get(reverse("message")) + unpack_werkzeug_response(client.get(reverse("message"))) message, transaction = events @@ -1061,7 +1067,7 @@ def test_signals_spans_filtering(sentry_init, client, capture_events, render_spa ) events = capture_events() - client.get(reverse("send_myapp_custom_signal")) + unpack_werkzeug_response(client.get(reverse("send_myapp_custom_signal"))) (transaction,) = events @@ -1186,7 +1192,7 @@ def test_span_origin(sentry_init, client, capture_events): ) events = capture_events() - client.get(reverse("view_with_signal")) + unpack_werkzeug_response(client.get(reverse("view_with_signal"))) (transaction,) = events @@ -1211,9 +1217,9 @@ def test_transaction_http_method_default(sentry_init, client, capture_events): ) events = capture_events() - client.get("/nomessage") - client.options("/nomessage") - client.head("/nomessage") + unpack_werkzeug_response(client.get("/nomessage")) + unpack_werkzeug_response(client.options("/nomessage")) + unpack_werkzeug_response(client.head("/nomessage")) (event,) = events @@ -1235,9 +1241,9 @@ def test_transaction_http_method_custom(sentry_init, client, capture_events): ) events = capture_events() - client.get("/nomessage") - client.options("/nomessage") - client.head("/nomessage") + unpack_werkzeug_response(client.get("/nomessage")) + unpack_werkzeug_response(client.options("/nomessage")) + unpack_werkzeug_response(client.head("/nomessage")) assert len(events) == 2 diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 6febb12b8b..e2c37aa5f7 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -394,6 +394,8 @@ def index(): client = app.test_client() response = client.post("/", data=data) assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() event, transaction_event = events @@ -746,6 +748,8 @@ def hi_tx(): with app.test_client() as client: response = client.get("/message_tx") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() message_event, transaction_event = events @@ -938,7 +942,9 @@ def test_response_status_code_not_found_in_transaction_context( envelopes = capture_envelopes() client = app.test_client() - client.get("/not-existing-route") + response = client.get("/not-existing-route") + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() sentry_sdk.get_client().flush() @@ -983,14 +989,21 @@ def test_transaction_http_method_default( events = capture_events() client = app.test_client() + response = client.get("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() response = client.options("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() response = client.head("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() (event,) = events @@ -1020,14 +1033,21 @@ def test_transaction_http_method_custom( events = capture_events() client = app.test_client() + response = client.get("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() response = client.options("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() response = client.head("/nomessage") assert response.status_code == 200 + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + response.close() assert len(events) == 2 diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index 7b40b238d2..0aab78f443 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -198,7 +198,10 @@ def test_capture_request_if_available_and_send_pii_is_on( client = client_factory(schema) query = "query ErrorQuery { error }" - client.post("/graphql", json={"query": query, "operationName": "ErrorQuery"}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post( + "/graphql", json={"query": query, "operationName": "ErrorQuery"} + ).close() assert len(events) == 1 @@ -253,7 +256,10 @@ def test_do_not_capture_request_if_send_pii_is_off( client = client_factory(schema) query = "query ErrorQuery { error }" - client.post("/graphql", json={"query": query, "operationName": "ErrorQuery"}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post( + "/graphql", json={"query": query, "operationName": "ErrorQuery"} + ).close() assert len(events) == 1 @@ -293,7 +299,8 @@ def test_breadcrumb_no_operation_name( client = client_factory(schema) query = "{ error }" - client.post("/graphql", json={"query": query}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={"query": query}).close() assert len(events) == 1 @@ -332,7 +339,10 @@ def test_capture_transaction_on_error( client = client_factory(schema) query = "query ErrorQuery { error }" - client.post("/graphql", json={"query": query, "operationName": "ErrorQuery"}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post( + "/graphql", json={"query": query, "operationName": "ErrorQuery"} + ).close() assert len(events) == 2 (_, transaction_event) = events @@ -409,7 +419,10 @@ def test_capture_transaction_on_success( client = client_factory(schema) query = "query GreetingQuery { hello }" - client.post("/graphql", json={"query": query, "operationName": "GreetingQuery"}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post( + "/graphql", json={"query": query, "operationName": "GreetingQuery"} + ).close() assert len(events) == 1 (transaction_event,) = events @@ -486,7 +499,8 @@ def test_transaction_no_operation_name( client = client_factory(schema) query = "{ hello }" - client.post("/graphql", json={"query": query}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={"query": query}).close() assert len(events) == 1 (transaction_event,) = events @@ -566,7 +580,8 @@ def test_transaction_mutation( client = client_factory(schema) query = 'mutation Change { change(attribute: "something") }' - client.post("/graphql", json={"query": query}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={"query": query}).close() assert len(events) == 1 (transaction_event,) = events @@ -641,7 +656,8 @@ def test_handle_none_query_gracefully( client_factory = request.getfixturevalue(client_factory) client = client_factory(schema) - client.post("/graphql", json={}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={}).close() assert len(events) == 0, "expected no events to be sent to Sentry" @@ -673,7 +689,8 @@ def test_span_origin( client = client_factory(schema) query = 'mutation Change { change(attribute: "something") }' - client.post("/graphql", json={"query": query}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={"query": query}).close() (event,) = events @@ -715,7 +732,10 @@ def test_span_origin2( client = client_factory(schema) query = "query GreetingQuery { hello }" - client.post("/graphql", json={"query": query, "operationName": "GreetingQuery"}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post( + "/graphql", json={"query": query, "operationName": "GreetingQuery"} + ).close() (event,) = events @@ -757,7 +777,8 @@ def test_span_origin3( client = client_factory(schema) query = "subscription { messageAdded { content } }" - client.post("/graphql", json={"query": query}) + # Close the response to ensure the WSGI cycle is complete and the transaction is finished + client.post("/graphql", json={"query": query}).close() (event,) = events diff --git a/tests/integrations/wsgi/test_wsgi.py b/tests/integrations/wsgi/test_wsgi.py index 656fc1757f..a4f5ca0623 100644 --- a/tests/integrations/wsgi/test_wsgi.py +++ b/tests/integrations/wsgi/test_wsgi.py @@ -1,7 +1,9 @@ +import time from collections import Counter from unittest import mock import pytest +from sentry_sdk.utils import datetime_from_isoformat from werkzeug.test import Client import sentry_sdk @@ -495,3 +497,80 @@ def dogpark(environ, start_response): (event,) = events assert event["contexts"]["trace"]["origin"] == "auto.dogpark.deluxe" + + +def test_long_running_transaction_finished(sentry_init, capture_events): + # we allow transactions to be 0.5 seconds as a maximum + new_max_duration = 0.5 + + with mock.patch.object( + sentry_sdk.integrations.wsgi, + "MAX_TRANSACTION_DURATION_SECONDS", + new_max_duration, + ): + + def generate_content(): + # This response will take 1.5 seconds to generate + for _ in range(15): + time.sleep(0.1) + yield "ok" + + def long_running_app(environ, start_response): + start_response("200 OK", []) + return generate_content() + + sentry_init(send_default_pii=True, traces_sample_rate=1.0) + app = SentryWsgiMiddleware(long_running_app) + + events = capture_events() + + client = Client(app) + response = client.get("/") + _ = response.get_data() + + (transaction,) = events + + transaction_duration = ( + datetime_from_isoformat(transaction["timestamp"]) + - datetime_from_isoformat(transaction["start_timestamp"]) + ).total_seconds() + assert ( + transaction_duration <= new_max_duration * 1.02 + ) # we allow 2% margin for processing the request + + +def test_long_running_transaction_timer_canceled(sentry_init, capture_events): + # we allow transactions to be 0.5 seconds as a maximum + new_max_duration = 0.5 + + with mock.patch.object( + sentry_sdk.integrations.wsgi, + "MAX_TRANSACTION_DURATION_SECONDS", + new_max_duration, + ): + with mock.patch( + "sentry_sdk.integrations.wsgi._finish_long_running_transaction" + ) as mock_finish: + + def generate_content(): + # This response will take 0.3 seconds to generate + for _ in range(3): + time.sleep(0.1) + yield "ok" + + def long_running_app(environ, start_response): + start_response("200 OK", []) + return generate_content() + + sentry_init(send_default_pii=True, traces_sample_rate=1.0) + app = SentryWsgiMiddleware(long_running_app) + + events = capture_events() + + client = Client(app) + response = client.get("/") + _ = response.get_data() + + (transaction,) = events + + mock_finish.assert_not_called()