diff --git a/sentry_sdk/_init_implementation.py b/sentry_sdk/_init_implementation.py index dc235af243..5be94e1e60 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 diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index b0f9a6a443..5aa0ba7302 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -206,6 +206,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/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/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/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 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") 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: 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") ( 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]" diff --git a/tests/test_api.py b/tests/test_api.py index 46fc24fd24..0c79c035b1 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,