From d4bc0f81b90f97525a7c39399ea25729949eae86 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 15 Jul 2022 13:38:39 +0200 Subject: [PATCH] feat(transactions): Transaction Source (#1490) Added transaction source (plus tests) to the following Integrations: Flask, ASGI, Bottle, Django, Celery, Falcon, Pyramid, Quart, Sanic, Tornado, AIOHTTP, Chalice, GCP, AWS Lambda, --- .pre-commit-config.yaml | 6 +- sentry_sdk/integrations/aiohttp.py | 7 +- sentry_sdk/integrations/asgi.py | 64 ++++++++++----- sentry_sdk/integrations/aws_lambda.py | 7 +- sentry_sdk/integrations/bottle.py | 39 +++++---- sentry_sdk/integrations/celery.py | 8 +- sentry_sdk/integrations/chalice.py | 7 +- sentry_sdk/integrations/django/__init__.py | 56 ++++++++----- sentry_sdk/integrations/falcon.py | 27 +++++-- sentry_sdk/integrations/flask.py | 65 +++++++-------- sentry_sdk/integrations/gcp.py | 7 +- sentry_sdk/integrations/pyramid.py | 35 +++++--- sentry_sdk/integrations/quart.py | 35 +++++--- sentry_sdk/integrations/sanic.py | 14 +++- sentry_sdk/integrations/tornado.py | 3 +- sentry_sdk/scope.py | 30 ++++++- sentry_sdk/tracing.py | 31 +++++++- tests/integrations/aiohttp/test_aiohttp.py | 22 ++++- tests/integrations/asgi/test_asgi.py | 93 ++++++++++++++++++++++ tests/integrations/aws_lambda/test_aws.py | 2 + tests/integrations/bottle/test_bottle.py | 25 ++++-- tests/integrations/celery/test_celery.py | 4 +- tests/integrations/chalice/test_chalice.py | 36 +++++++++ tests/integrations/django/test_basic.py | 14 +++- tests/integrations/falcon/test_falcon.py | 26 +++++- tests/integrations/flask/test_flask.py | 24 +++++- tests/integrations/gcp/test_gcp.py | 1 + tests/integrations/pyramid/test_pyramid.py | 33 ++++++-- tests/integrations/quart/test_quart.py | 26 +++++- tests/integrations/sanic/test_sanic.py | 26 ++++++ tests/integrations/tornado/test_tornado.py | 6 ++ 31 files changed, 613 insertions(+), 166 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 753558186f..3f7e548518 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,18 +2,18 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 + rev: v4.3.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - repo: https://github.com/psf/black - rev: stable + rev: 22.6.0 hooks: - id: black - repo: https://gitlab.com/pycqa/flake8 - rev: 4.0.1 + rev: 3.9.2 hooks: - id: flake8 diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 8a828b2fe3..9f4a823b98 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -9,7 +9,7 @@ _filter_headers, request_body_within_bounds, ) -from sentry_sdk.tracing import Transaction +from sentry_sdk.tracing import SOURCE_FOR_STYLE, Transaction from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -148,7 +148,10 @@ async def sentry_urldispatcher_resolve(self, request): if name is not None: with Hub.current.configure_scope() as scope: - scope.transaction = name + scope.set_transaction_name( + name, + source=SOURCE_FOR_STYLE[integration.transaction_style], + ) return rv diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 5f7810732b..3aa9fcb572 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -13,6 +13,11 @@ from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.integrations._wsgi_common import _filter_headers from sentry_sdk.sessions import auto_session_tracking +from sentry_sdk.tracing import ( + SOURCE_FOR_STYLE, + TRANSACTION_SOURCE_ROUTE, + TRANSACTION_SOURCE_UNKNOWN, +) from sentry_sdk.utils import ( ContextVar, event_from_exception, @@ -147,6 +152,7 @@ async def _run_app(self, scope, callback): transaction = Transaction(op="asgi.server") transaction.name = _DEFAULT_TRANSACTION_NAME + transaction.source = TRANSACTION_SOURCE_ROUTE transaction.set_tag("asgi.type", ty) with hub.start_transaction( @@ -183,25 +189,7 @@ def event_processor(self, event, hint, asgi_scope): if client and _should_send_default_pii(): request_info["env"] = {"REMOTE_ADDR": self._get_ip(asgi_scope)} - if ( - event.get("transaction", _DEFAULT_TRANSACTION_NAME) - == _DEFAULT_TRANSACTION_NAME - ): - if self.transaction_style == "endpoint": - endpoint = asgi_scope.get("endpoint") - # Webframeworks like Starlette mutate the ASGI env once routing is - # done, which is sometime after the request has started. If we have - # an endpoint, overwrite our generic transaction name. - if endpoint: - event["transaction"] = transaction_from_function(endpoint) - elif self.transaction_style == "url": - # FastAPI includes the route object in the scope to let Sentry extract the - # path from it for the transaction name - route = asgi_scope.get("route") - if route: - path = getattr(route, "path", None) - if path is not None: - event["transaction"] = path + self._set_transaction_name_and_source(event, self.transaction_style, asgi_scope) event["request"] = request_info @@ -213,6 +201,44 @@ def event_processor(self, event, hint, asgi_scope): # data to your liking it's recommended to use the `before_send` callback # for that. + def _set_transaction_name_and_source(self, event, transaction_style, asgi_scope): + # type: (Event, str, Any) -> None + + transaction_name_already_set = ( + event.get("transaction", _DEFAULT_TRANSACTION_NAME) + != _DEFAULT_TRANSACTION_NAME + ) + if transaction_name_already_set: + return + + name = "" + + if transaction_style == "endpoint": + endpoint = asgi_scope.get("endpoint") + # Webframeworks like Starlette mutate the ASGI env once routing is + # done, which is sometime after the request has started. If we have + # an endpoint, overwrite our generic transaction name. + if endpoint: + name = transaction_from_function(endpoint) or "" + + elif transaction_style == "url": + # FastAPI includes the route object in the scope to let Sentry extract the + # path from it for the transaction name + route = asgi_scope.get("route") + if route: + path = getattr(route, "path", None) + if path is not None: + name = path + + if not name: + # If no transaction name can be found set an unknown source. + # This can happen when ASGI frameworks that are not yet supported well are used. + event["transaction_info"] = {"source": TRANSACTION_SOURCE_UNKNOWN} + return + + event["transaction"] = name + event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]} + def _get_url(self, scope, default_scheme, host): # type: (Dict[str, Any], Literal["ws", "http"], Optional[str]) -> str """ diff --git a/sentry_sdk/integrations/aws_lambda.py b/sentry_sdk/integrations/aws_lambda.py index 10b5025abe..8f41ce52cb 100644 --- a/sentry_sdk/integrations/aws_lambda.py +++ b/sentry_sdk/integrations/aws_lambda.py @@ -3,7 +3,7 @@ import sys from sentry_sdk.hub import Hub, _should_send_default_pii -from sentry_sdk.tracing import Transaction +from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT, Transaction from sentry_sdk._compat import reraise from sentry_sdk.utils import ( AnnotatedValue, @@ -139,7 +139,10 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs): if headers is None: headers = {} transaction = Transaction.continue_from_headers( - headers, op="serverless.function", name=aws_context.function_name + headers, + op="serverless.function", + name=aws_context.function_name, + source=TRANSACTION_SOURCE_COMPONENT, ) with hub.start_transaction( transaction, diff --git a/sentry_sdk/integrations/bottle.py b/sentry_sdk/integrations/bottle.py index 4fa077e8f6..271fc150b1 100644 --- a/sentry_sdk/integrations/bottle.py +++ b/sentry_sdk/integrations/bottle.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from sentry_sdk.hub import Hub +from sentry_sdk.tracing import SOURCE_FOR_STYLE from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -20,7 +21,7 @@ from typing import Optional from bottle import FileUpload, FormsDict, LocalRequest # type: ignore - from sentry_sdk._types import EventProcessor + from sentry_sdk._types import EventProcessor, Event try: from bottle import ( @@ -40,7 +41,7 @@ class BottleIntegration(Integration): identifier = "bottle" - transaction_style = None + transaction_style = "" def __init__(self, transaction_style="endpoint"): # type: (str) -> None @@ -176,24 +177,34 @@ def size_of_file(self, file): return file.content_length +def _set_transaction_name_and_source(event, transaction_style, request): + # type: (Event, str, Any) -> None + name = "" + + if transaction_style == "url": + name = request.route.rule or "" + + elif transaction_style == "endpoint": + name = ( + request.route.name + or transaction_from_function(request.route.callback) + or "" + ) + + event["transaction"] = name + event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]} + + def _make_request_event_processor(app, request, integration): # type: (Bottle, LocalRequest, BottleIntegration) -> EventProcessor - def inner(event, hint): - # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any] - try: - if integration.transaction_style == "endpoint": - event["transaction"] = request.route.name or transaction_from_function( - request.route.callback - ) - elif integration.transaction_style == "url": - event["transaction"] = request.route.rule - except Exception: - pass + def event_processor(event, hint): + # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any] + _set_transaction_name_and_source(event, integration.transaction_style, request) with capture_internal_exceptions(): BottleRequestExtractor(request).extract_into_event(event) return event - return inner + return event_processor diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py index 743e2cfb50..2a095ec8c6 100644 --- a/sentry_sdk/integrations/celery.py +++ b/sentry_sdk/integrations/celery.py @@ -3,7 +3,11 @@ import sys from sentry_sdk.hub import Hub -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception +from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) from sentry_sdk.tracing import Transaction from sentry_sdk._compat import reraise from sentry_sdk.integrations import Integration, DidNotEnable @@ -154,8 +158,8 @@ def _inner(*args, **kwargs): args[3].get("headers") or {}, op="celery.task", name="unknown celery task", + source=TRANSACTION_SOURCE_TASK, ) - transaction.name = task.name transaction.set_status("ok") diff --git a/sentry_sdk/integrations/chalice.py b/sentry_sdk/integrations/chalice.py index 109862bd90..80069b2951 100644 --- a/sentry_sdk/integrations/chalice.py +++ b/sentry_sdk/integrations/chalice.py @@ -4,6 +4,7 @@ from sentry_sdk.hub import Hub from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations.aws_lambda import _make_request_event_processor +from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -65,7 +66,11 @@ def wrapped_view_function(**function_args): with hub.push_scope() as scope: with capture_internal_exceptions(): configured_time = app.lambda_context.get_remaining_time_in_millis() - scope.transaction = app.lambda_context.function_name + scope.set_transaction_name( + app.lambda_context.function_name, + source=TRANSACTION_SOURCE_COMPONENT, + ) + scope.add_event_processor( _make_request_event_processor( app.current_request.to_dict(), diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index d2ca12be4a..6bd1dd2c0b 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -9,6 +9,7 @@ from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.scope import add_global_event_processor from sentry_sdk.serializer import add_global_repr_processor +from sentry_sdk.tracing import SOURCE_FOR_STYLE from sentry_sdk.tracing_utils import record_sql_queries from sentry_sdk.utils import ( HAS_REAL_CONTEXTVARS, @@ -82,7 +83,7 @@ def is_authenticated(request_user): class DjangoIntegration(Integration): identifier = "django" - transaction_style = None + transaction_style = "" middleware_spans = None def __init__(self, transaction_style="url", middleware_spans=True): @@ -319,6 +320,32 @@ def _patch_django_asgi_handler(): patch_django_asgi_handler_impl(ASGIHandler) +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (Scope, str, WSGIRequest) -> None + try: + transaction_name = "" + if transaction_style == "function_name": + fn = resolve(request.path).func + transaction_name = ( + transaction_from_function(getattr(fn, "view_class", fn)) or "" + ) + + elif transaction_style == "url": + if hasattr(request, "urlconf"): + transaction_name = LEGACY_RESOLVER.resolve( + request.path_info, urlconf=request.urlconf + ) + else: + transaction_name = LEGACY_RESOLVER.resolve(request.path_info) + + scope.set_transaction_name( + transaction_name, + source=SOURCE_FOR_STYLE[transaction_style], + ) + except Exception: + pass + + def _before_get_response(request): # type: (WSGIRequest) -> None hub = Hub.current @@ -330,24 +357,15 @@ def _before_get_response(request): with hub.configure_scope() as scope: # Rely on WSGI middleware to start a trace - try: - if integration.transaction_style == "function_name": - fn = resolve(request.path).func - scope.transaction = transaction_from_function( - getattr(fn, "view_class", fn) - ) - elif integration.transaction_style == "url": - scope.transaction = LEGACY_RESOLVER.resolve(request.path_info) - except Exception: - pass + _set_transaction_name_and_source(scope, integration.transaction_style, request) scope.add_event_processor( _make_event_processor(weakref.ref(request), integration) ) -def _attempt_resolve_again(request, scope): - # type: (WSGIRequest, Scope) -> None +def _attempt_resolve_again(request, scope, transaction_style): + # type: (WSGIRequest, Scope, str) -> None """ Some django middlewares overwrite request.urlconf so we need to respect that contract, @@ -356,13 +374,7 @@ def _attempt_resolve_again(request, scope): if not hasattr(request, "urlconf"): return - try: - scope.transaction = LEGACY_RESOLVER.resolve( - request.path_info, - urlconf=request.urlconf, - ) - except Exception: - pass + _set_transaction_name_and_source(scope, transaction_style, request) def _after_get_response(request): @@ -373,7 +385,7 @@ def _after_get_response(request): return with hub.configure_scope() as scope: - _attempt_resolve_again(request, scope) + _attempt_resolve_again(request, scope, integration.transaction_style) def _patch_get_response(): @@ -438,7 +450,7 @@ def _got_request_exception(request=None, **kwargs): if request is not None and integration.transaction_style == "url": with hub.configure_scope() as scope: - _attempt_resolve_again(request, scope) + _attempt_resolve_again(request, scope, integration.transaction_style) # If an integration is there, a client has to be there. client = hub.client # type: Any diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 8129fab46b..b38e4bd5b4 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -4,7 +4,11 @@ from sentry_sdk.integrations import Integration, DidNotEnable from sentry_sdk.integrations._wsgi_common import RequestExtractor from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception +from sentry_sdk.tracing import SOURCE_FOR_STYLE +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) from sentry_sdk._types import MYPY @@ -87,7 +91,7 @@ def process_request(self, req, resp, *args, **kwargs): class FalconIntegration(Integration): identifier = "falcon" - transaction_style = None + transaction_style = "" def __init__(self, transaction_style="uri_template"): # type: (str) -> None @@ -197,19 +201,26 @@ def _exception_leads_to_http_5xx(ex): return is_server_error or is_unhandled_error +def _set_transaction_name_and_source(event, transaction_style, request): + # type: (Dict[str, Any], str, falcon.Request) -> None + name_for_style = { + "uri_template": request.uri_template, + "path": request.path, + } + event["transaction"] = name_for_style[transaction_style] + event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]} + + def _make_request_event_processor(req, integration): # type: (falcon.Request, FalconIntegration) -> EventProcessor - def inner(event, hint): + def event_processor(event, hint): # type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any] - if integration.transaction_style == "uri_template": - event["transaction"] = req.uri_template - elif integration.transaction_style == "path": - event["transaction"] = req.path + _set_transaction_name_and_source(event, integration.transaction_style, req) with capture_internal_exceptions(): FalconRequestExtractor(req).extract_into_event(event) return event - return inner + return event_processor diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index 5aade50a94..0aa8d2f120 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -1,23 +1,23 @@ from __future__ import absolute_import +from sentry_sdk._types import MYPY from sentry_sdk.hub import Hub, _should_send_default_pii -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception -from sentry_sdk.integrations import Integration, DidNotEnable -from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware +from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.integrations._wsgi_common import RequestExtractor - -from sentry_sdk._types import MYPY +from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware +from sentry_sdk.scope import Scope +from sentry_sdk.tracing import SOURCE_FOR_STYLE +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) if MYPY: - from sentry_sdk.integrations.wsgi import _ScopedResponse - from typing import Any - from typing import Dict - from werkzeug.datastructures import ImmutableMultiDict - from werkzeug.datastructures import FileStorage - from typing import Union - from typing import Callable + from typing import Any, Callable, Dict, Union from sentry_sdk._types import EventProcessor + from sentry_sdk.integrations.wsgi import _ScopedResponse + from werkzeug.datastructures import FileStorage, ImmutableMultiDict try: @@ -26,14 +26,9 @@ flask_login = None try: - from flask import ( # type: ignore - Markup, - Request, - Flask, - _request_ctx_stack, - _app_ctx_stack, - __version__ as FLASK_VERSION, - ) + from flask import Flask, Markup, Request # type: ignore + from flask import __version__ as FLASK_VERSION + from flask import _app_ctx_stack, _request_ctx_stack from flask.signals import ( before_render_template, got_request_exception, @@ -53,7 +48,7 @@ class FlaskIntegration(Integration): identifier = "flask" - transaction_style = None + transaction_style = "" def __init__(self, transaction_style="endpoint"): # type: (str) -> None @@ -114,6 +109,21 @@ def _add_sentry_trace(sender, template, context, **extra): ) +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (Scope, str, Request) -> None + try: + name_for_style = { + "url": request.url_rule.rule, + "endpoint": request.url_rule.endpoint, + } + scope.set_transaction_name( + name_for_style[transaction_style], + source=SOURCE_FOR_STYLE[transaction_style], + ) + except Exception: + pass + + def _request_started(sender, **kwargs): # type: (Flask, **Any) -> None hub = Hub.current @@ -125,16 +135,9 @@ def _request_started(sender, **kwargs): with hub.configure_scope() as scope: request = _request_ctx_stack.top.request - # Set the transaction name here, but rely on WSGI middleware to actually - # start the transaction - try: - if integration.transaction_style == "endpoint": - scope.transaction = request.url_rule.endpoint - elif integration.transaction_style == "url": - scope.transaction = request.url_rule.rule - except Exception: - pass - + # Set the transaction name and source here, + # but rely on WSGI middleware to actually start the transaction + _set_transaction_name_and_source(scope, integration.transaction_style, request) evt_processor = _make_request_event_processor(app, request, integration) scope.add_event_processor(evt_processor) diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index 118970e9d8..e401daa9ca 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -3,7 +3,7 @@ import sys from sentry_sdk.hub import Hub, _should_send_default_pii -from sentry_sdk.tracing import Transaction +from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT, Transaction from sentry_sdk._compat import reraise from sentry_sdk.utils import ( AnnotatedValue, @@ -81,7 +81,10 @@ def sentry_func(functionhandler, gcp_event, *args, **kwargs): if hasattr(gcp_event, "headers"): headers = gcp_event.headers transaction = Transaction.continue_from_headers( - headers, op="serverless.function", name=environ.get("FUNCTION_NAME", "") + headers, + op="serverless.function", + name=environ.get("FUNCTION_NAME", ""), + source=TRANSACTION_SOURCE_COMPONENT, ) sampling_context = { "gcp_env": { diff --git a/sentry_sdk/integrations/pyramid.py b/sentry_sdk/integrations/pyramid.py index 07142254d2..1e234fcffd 100644 --- a/sentry_sdk/integrations/pyramid.py +++ b/sentry_sdk/integrations/pyramid.py @@ -5,7 +5,12 @@ import weakref from sentry_sdk.hub import Hub, _should_send_default_pii -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception +from sentry_sdk.scope import Scope +from sentry_sdk.tracing import SOURCE_FOR_STYLE +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) from sentry_sdk._compat import reraise, iteritems from sentry_sdk.integrations import Integration, DidNotEnable @@ -51,7 +56,7 @@ def authenticated_userid(request): class PyramidIntegration(Integration): identifier = "pyramid" - transaction_style = None + transaction_style = "" def __init__(self, transaction_style="route_name"): # type: (str) -> None @@ -76,14 +81,9 @@ def sentry_patched_call_view(registry, request, *args, **kwargs): if integration is not None: with hub.configure_scope() as scope: - try: - if integration.transaction_style == "route_name": - scope.transaction = request.matched_route.name - elif integration.transaction_style == "route_pattern": - scope.transaction = request.matched_route.pattern - except Exception: - pass - + _set_transaction_name_and_source( + scope, integration.transaction_style, request + ) scope.add_event_processor( _make_event_processor(weakref.ref(request), integration) ) @@ -156,6 +156,21 @@ def _capture_exception(exc_info): hub.capture_event(event, hint=hint) +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (Scope, str, Request) -> None + try: + name_for_style = { + "route_name": request.matched_route.name, + "route_pattern": request.matched_route.pattern, + } + scope.set_transaction_name( + name_for_style[transaction_style], + source=SOURCE_FOR_STYLE[transaction_style], + ) + except Exception: + pass + + class PyramidRequestExtractor(RequestExtractor): def url(self): # type: () -> str diff --git a/sentry_sdk/integrations/quart.py b/sentry_sdk/integrations/quart.py index 411817c708..1ccd982d0e 100644 --- a/sentry_sdk/integrations/quart.py +++ b/sentry_sdk/integrations/quart.py @@ -4,7 +4,12 @@ from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.integrations._wsgi_common import _filter_headers from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from sentry_sdk.utils import capture_internal_exceptions, event_from_exception +from sentry_sdk.scope import Scope +from sentry_sdk.tracing import SOURCE_FOR_STYLE +from sentry_sdk.utils import ( + capture_internal_exceptions, + event_from_exception, +) from sentry_sdk._types import MYPY @@ -44,7 +49,7 @@ class QuartIntegration(Integration): identifier = "quart" - transaction_style = None + transaction_style = "" def __init__(self, transaction_style="endpoint"): # type: (str) -> None @@ -79,6 +84,22 @@ async def sentry_patched_asgi_app(self, scope, receive, send): Quart.__call__ = sentry_patched_asgi_app +def _set_transaction_name_and_source(scope, transaction_style, request): + # type: (Scope, str, Request) -> None + + try: + name_for_style = { + "url": request.url_rule.rule, + "endpoint": request.url_rule.endpoint, + } + scope.set_transaction_name( + name_for_style[transaction_style], + source=SOURCE_FOR_STYLE[transaction_style], + ) + except Exception: + pass + + def _request_websocket_started(sender, **kwargs): # type: (Quart, **Any) -> None hub = Hub.current @@ -95,13 +116,9 @@ def _request_websocket_started(sender, **kwargs): # Set the transaction name here, but rely on ASGI middleware # to actually start the transaction - try: - if integration.transaction_style == "endpoint": - scope.transaction = request_websocket.url_rule.endpoint - elif integration.transaction_style == "url": - scope.transaction = request_websocket.url_rule.rule - except Exception: - pass + _set_transaction_name_and_source( + scope, integration.transaction_style, request_websocket + ) evt_processor = _make_request_event_processor( app, request_websocket, integration diff --git a/sentry_sdk/integrations/sanic.py b/sentry_sdk/integrations/sanic.py index 4e20cc9ece..8892f93ed7 100644 --- a/sentry_sdk/integrations/sanic.py +++ b/sentry_sdk/integrations/sanic.py @@ -4,6 +4,7 @@ from sentry_sdk._compat import urlparse, reraise from sentry_sdk.hub import Hub +from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT from sentry_sdk.utils import ( capture_internal_exceptions, event_from_exception, @@ -191,7 +192,9 @@ async def _set_transaction(request, route, **kwargs): with capture_internal_exceptions(): with hub.configure_scope() as scope: route_name = route.name.replace(request.app.name, "").strip(".") - scope.transaction = route_name + scope.set_transaction_name( + route_name, source=TRANSACTION_SOURCE_COMPONENT + ) def _sentry_error_handler_lookup(self, exception, *args, **kwargs): @@ -268,9 +271,14 @@ def _legacy_router_get(self, *args): # Format: app_name.route_name sanic_route = sanic_route[len(sanic_app_name) + 1 :] - scope.transaction = sanic_route + scope.set_transaction_name( + sanic_route, source=TRANSACTION_SOURCE_COMPONENT + ) else: - scope.transaction = rv[0].__name__ + scope.set_transaction_name( + rv[0].__name__, source=TRANSACTION_SOURCE_COMPONENT + ) + return rv diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index 443ebefaa8..af048fb5e0 100644 --- a/sentry_sdk/integrations/tornado.py +++ b/sentry_sdk/integrations/tornado.py @@ -3,7 +3,7 @@ from inspect import iscoroutinefunction from sentry_sdk.hub import Hub, _should_send_default_pii -from sentry_sdk.tracing import Transaction +from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT, Transaction from sentry_sdk.utils import ( HAS_REAL_CONTEXTVARS, CONTEXTVARS_ERROR_MESSAGE, @@ -157,6 +157,7 @@ def tornado_processor(event, hint): with capture_internal_exceptions(): method = getattr(handler, handler.request.method.lower()) event["transaction"] = transaction_from_function(method) + event["transaction_info"] = {"source": TRANSACTION_SOURCE_COMPONENT} with capture_internal_exceptions(): extractor = TornadoRequestExtractor(request) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index bcfbf5c166..e0a2dc7a8d 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -81,6 +81,7 @@ class Scope(object): # note that for legacy reasons, _transaction is the transaction *name*, # not a Transaction object (the object is stored in _span) "_transaction", + "_transaction_info", "_user", "_tags", "_contexts", @@ -109,6 +110,7 @@ def clear(self): self._level = None # type: Optional[str] self._fingerprint = None # type: Optional[List[str]] self._transaction = None # type: Optional[str] + self._transaction_info = {} # type: Dict[str, str] self._user = None # type: Optional[Dict[str, Any]] self._tags = {} # type: Dict[str, Any] @@ -162,7 +164,10 @@ def transaction(self): def transaction(self, value): # type: (Any) -> None # would be type: (Optional[str]) -> None, see https://github.com/python/mypy/issues/3004 - """When set this forces a specific transaction name to be set.""" + """When set this forces a specific transaction name to be set. + + Deprecated: use set_transaction_name instead.""" + # XXX: the docstring above is misleading. The implementation of # apply_to_event prefers an existing value of event.transaction over # anything set in the scope. @@ -172,10 +177,27 @@ def transaction(self, value): # Without breaking version compatibility, we could make the setter set a # transaction name or transaction (self._span) depending on the type of # the value argument. + + logger.warning( + "Assigning to scope.transaction directly is deprecated: use scope.set_transaction_name() instead." + ) self._transaction = value if self._span and self._span.containing_transaction: self._span.containing_transaction.name = value + def set_transaction_name(self, name, source=None): + # type: (str, Optional[str]) -> None + """Set the transaction name and optionally the transaction source.""" + self._transaction = name + + if self._span and self._span.containing_transaction: + self._span.containing_transaction.name = name + if source: + self._span.containing_transaction.source = source + + if source: + self._transaction_info["source"] = source + @_attr_setter def user(self, value): # type: (Optional[Dict[str, Any]]) -> None @@ -363,6 +385,9 @@ def _drop(event, cause, ty): if event.get("transaction") is None and self._transaction is not None: event["transaction"] = self._transaction + if event.get("transaction_info") is None and self._transaction_info is not None: + event["transaction_info"] = self._transaction_info + if event.get("fingerprint") is None and self._fingerprint is not None: event["fingerprint"] = self._fingerprint @@ -406,6 +431,8 @@ def update_from_scope(self, scope): self._fingerprint = scope._fingerprint if scope._transaction is not None: self._transaction = scope._transaction + if scope._transaction_info is not None: + self._transaction_info.update(scope._transaction_info) if scope._user is not None: self._user = scope._user if scope._tags: @@ -452,6 +479,7 @@ def __copy__(self): rv._name = self._name rv._fingerprint = self._fingerprint rv._transaction = self._transaction + rv._transaction_info = dict(self._transaction_info) rv._user = self._user rv._tags = dict(self._tags) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index fe53386597..dd4b1a730d 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -23,6 +23,29 @@ from sentry_sdk._types import SamplingContext, MeasurementUnit +# Transaction source +# see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations +TRANSACTION_SOURCE_CUSTOM = "custom" +TRANSACTION_SOURCE_URL = "url" +TRANSACTION_SOURCE_ROUTE = "route" +TRANSACTION_SOURCE_VIEW = "view" +TRANSACTION_SOURCE_COMPONENT = "component" +TRANSACTION_SOURCE_TASK = "task" +TRANSACTION_SOURCE_UNKNOWN = "unknown" + +SOURCE_FOR_STYLE = { + "endpoint": TRANSACTION_SOURCE_COMPONENT, + "function_name": TRANSACTION_SOURCE_COMPONENT, + "handler_name": TRANSACTION_SOURCE_COMPONENT, + "method_and_path_pattern": TRANSACTION_SOURCE_ROUTE, + "path": TRANSACTION_SOURCE_URL, + "route_name": TRANSACTION_SOURCE_COMPONENT, + "route_pattern": TRANSACTION_SOURCE_ROUTE, + "uri_template": TRANSACTION_SOURCE_ROUTE, + "url": TRANSACTION_SOURCE_ROUTE, +} + + class _SpanRecorder(object): """Limits the number of spans recorded in a transaction.""" @@ -498,6 +521,7 @@ def get_trace_context(self): class Transaction(Span): __slots__ = ( "name", + "source", "parent_sampled", # the sentry portion of the `tracestate` header used to transmit # correlation context for server-side dynamic sampling, of the form @@ -517,6 +541,7 @@ def __init__( sentry_tracestate=None, # type: Optional[str] third_party_tracestate=None, # type: Optional[str] baggage=None, # type: Optional[Baggage] + source=TRANSACTION_SOURCE_UNKNOWN, # type: str **kwargs # type: Any ): # type: (...) -> None @@ -531,6 +556,7 @@ def __init__( name = kwargs.pop("transaction") Span.__init__(self, **kwargs) self.name = name + self.source = source self.parent_sampled = parent_sampled # if tracestate isn't inherited and set here, it will get set lazily, # either the first time an outgoing request needs it for a header or the @@ -543,7 +569,7 @@ def __init__( def __repr__(self): # type: () -> str return ( - "<%s(name=%r, op=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r)>" + "<%s(name=%r, op=%r, trace_id=%r, span_id=%r, parent_span_id=%r, sampled=%r, source=%r)>" % ( self.__class__.__name__, self.name, @@ -552,6 +578,7 @@ def __repr__(self): self.span_id, self.parent_span_id, self.sampled, + self.source, ) ) @@ -621,6 +648,7 @@ def finish(self, hub=None): event = { "type": "transaction", "transaction": self.name, + "transaction_info": {"source": self.source}, "contexts": {"trace": self.get_trace_context()}, "tags": self._tags, "timestamp": self.timestamp, @@ -648,6 +676,7 @@ def to_json(self): rv = super(Transaction, self).to_json() rv["name"] = self.name + rv["source"] = self.source rv["sampled"] = self.sampled return rv diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 5c590bcdfa..3375ee76ad 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -196,17 +196,30 @@ async def hello(request): @pytest.mark.parametrize( - "transaction_style,expected_transaction", + "url,transaction_style,expected_transaction,expected_source", [ ( + "/message", "handler_name", "tests.integrations.aiohttp.test_aiohttp.test_transaction_style..hello", + "component", + ), + ( + "/message", + "method_and_path_pattern", + "GET /{var}", + "route", ), - ("method_and_path_pattern", "GET /{var}"), ], ) async def test_transaction_style( - sentry_init, aiohttp_client, capture_events, transaction_style, expected_transaction + sentry_init, + aiohttp_client, + capture_events, + url, + transaction_style, + expected_transaction, + expected_source, ): sentry_init( integrations=[AioHttpIntegration(transaction_style=transaction_style)], @@ -222,13 +235,14 @@ async def hello(request): events = capture_events() client = await aiohttp_client(app) - resp = await client.get("/1") + resp = await client.get(url) assert resp.status == 200 (event,) = events assert event["type"] == "transaction" assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} async def test_traces_sampler_gets_request_object_in_sampling_context( diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index 5383b1a308..aed2157612 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -35,6 +35,33 @@ async def hi2(request): return app +@pytest.fixture +def transaction_app(): + transaction_app = Starlette() + + @transaction_app.route("/sync-message") + def hi(request): + capture_message("hi", level="error") + return PlainTextResponse("ok") + + @transaction_app.route("/sync-message/{user_id:int}") + def hi_with_id(request): + capture_message("hi", level="error") + return PlainTextResponse("ok") + + @transaction_app.route("/async-message") + async def async_hi(request): + capture_message("hi", level="error") + return PlainTextResponse("ok") + + @transaction_app.route("/async-message/{user_id:int}") + async def async_hi_with_id(request): + capture_message("hi", level="error") + return PlainTextResponse("ok") + + return transaction_app + + @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") def test_sync_request_data(sentry_init, app, capture_events): sentry_init(send_default_pii=True) @@ -230,6 +257,72 @@ def kangaroo_handler(request): ) +@pytest.mark.parametrize( + "url,transaction_style,expected_transaction,expected_source", + [ + ( + "/sync-message", + "endpoint", + "tests.integrations.asgi.test_asgi.transaction_app..hi", + "component", + ), + ( + "/sync-message", + "url", + "generic ASGI request", # the AsgiMiddleware can not extract routes from the Starlette framework used here for testing. + "unknown", + ), + ( + "/sync-message/123456", + "endpoint", + "tests.integrations.asgi.test_asgi.transaction_app..hi_with_id", + "component", + ), + ( + "/sync-message/123456", + "url", + "generic ASGI request", # the AsgiMiddleware can not extract routes from the Starlette framework used here for testing. + "unknown", + ), + ( + "/async-message", + "endpoint", + "tests.integrations.asgi.test_asgi.transaction_app..async_hi", + "component", + ), + ( + "/async-message", + "url", + "generic ASGI request", # the AsgiMiddleware can not extract routes from the Starlette framework used here for testing. + "unknown", + ), + ], +) +def test_transaction_style( + sentry_init, + transaction_app, + url, + transaction_style, + expected_transaction, + expected_source, + capture_events, +): + sentry_init(send_default_pii=True) + + transaction_app = SentryAsgiMiddleware( + transaction_app, transaction_style=transaction_style + ) + + events = capture_events() + + client = TestClient(transaction_app) + client.get(url) + + (event,) = events + assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} + + def test_traces_sampler_gets_scope_in_sampling_context( app, sentry_init, DictionaryContaining # noqa: N803 ): diff --git a/tests/integrations/aws_lambda/test_aws.py b/tests/integrations/aws_lambda/test_aws.py index c9084beb14..c6fb54b94f 100644 --- a/tests/integrations/aws_lambda/test_aws.py +++ b/tests/integrations/aws_lambda/test_aws.py @@ -362,6 +362,7 @@ def test_handler(event, context): assert envelope["type"] == "transaction" assert envelope["contexts"]["trace"]["op"] == "serverless.function" assert envelope["transaction"].startswith("test_function_") + assert envelope["transaction_info"] == {"source": "component"} assert envelope["transaction"] in envelope["request"]["url"] @@ -390,6 +391,7 @@ def test_handler(event, context): assert envelope["type"] == "transaction" assert envelope["contexts"]["trace"]["op"] == "serverless.function" assert envelope["transaction"].startswith("test_function_") + assert envelope["transaction_info"] == {"source": "component"} assert envelope["transaction"] in envelope["request"]["url"] diff --git a/tests/integrations/bottle/test_bottle.py b/tests/integrations/bottle/test_bottle.py index ec133e4d75..0ef4339874 100644 --- a/tests/integrations/bottle/test_bottle.py +++ b/tests/integrations/bottle/test_bottle.py @@ -24,6 +24,11 @@ def hi(): capture_message("hi") return "ok" + @app.route("/message/") + def hi_with_id(message_id): + capture_message("hi") + return "ok" + @app.route("/message-named-route", name="hi") def named_hi(): capture_message("hi") @@ -55,20 +60,21 @@ def test_has_context(sentry_init, app, capture_events, get_client): @pytest.mark.parametrize( - "url,transaction_style,expected_transaction", + "url,transaction_style,expected_transaction,expected_source", [ - ("/message", "endpoint", "hi"), - ("/message", "url", "/message"), - ("/message-named-route", "endpoint", "hi"), + ("/message", "endpoint", "hi", "component"), + ("/message", "url", "/message", "route"), + ("/message/123456", "url", "/message/", "route"), + ("/message-named-route", "endpoint", "hi", "component"), ], ) def test_transaction_style( sentry_init, - app, - capture_events, + url, transaction_style, expected_transaction, - url, + expected_source, + capture_events, get_client, ): sentry_init( @@ -79,11 +85,14 @@ def test_transaction_style( events = capture_events() client = get_client() - response = client.get("/message") + response = client.get(url) assert response[1] == "200 OK" (event,) = events + # We use endswith() because in Python 2.7 it is "test_bottle.hi" + # and in later Pythons "test_bottle.app..hi" assert event["transaction"].endswith(expected_transaction) + assert event["transaction_info"] == {"source": expected_source} @pytest.mark.parametrize("debug", (True, False), ids=["debug", "nodebug"]) diff --git a/tests/integrations/celery/test_celery.py b/tests/integrations/celery/test_celery.py index a77ac1adb1..951f8ecb8c 100644 --- a/tests/integrations/celery/test_celery.py +++ b/tests/integrations/celery/test_celery.py @@ -155,9 +155,11 @@ def dummy_task(x, y): assert error_event["exception"]["values"][0]["type"] == "ZeroDivisionError" execution_event, submission_event = events - assert execution_event["transaction"] == "dummy_task" + assert execution_event["transaction_info"] == {"source": "task"} + assert submission_event["transaction"] == "submission" + assert submission_event["transaction_info"] == {"source": "unknown"} assert execution_event["type"] == submission_event["type"] == "transaction" assert execution_event["contexts"]["trace"]["trace_id"] == transaction.trace_id diff --git a/tests/integrations/chalice/test_chalice.py b/tests/integrations/chalice/test_chalice.py index 8bb33a5cb6..4162a55623 100644 --- a/tests/integrations/chalice/test_chalice.py +++ b/tests/integrations/chalice/test_chalice.py @@ -4,6 +4,7 @@ from chalice.local import LambdaContext, LocalGateway from sentry_sdk.integrations.chalice import ChaliceIntegration +from sentry_sdk import capture_message from pytest_chalice.handlers import RequestHandler @@ -41,6 +42,16 @@ def has_request(): def badrequest(): raise BadRequestError("bad-request") + @app.route("/message") + def hi(): + capture_message("hi") + return {"status": "ok"} + + @app.route("/message/{message_id}") + def hi_with_id(message_id): + capture_message("hi again") + return {"status": "ok"} + LocalGateway._generate_lambda_context = _generate_lambda_context return app @@ -109,3 +120,28 @@ def test_bad_reques(client: RequestHandler) -> None: ("Message", "BadRequestError: bad-request"), ] ) + + +@pytest.mark.parametrize( + "url,expected_transaction,expected_source", + [ + ("/message", "api_handler", "component"), + ("/message/123456", "api_handler", "component"), + ], +) +def test_transaction( + app, + client: RequestHandler, + capture_events, + url, + expected_transaction, + expected_source, +): + events = capture_events() + + response = client.get(url) + assert response.status_code == 200 + + (event,) = events + assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 6106131375..6195811fe0 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -469,14 +469,19 @@ def test_django_connect_breadcrumbs( @pytest.mark.parametrize( - "transaction_style,expected_transaction", + "transaction_style,expected_transaction,expected_source", [ - ("function_name", "tests.integrations.django.myapp.views.message"), - ("url", "/message"), + ("function_name", "tests.integrations.django.myapp.views.message", "component"), + ("url", "/message", "route"), ], ) def test_transaction_style( - sentry_init, client, capture_events, transaction_style, expected_transaction + sentry_init, + client, + capture_events, + transaction_style, + expected_transaction, + expected_source, ): sentry_init( integrations=[DjangoIntegration(transaction_style=transaction_style)], @@ -488,6 +493,7 @@ def test_transaction_style( (event,) = events assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} def test_request_body(sentry_init, client, capture_events): diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py index 84e8d228f0..96aa0ee036 100644 --- a/tests/integrations/falcon/test_falcon.py +++ b/tests/integrations/falcon/test_falcon.py @@ -21,8 +21,14 @@ def on_get(self, req, resp): sentry_sdk.capture_message("hi") resp.media = "hi" + class MessageByIdResource: + def on_get(self, req, resp, message_id): + sentry_sdk.capture_message("hi") + resp.media = "hi" + app = falcon.API() app.add_route("/message", MessageResource()) + app.add_route("/message/{message_id:int}", MessageByIdResource()) return app @@ -53,22 +59,34 @@ def test_has_context(sentry_init, capture_events, make_client): @pytest.mark.parametrize( - "transaction_style,expected_transaction", - [("uri_template", "/message"), ("path", "/message")], + "url,transaction_style,expected_transaction,expected_source", + [ + ("/message", "uri_template", "/message", "route"), + ("/message", "path", "/message", "url"), + ("/message/123456", "uri_template", "/message/{message_id:int}", "route"), + ("/message/123456", "path", "/message/123456", "url"), + ], ) def test_transaction_style( - sentry_init, make_client, capture_events, transaction_style, expected_transaction + sentry_init, + make_client, + capture_events, + url, + transaction_style, + expected_transaction, + expected_source, ): integration = FalconIntegration(transaction_style=transaction_style) sentry_init(integrations=[integration]) events = capture_events() client = make_client() - response = client.simulate_get("/message") + response = client.simulate_get(url) assert response.status == falcon.HTTP_200 (event,) = events assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} def test_unhandled_errors(sentry_init, capture_exceptions, capture_events): diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 8723a35c86..d64e616b37 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -46,6 +46,11 @@ def hi(): capture_message("hi") return "ok" + @app.route("/message/") + def hi_with_id(message_id): + capture_message("hi again") + return "ok" + return app @@ -74,10 +79,22 @@ def test_has_context(sentry_init, app, capture_events): @pytest.mark.parametrize( - "transaction_style,expected_transaction", [("endpoint", "hi"), ("url", "/message")] + "url,transaction_style,expected_transaction,expected_source", + [ + ("/message", "endpoint", "hi", "component"), + ("/message", "url", "/message", "route"), + ("/message/123456", "endpoint", "hi_with_id", "component"), + ("/message/123456", "url", "/message/", "route"), + ], ) def test_transaction_style( - sentry_init, app, capture_events, transaction_style, expected_transaction + sentry_init, + app, + capture_events, + url, + transaction_style, + expected_transaction, + expected_source, ): sentry_init( integrations=[ @@ -87,11 +104,12 @@ def test_transaction_style( events = capture_events() client = app.test_client() - response = client.get("/message") + response = client.get(url) assert response.status_code == 200 (event,) = events assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} @pytest.mark.parametrize("debug", (True, False)) diff --git a/tests/integrations/gcp/test_gcp.py b/tests/integrations/gcp/test_gcp.py index 78ac8f2746..5f41300bcb 100644 --- a/tests/integrations/gcp/test_gcp.py +++ b/tests/integrations/gcp/test_gcp.py @@ -255,6 +255,7 @@ def cloud_function(functionhandler, event): assert envelope["type"] == "transaction" assert envelope["contexts"]["trace"]["op"] == "serverless.function" assert envelope["transaction"].startswith("Google Cloud function") + assert envelope["transaction_info"] == {"source": "component"} assert envelope["transaction"] in envelope["request"]["url"] diff --git a/tests/integrations/pyramid/test_pyramid.py b/tests/integrations/pyramid/test_pyramid.py index 9c6fd51222..c49f8b4475 100644 --- a/tests/integrations/pyramid/test_pyramid.py +++ b/tests/integrations/pyramid/test_pyramid.py @@ -26,12 +26,19 @@ def hi(request): return Response("hi") +def hi_with_id(request): + capture_message("hi with id") + return Response("hi with id") + + @pytest.fixture def pyramid_config(): config = pyramid.testing.setUp() try: config.add_route("hi", "/message") config.add_view(hi, route_name="hi") + config.add_route("hi_with_id", "/message/{message_id}") + config.add_view(hi_with_id, route_name="hi_with_id") yield config finally: pyramid.testing.tearDown() @@ -89,13 +96,13 @@ def test_has_context(route, get_client, sentry_init, capture_events): sentry_init(integrations=[PyramidIntegration()]) events = capture_events() - @route("/message/{msg}") + @route("/context_message/{msg}") def hi2(request): capture_message(request.matchdict["msg"]) return Response("hi") client = get_client() - client.get("/message/yoo") + client.get("/context_message/yoo") (event,) = events assert event["message"] == "yoo" @@ -104,26 +111,38 @@ def hi2(request): "headers": {"Host": "localhost"}, "method": "GET", "query_string": "", - "url": "http://localhost/message/yoo", + "url": "http://localhost/context_message/yoo", } assert event["transaction"] == "hi2" @pytest.mark.parametrize( - "transaction_style,expected_transaction", - [("route_name", "hi"), ("route_pattern", "/message")], + "url,transaction_style,expected_transaction,expected_source", + [ + ("/message", "route_name", "hi", "component"), + ("/message", "route_pattern", "/message", "route"), + ("/message/123456", "route_name", "hi_with_id", "component"), + ("/message/123456", "route_pattern", "/message/{message_id}", "route"), + ], ) def test_transaction_style( - sentry_init, get_client, capture_events, transaction_style, expected_transaction + sentry_init, + get_client, + capture_events, + url, + transaction_style, + expected_transaction, + expected_source, ): sentry_init(integrations=[PyramidIntegration(transaction_style=transaction_style)]) events = capture_events() client = get_client() - client.get("/message") + client.get(url) (event,) = events assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} def test_large_json_request(sentry_init, capture_events, route, get_client): diff --git a/tests/integrations/quart/test_quart.py b/tests/integrations/quart/test_quart.py index d827b3c4aa..6d2c590a53 100644 --- a/tests/integrations/quart/test_quart.py +++ b/tests/integrations/quart/test_quart.py @@ -1,4 +1,5 @@ import pytest +import pytest_asyncio quart = pytest.importorskip("quart") @@ -21,7 +22,7 @@ auth_manager = AuthManager() -@pytest.fixture +@pytest_asyncio.fixture async def app(): app = Quart(__name__) app.debug = True @@ -35,6 +36,11 @@ async def hi(): capture_message("hi") return "ok" + @app.route("/message/") + async def hi_with_id(message_id): + capture_message("hi with id") + return "ok with id" + return app @@ -63,10 +69,22 @@ async def test_has_context(sentry_init, app, capture_events): @pytest.mark.asyncio @pytest.mark.parametrize( - "transaction_style,expected_transaction", [("endpoint", "hi"), ("url", "/message")] + "url,transaction_style,expected_transaction,expected_source", + [ + ("/message", "endpoint", "hi", "component"), + ("/message", "url", "/message", "route"), + ("/message/123456", "endpoint", "hi_with_id", "component"), + ("/message/123456", "url", "/message/", "route"), + ], ) async def test_transaction_style( - sentry_init, app, capture_events, transaction_style, expected_transaction + sentry_init, + app, + capture_events, + url, + transaction_style, + expected_transaction, + expected_source, ): sentry_init( integrations=[ @@ -76,7 +94,7 @@ async def test_transaction_style( events = capture_events() client = app.test_client() - response = await client.get("/message") + response = await client.get(url) assert response.status_code == 200 (event,) = events diff --git a/tests/integrations/sanic/test_sanic.py b/tests/integrations/sanic/test_sanic.py index b91f94bfe9..f8fdd696bc 100644 --- a/tests/integrations/sanic/test_sanic.py +++ b/tests/integrations/sanic/test_sanic.py @@ -30,6 +30,11 @@ def hi(request): capture_message("hi") return response.text("ok") + @app.route("/message/") + def hi_with_id(request, message_id): + capture_message("hi with id") + return response.text("ok with id") + return app @@ -62,6 +67,27 @@ def test_request_data(sentry_init, app, capture_events): assert "transaction" not in event +@pytest.mark.parametrize( + "url,expected_transaction,expected_source", + [ + ("/message", "hi", "component"), + ("/message/123456", "hi_with_id", "component"), + ], +) +def test_transaction( + sentry_init, app, capture_events, url, expected_transaction, expected_source +): + sentry_init(integrations=[SanicIntegration()]) + events = capture_events() + + request, response = app.test_client.get(url) + assert response.status == 200 + + (event,) = events + assert event["transaction"] == expected_transaction + assert event["transaction_info"] == {"source": expected_source} + + def test_errors(sentry_init, app, capture_events): sentry_init(integrations=[SanicIntegration()]) events = capture_events() diff --git a/tests/integrations/tornado/test_tornado.py b/tests/integrations/tornado/test_tornado.py index 1c5137f2b2..f59781dc21 100644 --- a/tests/integrations/tornado/test_tornado.py +++ b/tests/integrations/tornado/test_tornado.py @@ -96,6 +96,7 @@ def test_basic(tornado_testcase, sentry_init, capture_events): event["transaction"] == "tests.integrations.tornado.test_tornado.CrashingHandler.get" ) + assert event["transaction_info"] == {"source": "component"} with configure_scope() as scope: assert not scope._tags @@ -129,6 +130,9 @@ def test_transactions(tornado_testcase, sentry_init, capture_events, handler, co assert client_tx["type"] == "transaction" assert client_tx["transaction"] == "client" + assert client_tx["transaction_info"] == { + "source": "unknown" + } # because this is just the start_transaction() above. if server_error is not None: assert server_error["exception"]["values"][0]["type"] == "ZeroDivisionError" @@ -136,6 +140,7 @@ def test_transactions(tornado_testcase, sentry_init, capture_events, handler, co server_error["transaction"] == "tests.integrations.tornado.test_tornado.CrashingHandler.post" ) + assert server_error["transaction_info"] == {"source": "component"} if code == 200: assert ( @@ -148,6 +153,7 @@ def test_transactions(tornado_testcase, sentry_init, capture_events, handler, co == "tests.integrations.tornado.test_tornado.CrashingHandler.post" ) + assert server_tx["transaction_info"] == {"source": "component"} assert server_tx["type"] == "transaction" request = server_tx["request"]