diff --git a/sentry_sdk/integrations/_asgi_common.py b/sentry_sdk/integrations/_asgi_common.py index f167c1bb85..65cb3b7155 100644 --- a/sentry_sdk/integrations/_asgi_common.py +++ b/sentry_sdk/integrations/_asgi_common.py @@ -2,6 +2,8 @@ from enum import Enum from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string from sentry_sdk.integrations._wsgi_common import _filter_headers @@ -161,7 +163,9 @@ def _get_request_attributes( client_options = sentry_sdk.get_client().options if ty in ("http", "websocket"): if asgi_scope.get("method"): - attributes["http.request.method"] = asgi_scope["method"].upper() + attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = asgi_scope[ + "method" + ].upper() headers = _get_headers(asgi_scope) @@ -178,10 +182,10 @@ def _get_request_attributes( behaviour=client_options["data_collection"]["url_query_params"], ) if filtered_query_string: - attributes["http.query"] = filtered_query_string + attributes[ATTRIBUTE_NAMES.HTTP_QUERY] = filtered_query_string path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path) - attributes["url.path"] = path + attributes[ATTRIBUTE_NAMES.URL_PATH] = path url_without_query_string = _get_url( asgi_scope, @@ -190,7 +194,7 @@ def _get_request_attributes( path=path, ) - attributes["url.full"] = ( + attributes[ATTRIBUTE_NAMES.URL_FULL] = ( f"{url_without_query_string}?{filtered_query_string}" if filtered_query_string is not None else url_without_query_string @@ -200,6 +204,6 @@ def _get_request_attributes( if asgi_scope_client: if client_options["data_collection"]["user_info"]: ip = _get_ip(asgi_scope) - attributes["client.address"] = ip + attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] = ip return attributes diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 76db74a9c4..e54332cbfa 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -2,6 +2,8 @@ import weakref from functools import wraps +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import _SENTRY_HEADER_NAMES, OP, SPANDATA from sentry_sdk.data_collection import ( @@ -118,7 +120,7 @@ async def sentry_app_handle( scope.add_event_processor(_make_request_processor(weak_request)) scope.set_attribute( - SPANDATA.SENTRY_IS_LOCALHOST, + ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST, _is_localhost( client_ip=_get_client_ip(request), host_header=request.headers.get("Host"), @@ -130,7 +132,9 @@ async def sentry_app_handle( user_agent = request.headers.get("User-Agent") if user_agent: - scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent) + scope.set_attribute( + ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, user_agent + ) headers = dict(request.headers) @@ -146,12 +150,12 @@ async def sentry_app_handle( url_attributes = {} client_address_attributes = {} - url_attributes["url.full"] = "%s://%s%s" % ( + url_attributes[ATTRIBUTE_NAMES.URL_FULL] = "%s://%s%s" % ( request.scheme, request.host, request.path, ) - url_attributes["url.path"] = request.path + url_attributes[ATTRIBUTE_NAMES.URL_PATH] = request.path if request.query_string: filtered_query_string = ( @@ -163,14 +167,20 @@ async def sentry_app_handle( ) ) if filtered_query_string: - url_attributes["url.query"] = filtered_query_string - url_attributes["url.full"] += "?" + filtered_query_string + url_attributes[ATTRIBUTE_NAMES.URL_QUERY] = ( + filtered_query_string + ) + url_attributes[ATTRIBUTE_NAMES.URL_FULL] += ( + "?" + filtered_query_string + ) if request.remote: if client.options["data_collection"]["user_info"]: - client_address_attributes["client.address"] = request.remote + client_address_attributes[ + ATTRIBUTE_NAMES.CLIENT_ADDRESS + ] = request.remote scope.set_attribute( - SPANDATA.USER_IP_ADDRESS, request.remote + ATTRIBUTE_NAMES.USER_IP_ADDRESS, request.remote ) span = sentry_sdk.start_span( @@ -178,10 +188,10 @@ async def sentry_app_handle( # resolver did not find a route or died trying. name="generic AIOHTTP request", attributes={ - "sentry.op": OP.HTTP_SERVER, - "sentry.origin": AioHttpIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.ROUTE.value, - "http.request.method": request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioHttpIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.ROUTE.value, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, **url_attributes, **client_address_attributes, **header_attributes, @@ -195,7 +205,7 @@ async def sentry_app_handle( response = await old_handle(self, request) except HTTPException as e: span.set_attribute( - "http.response.status_code", e.status_code + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, e.status_code ) if e.status_code >= 400: @@ -227,7 +237,8 @@ async def sentry_app_handle( pass else: span.set_attribute( - "http.response.status_code", response_status + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, + response_status, ) span.status = ( SpanStatus.ERROR.value @@ -256,7 +267,7 @@ async def sentry_urldispatcher_resolve( server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and pattern is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, pattern) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, pattern) current_scope = sentry_sdk.get_current_scope() @@ -309,13 +320,13 @@ async def on_request_start( span: "Optional[Span]" = None attributes: "Attributes" = { - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": AioHttpIntegration.origin, - "http.request.method": method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioHttpIntegration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: method, } if parsed_url is not None: url_full = parsed_url.url - attributes["url.path"] = params.url.path + attributes[ATTRIBUTE_NAMES.URL_PATH] = params.url.path if parsed_url.query: filtered_query = _apply_data_collection_filtering_to_query_string( @@ -323,16 +334,16 @@ async def on_request_start( behaviour=client.options["data_collection"]["url_query_params"], ) if filtered_query: - attributes["url.query"] = filtered_query + attributes[ATTRIBUTE_NAMES.URL_QUERY] = filtered_query url_full += "?" + filtered_query breadcrumb[SPANDATA.HTTP_QUERY] = filtered_query if parsed_url.fragment: - attributes["url.fragment"] = parsed_url.fragment + attributes[ATTRIBUTE_NAMES.URL_FRAGMENT] = parsed_url.fragment url_full += "#" + parsed_url.fragment breadcrumb[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment - attributes["url.full"] = url_full + attributes[ATTRIBUTE_NAMES.URL_FULL] = url_full breadcrumb["url"] = url_full if sentry_sdk.get_current_span() is not None: @@ -438,7 +449,7 @@ async def on_request_end( if span is None: return - span.set_attribute("http.response.status_code", status) + span.set_attribute(ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, status) span.status = SpanStatus.ERROR.value if status >= 400 else SpanStatus.OK.value with capture_internal_exceptions(): diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 5b07b43087..3b78604202 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -12,8 +12,10 @@ from functools import partial from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations._asgi_common import ( _get_headers, _get_ip, @@ -188,7 +190,7 @@ async def _run_app( headers = _get_headers(scope) sentry_scope.set_attribute( - SPANDATA.SENTRY_IS_LOCALHOST, + ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST, _is_localhost( client_ip=_get_ip(scope), url_host=scope.get("server")[0] @@ -201,7 +203,7 @@ async def _run_app( if headers.get("user-agent"): sentry_scope.set_attribute( - SPANDATA.USER_AGENT_ORIGINAL, headers["user-agent"] + ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, headers["user-agent"] ) ty = scope["type"] @@ -214,18 +216,20 @@ async def _run_app( span: "Optional[ContextManager[Optional[Span]]]" = None attributes: "Attributes" = { - "sentry.segment.name.source": getattr( + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: getattr( transaction_source, "value", transaction_source ), - "sentry.origin": self.span_origin, - "network.protocol.name": ty, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: self.span_origin, + ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME: ty, } ip = _get_ip(scope) if ip: client_options = sentry_sdk.get_client().options if client_options["data_collection"]["user_info"]: - sentry_scope.set_attribute(SPANDATA.USER_IP_ADDRESS, ip) + sentry_scope.set_attribute( + ATTRIBUTE_NAMES.USER_IP_ADDRESS, ip + ) if ty in ("http", "websocket"): if ty == "websocket" or method in self.http_methods_to_capture: @@ -233,7 +237,7 @@ async def _run_app( Scope.set_custom_sampling_context({"asgi_scope": scope}) - attributes["sentry.op"] = f"{ty}.server" + attributes[ATTRIBUTE_NAMES.SENTRY_OP] = f"{ty}.server" span = sentry_sdk.start_span( name=transaction_name, attributes=attributes, @@ -245,7 +249,7 @@ async def _run_app( Scope.set_custom_sampling_context({"asgi_scope": scope}) - attributes["sentry.op"] = OP.HTTP_SERVER + attributes[ATTRIBUTE_NAMES.SENTRY_OP] = OP.HTTP_SERVER span = sentry_sdk.start_span( name=transaction_name, attributes=attributes, @@ -276,7 +280,7 @@ async def _sentry_wrapped_send( "error" if event["status"] >= 400 else "ok" ) span.set_attribute( - "http.response.status_code", + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, event["status"], ) @@ -303,7 +307,7 @@ async def _sentry_wrapped_send( span is not None and span.name != _DEFAULT_TRANSACTION_NAME and span.get_attributes().get( - "sentry.segment.name.source" + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE ) in [ SegmentNameSource.COMPONENT.value, @@ -318,7 +322,8 @@ async def _sentry_wrapped_send( ) span.name = name span.set_attribute( - "sentry.segment.name.source", source + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, + source, ) finally: _asgi_middleware_applied.set(False) diff --git a/sentry_sdk/integrations/bottle.py b/sentry_sdk/integrations/bottle.py index 4e23dabbc2..5aaac0b1ce 100644 --- a/sentry_sdk/integrations/bottle.py +++ b/sentry_sdk/integrations/bottle.py @@ -1,8 +1,9 @@ import functools from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import ( _DEFAULT_FAILED_REQUEST_STATUS_CODES, DidNotEnable, @@ -112,7 +113,7 @@ def _patched_handle(self: "Bottle", environ: "Dict[str, Any]") -> "Any": pass if route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) name = _get_transaction_name(bottle_request) if name: diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 7c7e4e0e3d..80ab44f4c4 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -4,6 +4,8 @@ import threading import weakref +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP, SPANDATA, SPANNAME from sentry_sdk.integrations import ( @@ -341,8 +343,8 @@ def sentry_patched_drf_authenticate(self: "Request") -> "Any": with sentry_sdk.start_span( name="authenticate", attributes={ - "sentry.op": OP.VIEW, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.VIEW, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return old_drf_authenticate(self) @@ -396,7 +398,7 @@ def _set_transaction_name_and_source( if route_path is not None: server_span = scope._server_segment_span if server_span is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) except Exception: pass @@ -426,7 +428,7 @@ def _before_get_response(request: "WSGIRequest") -> None: server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) # Rely on WSGI middleware to start a trace _set_transaction_name_and_source(scope, request, route_path=route_path) @@ -464,7 +466,7 @@ def _attempt_resolve_again( server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) _set_transaction_name_and_source(scope, request, route_path=route_path) @@ -788,8 +790,8 @@ def connect(self: "BaseDatabaseWrapper") -> None: with sentry_sdk.start_span( name="connect", attributes={ - "sentry.op": OP.DB, - "sentry.origin": DjangoIntegration.origin_db, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin_db, }, ) as span: _set_db_data(span, self) @@ -807,8 +809,8 @@ def _commit(self: "BaseDatabaseWrapper") -> None: with sentry_sdk.start_span( name=SPANNAME.DB_COMMIT, attributes={ - "sentry.op": OP.DB, - "sentry.origin": DjangoIntegration.origin_db, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin_db, }, ) as span: _set_db_data(span, self, SPANNAME.DB_COMMIT) @@ -826,8 +828,8 @@ def _rollback(self: "BaseDatabaseWrapper") -> None: with sentry_sdk.start_span( name=SPANNAME.DB_ROLLBACK, attributes={ - "sentry.op": OP.DB, - "sentry.origin": DjangoIntegration.origin_db, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin_db, }, ) as span: _set_db_data(span, self, SPANNAME.DB_ROLLBACK) @@ -848,10 +850,10 @@ def _set_db_data( ) -> None: db = cursor_or_db.db if hasattr(cursor_or_db, "db") else cursor_or_db vendor = db.vendor - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, vendor) + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, vendor) if db_operation is not None: - span.set_attribute(SPANDATA.DB_OPERATION_NAME, db_operation) + span.set_attribute(ATTRIBUTE_NAMES.DB_OPERATION_NAME, db_operation) # Some custom backends override `__getattr__`, making it look like `cursor_or_db` # actually has a `connection` and the `connection` has a `get_dsn_parameters` @@ -885,15 +887,15 @@ def _set_db_data( db_name = connection_params.get("dbname") or connection_params.get("database") if db_name is not None: - span.set_attribute(SPANDATA.DB_NAMESPACE, db_name) + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, db_name) server_address = connection_params.get("host") if server_address is not None: - span.set_attribute(SPANDATA.SERVER_ADDRESS, server_address) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, server_address) server_port = connection_params.get("port") if server_port is not None: - span.set_attribute(SPANDATA.SERVER_PORT, str(server_port)) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, str(server_port)) server_socket_address = connection_params.get("unix_socket") if server_socket_address is not None: diff --git a/sentry_sdk/integrations/django/asgi.py b/sentry_sdk/integrations/django/asgi.py index 82362bbf3f..cfde526d71 100644 --- a/sentry_sdk/integrations/django/asgi.py +++ b/sentry_sdk/integrations/django/asgi.py @@ -12,6 +12,7 @@ from typing import TYPE_CHECKING from django.core.handlers.wsgi import WSGIRequest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -183,8 +184,8 @@ async def sentry_wrapped_callback( with sentry_sdk.start_span( name=request.resolver_match.view_name, attributes={ - "sentry.op": OP.VIEW_RENDER, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.VIEW_RENDER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return await callback(request, *args, **kwargs) diff --git a/sentry_sdk/integrations/django/caching.py b/sentry_sdk/integrations/django/caching.py index 426daec209..f73319878e 100644 --- a/sentry_sdk/integrations/django/caching.py +++ b/sentry_sdk/integrations/django/caching.py @@ -3,10 +3,11 @@ from django import VERSION as DJANGO_VERSION from django.core.cache import CacheHandler +from sentry_conventions.attributes import ATTRIBUTE_NAMES from urllib3.util import parse_url as urlparse import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis.utils import _get_safe_key, _key_as_string from sentry_sdk.utils import ( capture_internal_exceptions, @@ -64,30 +65,30 @@ def _instrument_call( with sentry_sdk.start_span( name=description, attributes={ - "sentry.op": op, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ) as span: value = original_method(*args, **kwargs) with capture_internal_exceptions(): if address is not None: - span.set_attribute(SPANDATA.NETWORK_PEER_ADDRESS, address) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS, address) if port is not None: - span.set_attribute(SPANDATA.NETWORK_PEER_PORT, port) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_PORT, port) key = _get_safe_key(method_name, args, kwargs) if key is not None: - span.set_attribute(SPANDATA.CACHE_KEY, key) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_KEY, key) item_size = None if is_get_many_method: if value != {}: item_size = len(str(value)) - span.set_attribute(SPANDATA.CACHE_HIT, True) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, True) else: - span.set_attribute(SPANDATA.CACHE_HIT, False) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, False) elif is_get_method: default_value = None if len(args) >= 2: @@ -97,9 +98,9 @@ def _instrument_call( if value != default_value: item_size = len(str(value)) - span.set_attribute(SPANDATA.CACHE_HIT, True) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, True) else: - span.set_attribute(SPANDATA.CACHE_HIT, False) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, False) else: # TODO: We don't handle `get_or_set` which we should arg_count = len(args) if arg_count >= 2: @@ -110,7 +111,7 @@ def _instrument_call( item_size = len(str(args[0])) if item_size is not None: - span.set_attribute(SPANDATA.CACHE_ITEM_SIZE, item_size) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_ITEM_SIZE, item_size) return value diff --git a/sentry_sdk/integrations/django/middleware.py b/sentry_sdk/integrations/django/middleware.py index 7d35896db9..31c9b79bd0 100644 --- a/sentry_sdk/integrations/django/middleware.py +++ b/sentry_sdk/integrations/django/middleware.py @@ -7,9 +7,10 @@ from typing import TYPE_CHECKING from django import VERSION as DJANGO_VERSION +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.utils import ( capture_internal_exceptions, ) @@ -82,9 +83,9 @@ def _check_middleware_span( middleware_span = sentry_sdk.start_span( name=description, attributes={ - "sentry.op": OP.MIDDLEWARE, - "sentry.origin": DjangoIntegration.origin, - SPANDATA.MIDDLEWARE_NAME: middleware_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: middleware_name, }, ) diff --git a/sentry_sdk/integrations/django/signals_handlers.py b/sentry_sdk/integrations/django/signals_handlers.py index 474ada516a..ee65642483 100644 --- a/sentry_sdk/integrations/django/signals_handlers.py +++ b/sentry_sdk/integrations/django/signals_handlers.py @@ -2,9 +2,10 @@ from typing import TYPE_CHECKING from django.dispatch import Signal +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.django import DJANGO_VERSION if TYPE_CHECKING: @@ -69,9 +70,9 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=signal_name, attributes={ - "sentry.op": OP.FUNCTION, - "sentry.origin": DjangoIntegration.origin, - SPANDATA.CODE_FUNCTION_NAME: signal_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.FUNCTION, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, + ATTRIBUTE_NAMES.CODE_FUNCTION_NAME: signal_name, }, ): return receiver(*args, **kwargs) diff --git a/sentry_sdk/integrations/django/tasks.py b/sentry_sdk/integrations/django/tasks.py index ea6ff89500..b3ca4b8fd6 100644 --- a/sentry_sdk/integrations/django/tasks.py +++ b/sentry_sdk/integrations/django/tasks.py @@ -1,5 +1,7 @@ from functools import wraps +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.utils import qualname_from_function @@ -38,8 +40,8 @@ def _sentry_enqueue(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=name, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return old_task_enqueue(self, *args, **kwargs) diff --git a/sentry_sdk/integrations/django/templates.py b/sentry_sdk/integrations/django/templates.py index 728e13b949..64c02a95c1 100644 --- a/sentry_sdk/integrations/django/templates.py +++ b/sentry_sdk/integrations/django/templates.py @@ -4,6 +4,7 @@ from django.template import TemplateSyntaxError from django.template.base import Origin from django.utils.safestring import mark_safe +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -60,8 +61,8 @@ def rendered_content(self: "SimpleTemplateResponse") -> str: with sentry_sdk.start_span( name=_get_template_name_description(self.template_name), attributes={ - "sentry.op": OP.TEMPLATE_RENDER, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.TEMPLATE_RENDER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return real_rendered_content.fget(self) @@ -94,8 +95,8 @@ def render( with sentry_sdk.start_span( name=_get_template_name_description(template_name), attributes={ - "sentry.op": OP.TEMPLATE_RENDER, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.TEMPLATE_RENDER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return real_render(request, template_name, context, *args, **kwargs) diff --git a/sentry_sdk/integrations/django/views.py b/sentry_sdk/integrations/django/views.py index 655a54663a..38c60ddc4f 100644 --- a/sentry_sdk/integrations/django/views.py +++ b/sentry_sdk/integrations/django/views.py @@ -1,6 +1,8 @@ import functools from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.traces import Span @@ -27,8 +29,8 @@ def sentry_patched_render(self: "SimpleTemplateResponse") -> "Any": with sentry_sdk.start_span( name="serialize response", attributes={ - "sentry.op": OP.VIEW_RENDER, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.VIEW_RENDER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return old_render(self) @@ -77,8 +79,8 @@ def sentry_wrapped_callback(request: "Any", *args: "Any", **kwargs: "Any") -> "A with sentry_sdk.start_span( name=request.resolver_match.view_name, attributes={ - "sentry.op": OP.VIEW_RENDER, - "sentry.origin": DjangoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.VIEW_RENDER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DjangoIntegration.origin, }, ): return callback(request, *args, **kwargs) diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 66c56a8cc2..65c081a630 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -1,7 +1,8 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import RequestExtractor from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware @@ -99,7 +100,7 @@ def process_resource( server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, req.uri_template) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, req.uri_template) sentry_sdk.set_transaction_name(req.uri_template, SegmentNameSource.ROUTE) diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index a448559c09..1d345c8001 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -2,8 +2,9 @@ from copy import deepcopy from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, _check_minimum_version from sentry_sdk.traces import ( SegmentNameSource, @@ -94,7 +95,7 @@ async def _wrap_async_handler( server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) sentry_sdk.get_current_scope().set_transaction_name( route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, @@ -149,7 +150,7 @@ def event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event": ) if request_body: current_span._segment.set_attribute( - SPANDATA.HTTP_REQUEST_BODY_DATA, + ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA, request_body, ) diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index e43fda0021..1a78451d38 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -1,7 +1,8 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import ( DEFAULT_HTTP_METHODS_TO_CAPTURE, @@ -130,7 +131,7 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None: server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) # Set the transaction name and source here, # but rely on WSGI middleware to actually start the transaction diff --git a/sentry_sdk/integrations/graphene.py b/sentry_sdk/integrations/graphene.py index acf205f9e2..3ff3fe8583 100644 --- a/sentry_sdk/integrations/graphene.py +++ b/sentry_sdk/integrations/graphene.py @@ -1,5 +1,7 @@ from contextlib import contextmanager +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -148,14 +150,14 @@ def graphql_span( additional_attributes = {} if client_options["data_collection"]["graphql"]["document"]: - additional_attributes["graphql.document"] = source + additional_attributes[ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] = source _graphql_span = sentry_sdk.start_span( name=operation_name, attributes={ - "sentry.op": op, - "graphql.operation.name": operation_name, - "graphql.operation.type": operation_type, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME: operation_name, + ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE: operation_type, **additional_attributes, }, ) diff --git a/sentry_sdk/integrations/litestar.py b/sentry_sdk/integrations/litestar.py index 2e142b3ca0..bea22dde19 100644 --- a/sentry_sdk/integrations/litestar.py +++ b/sentry_sdk/integrations/litestar.py @@ -1,8 +1,10 @@ from collections.abc import Set from copy import deepcopy +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.data_collection import _apply_key_value_collection_filtering from sentry_sdk.integrations import ( _DEFAULT_FAILED_REQUEST_STATUS_CODES, @@ -170,11 +172,13 @@ async def _create_span_call( with sentry_sdk.start_span( name=middleware_name, attributes={ - "sentry.op": OP.MIDDLEWARE, - "sentry.origin": LitestarIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: LitestarIntegration.origin, }, ) as middleware_span: - middleware_span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) + middleware_span.set_attribute( + ATTRIBUTE_NAMES.MIDDLEWARE_NAME, middleware_name + ) # Creating spans for the "receive" callback async def _sentry_receive( @@ -187,11 +191,11 @@ async def _sentry_receive( with sentry_sdk.start_span( name=getattr(receive, "__qualname__", str(receive)), attributes={ - "sentry.op": OP.MIDDLEWARE, - "sentry.origin": LitestarIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: LitestarIntegration.origin, }, ) as span: - span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) + span.set_attribute(ATTRIBUTE_NAMES.MIDDLEWARE_NAME, middleware_name) return await receive(*args, **kwargs) receive_name = getattr(receive, "__name__", str(receive)) @@ -207,11 +211,11 @@ async def _sentry_send(message: "Message") -> None: with sentry_sdk.start_span( name=getattr(send, "__qualname__", str(send)), attributes={ - "sentry.op": OP.MIDDLEWARE, - "sentry.origin": LitestarIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: LitestarIntegration.origin, }, ) as span: - span.set_attribute(SPANDATA.MIDDLEWARE_NAME, middleware_name) + span.set_attribute(ATTRIBUTE_NAMES.MIDDLEWARE_NAME, middleware_name) return await send(message) send_name = getattr(send, "__name__", str(send)) diff --git a/sentry_sdk/integrations/pyramid.py b/sentry_sdk/integrations/pyramid.py index 4796f4163b..3d74726f78 100644 --- a/sentry_sdk/integrations/pyramid.py +++ b/sentry_sdk/integrations/pyramid.py @@ -3,8 +3,9 @@ import sys import weakref +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import RequestExtractor from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware @@ -67,7 +68,7 @@ def sentry_patched_call_view( server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: server_span.set_attribute( - SPANDATA.HTTP_ROUTE, request.matched_route.pattern + ATTRIBUTE_NAMES.HTTP_ROUTE, request.matched_route.pattern ) try: diff --git a/sentry_sdk/integrations/quart.py b/sentry_sdk/integrations/quart.py index e8b067d173..bc98c9f3ef 100644 --- a/sentry_sdk/integrations/quart.py +++ b/sentry_sdk/integrations/quart.py @@ -4,8 +4,9 @@ from functools import wraps from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.integrations._wsgi_common import _filter_headers @@ -138,7 +139,7 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None: server_span = sentry_sdk.get_current_scope()._server_segment_span if server_span is not None and route_path is not None: - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) # Set the transaction name here, but rely on ASGI middleware # to actually start the transaction @@ -156,7 +157,9 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None: if type(current_span) is Span: segment = current_span._segment - segment.set_attribute("http.request.method", request_websocket.method) + segment.set_attribute( + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD, request_websocket.method + ) header_attributes: "dict[str, Any]" = {} for header, header_value in _filter_headers( @@ -176,11 +179,11 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None: behaviour=client_options["data_collection"]["url_query_params"], ) if filtered_query_string: - segment.set_attribute("url.query", filtered_query_string) + segment.set_attribute(ATTRIBUTE_NAMES.URL_QUERY, filtered_query_string) parsed_url = parse_url(request_websocket.url) segment.set_attribute( - "url.full", + ATTRIBUTE_NAMES.URL_FULL, f"{parsed_url.url}?{filtered_query_string}" if filtered_query_string else parsed_url.url, @@ -191,7 +194,7 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None: if len(request_websocket.access_route) >= 1: segment.set_attribute( - "client.address", request_websocket.access_route[0] + ATTRIBUTE_NAMES.CLIENT_ADDRESS, request_websocket.access_route[0] ) user_properties["ip_address"] = request_websocket.access_route[0] diff --git a/sentry_sdk/integrations/sanic.py b/sentry_sdk/integrations/sanic.py index 8fd0f7cc35..9e449f05d3 100644 --- a/sentry_sdk/integrations/sanic.py +++ b/sentry_sdk/integrations/sanic.py @@ -4,6 +4,8 @@ from typing import TYPE_CHECKING from urllib.parse import urlsplit +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.data_collection import ( @@ -135,7 +137,7 @@ async def _context_enter(request: "Request") -> None: scope.set_custom_sampling_context({"sanic_request": request}) scope.set_attribute( - SPANDATA.SENTRY_IS_LOCALHOST, + ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST, _is_localhost( client_ip=request.remote_addr or None, url_host=urlsplit(request.url).hostname, @@ -145,20 +147,20 @@ async def _context_enter(request: "Request") -> None: ) if request.remote_addr and client.options["data_collection"]["user_info"]: - scope.set_attribute(SPANDATA.USER_IP_ADDRESS, request.remote_addr) + scope.set_attribute(ATTRIBUTE_NAMES.USER_IP_ADDRESS, request.remote_addr) user_agent = request.headers.get("user-agent") if user_agent: - scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent) + scope.set_attribute(ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, user_agent) span = sentry_sdk.start_span( # Unless the request results in a 404 error, the name and source # will get overwritten in _set_transaction name=request.path, attributes={ - "sentry.op": OP.HTTP_SERVER, - "sentry.origin": SanicIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.URL.value, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SanicIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.URL.value, }, parent_span=None, ) @@ -184,7 +186,9 @@ async def _context_exit( span.set_attribute(attr, value) if response_status is not None: - span.set_attribute(SPANDATA.HTTP_STATUS_CODE, response_status) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, response_status + ) span.status = "error" if response_status >= 400 else "ok" span.end() @@ -257,7 +261,7 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]": attributes = {} # type: Dict[str, Any] if request.method: - attributes[SPANDATA.HTTP_REQUEST_METHOD] = request.method.upper() + attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = request.method.upper() headers = _filter_headers(dict(request.headers)) for header, value in headers.items(): @@ -266,7 +270,7 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]": urlparts = urlsplit(request.url) client_options = sentry_sdk.get_client().options - attributes["url.path"] = urlparts.path + attributes[ATTRIBUTE_NAMES.URL_PATH] = urlparts.path filtered_query = None if urlparts.query: @@ -275,17 +279,17 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]": behaviour=client_options["data_collection"]["url_query_params"], ) if filtered_query: - attributes[SPANDATA.HTTP_QUERY] = filtered_query + attributes[ATTRIBUTE_NAMES.HTTP_QUERY] = filtered_query - attributes[SPANDATA.URL_FULL] = urlparts._replace( + attributes[ATTRIBUTE_NAMES.URL_FULL] = urlparts._replace( query=filtered_query or "" ).geturl() if request.remote_addr and client_options["data_collection"]["user_info"]: - attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_addr + attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] = request.remote_addr if urlparts.scheme: - attributes[SPANDATA.NETWORK_PROTOCOL_NAME] = urlparts.scheme + attributes[ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME] = urlparts.scheme return attributes diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 99b486c39e..707367d40c 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -7,9 +7,11 @@ from json import JSONDecodeError from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk._types import OVER_SIZE_LIMIT_SUBSTITUTE -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.data_collection import _apply_key_value_collection_filtering from sentry_sdk.integrations import ( _DEFAULT_FAILED_REQUEST_STATUS_CODES, @@ -146,7 +148,7 @@ async def _create_span_call( and route_path is not None and name_source == SegmentNameSource.ROUTE ): - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) # Update transaction name with middleware name if route_path is not None: @@ -167,9 +169,9 @@ def _start_middleware_span(op: str, name: str) -> "Any": return sentry_sdk.start_span( name=name, attributes={ - "sentry.op": op, - "sentry.origin": StarletteIntegration.origin, - "middleware.name": middleware_name, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StarletteIntegration.origin, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: middleware_name, }, ) @@ -481,7 +483,7 @@ async def _wrap_async_handler( and route_path is not None and name_source == SegmentNameSource.ROUTE ): - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) sentry_sdk.get_current_scope().set_transaction_name( route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, @@ -530,7 +532,7 @@ def event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event": ) if request_body: current_span._segment.set_attribute( - SPANDATA.HTTP_REQUEST_BODY_DATA, + ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA, request_body, ) @@ -594,7 +596,7 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any": and route_path is not None and name_source == SegmentNameSource.ROUTE ): - server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) + server_span.set_attribute(ATTRIBUTE_NAMES.HTTP_ROUTE, route_path) sentry_sdk.get_current_scope().set_transaction_name( route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, diff --git a/sentry_sdk/integrations/starlite.py b/sentry_sdk/integrations/starlite.py index 08a6f9badc..cf926263b0 100644 --- a/sentry_sdk/integrations/starlite.py +++ b/sentry_sdk/integrations/starlite.py @@ -1,8 +1,10 @@ from contextlib import nullcontext from copy import deepcopy +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.data_collection import _apply_key_value_collection_filtering from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations.asgi import SentryAsgiMiddleware @@ -157,9 +159,9 @@ def _start_middleware_span(op: str, name: str) -> "Any": return sentry_sdk.start_span( name=name, attributes={ - "sentry.op": op, - "sentry.origin": StarliteIntegration.origin, - SPANDATA.MIDDLEWARE_NAME: middleware_name, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StarliteIntegration.origin, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: middleware_name, }, ) diff --git a/sentry_sdk/integrations/strawberry.py b/sentry_sdk/integrations/strawberry.py index 7fc2f9f15c..b14ab67fc1 100644 --- a/sentry_sdk/integrations/strawberry.py +++ b/sentry_sdk/integrations/strawberry.py @@ -1,6 +1,8 @@ import functools from inspect import isawaitable +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -167,17 +169,21 @@ def on_operation(self) -> "Generator[None, None, None]": additional_attributes: "dict[str, Any]" = {} if client.options["data_collection"]["graphql"]["document"]: - additional_attributes["graphql.document"] = self.execution_context.query + additional_attributes[ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] = ( + self.execution_context.query + ) if operation_name: - additional_attributes["graphql.operation.name"] = operation_name + additional_attributes[ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] = ( + operation_name + ) graphql_span = sentry_sdk.start_span( name=description, attributes={ - "sentry.origin": StrawberryIntegration.origin, - "sentry.op": op, - "graphql.operation.type": operation_type, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StrawberryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: op, + ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE: operation_type, **additional_attributes, }, ) @@ -187,9 +193,9 @@ def on_operation(self) -> "Generator[None, None, None]": if self.execution_context.operation_name: segment = graphql_span._segment segment.set_attribute( - "sentry.segment.name.source", SegmentNameSource.COMPONENT + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, SegmentNameSource.COMPONENT ) - segment.set_attribute("sentry.op", op) + segment.set_attribute(ATTRIBUTE_NAMES.SENTRY_OP, op) segment.name = self.execution_context.operation_name graphql_span.end() @@ -202,8 +208,8 @@ def on_validate(self) -> "Generator[None, None, None]": validation_span = sentry_sdk.start_span( name="validation", attributes={ - "sentry.op": OP.GRAPHQL_VALIDATE, - "sentry.origin": StrawberryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRAPHQL_VALIDATE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StrawberryIntegration.origin, }, ) @@ -221,8 +227,8 @@ def on_parse(self) -> "Generator[None, None, None]": parsing_span = sentry_sdk.start_span( name="parsing", attributes={ - "sentry.op": OP.GRAPHQL_PARSE, - "sentry.origin": StrawberryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRAPHQL_PARSE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StrawberryIntegration.origin, }, ) @@ -273,8 +279,8 @@ async def resolve( with sentry_sdk.start_span( name=f"resolving {field_path}", attributes={ - "sentry.origin": StrawberryIntegration.origin, - "sentry.op": OP.GRAPHQL_RESOLVE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StrawberryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRAPHQL_RESOLVE, }, ): return await self._resolve(_next, root, info, *args, **kwargs) @@ -300,8 +306,8 @@ def resolve( with sentry_sdk.start_span( name=f"resolving {field_path}", attributes={ - "sentry.origin": StrawberryIntegration.origin, - "sentry.op": OP.GRAPHQL_RESOLVE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: StrawberryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRAPHQL_RESOLVE, }, ): return _next(root, info, *args, **kwargs) diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index 9c9ccf7e8f..a410ee4cd5 100644 --- a/sentry_sdk/integrations/tornado.py +++ b/sentry_sdk/integrations/tornado.py @@ -1,6 +1,8 @@ import contextlib import weakref +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string @@ -98,7 +100,7 @@ def _handle_request_impl(self: "RequestHandler") -> "Generator[None, None, None] scope.set_custom_sampling_context({"tornado_request": self.request}) scope.set_attribute( - SPANDATA.SENTRY_IS_LOCALHOST, + ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST, _is_localhost( client_ip=_get_client_ip(self.request), host_header=self.request.headers.get("Host"), @@ -107,18 +109,18 @@ def _handle_request_impl(self: "RequestHandler") -> "Generator[None, None, None] ) if self.request.remote_ip and client.options["data_collection"]["user_info"]: - scope.set_attribute(SPANDATA.USER_IP_ADDRESS, self.request.remote_ip) + scope.set_attribute(ATTRIBUTE_NAMES.USER_IP_ADDRESS, self.request.remote_ip) user_agent = headers.get("User-Agent") if user_agent: - scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent) + scope.set_attribute(ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, user_agent) with sentry_sdk.start_span( name=_DEFAULT_ROOT_SPAN_NAME, attributes={ - "sentry.op": OP.HTTP_SERVER, - "sentry.origin": TornadoIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.ROUTE, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: TornadoIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.ROUTE, }, parent_span=None, ) as span: @@ -136,13 +138,15 @@ def _handle_request_impl(self: "RequestHandler") -> "Generator[None, None, None] if span_name: span.name = span_name span.set_attribute( - "sentry.segment.name.source", + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, SegmentNameSource.COMPONENT, ) with capture_internal_exceptions(): status_int = self.get_status() - span.set_attribute(SPANDATA.HTTP_STATUS_CODE, status_int) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, status_int + ) span.status = "error" if status_int >= 400 else "ok" @@ -162,13 +166,13 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]": client_options = sentry_sdk.get_client().options if request.method: - attributes[SPANDATA.HTTP_REQUEST_METHOD] = request.method.upper() + attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = request.method.upper() headers = _filter_headers(dict(request.headers)) for header, value in headers.items(): attributes[f"{SPANDATA.HTTP_REQUEST_HEADER}.{header.lower()}"] = [value] - attributes["url.path"] = request.path + attributes[ATTRIBUTE_NAMES.URL_PATH] = request.path filtered_query = None if request.query: @@ -177,18 +181,18 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]": behaviour=client_options["data_collection"]["url_query_params"], ) if filtered_query: - attributes[SPANDATA.URL_QUERY] = filtered_query + attributes[ATTRIBUTE_NAMES.URL_QUERY] = filtered_query parsed_url = parse_url(request.full_url()) - attributes[SPANDATA.URL_FULL] = ( + attributes[ATTRIBUTE_NAMES.URL_FULL] = ( f"{parsed_url.url}?{filtered_query}" if filtered_query else parsed_url.url ) if request.remote_ip and client_options["data_collection"]["user_info"]: - attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_ip + attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] = request.remote_ip if request.protocol: - attributes[SPANDATA.NETWORK_PROTOCOL_NAME] = request.protocol + attributes[ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME] = request.protocol record_incoming_request_data = ( "incoming_request" in client_options["data_collection"]["http_bodies"] @@ -201,7 +205,7 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]": raw_data.value if isinstance(raw_data, AnnotatedValue) else raw_data ) if body_data is not None: - attributes[SPANDATA.HTTP_REQUEST_BODY_DATA] = body_data + attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] = body_data return attributes diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index b70bfd794d..f3ed530651 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -4,9 +4,11 @@ from functools import partial from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk._werkzeug import _get_headers, get_host -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string from sentry_sdk.integrations._wsgi_common import ( DEFAULT_HTTP_METHODS_TO_CAPTURE, @@ -126,7 +128,7 @@ def __call__( ) scope.set_attribute( - SPANDATA.SENTRY_IS_LOCALHOST, + ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST, _is_localhost( client_ip=get_client_ip(environ), url_host=environ.get("SERVER_NAME") @@ -138,7 +140,8 @@ def __call__( ) if environ.get("HTTP_USER_AGENT"): scope.set_attribute( - SPANDATA.USER_AGENT_ORIGINAL, environ["HTTP_USER_AGENT"] + ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL, + environ["HTTP_USER_AGENT"], ) method = environ.get("REQUEST_METHOD", "").upper() @@ -151,14 +154,16 @@ def __call__( if client.options["data_collection"]["user_info"]: client_ip = get_client_ip(environ) if client_ip: - scope.set_attribute(SPANDATA.USER_IP_ADDRESS, client_ip) + scope.set_attribute( + ATTRIBUTE_NAMES.USER_IP_ADDRESS, client_ip + ) span_ctx = sentry_sdk.start_span( name=_DEFAULT_TRANSACTION_NAME, attributes={ - "sentry.segment.name.source": SegmentNameSource.ROUTE, - "sentry.origin": self.span_origin, - "sentry.op": OP.HTTP_SERVER, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.ROUTE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: self.span_origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_SERVER, }, parent_span=None, ) @@ -219,7 +224,7 @@ def _sentry_start_response( status_int = int(status.split(" ", 1)[0]) if span is not None: span.status = "error" if status_int >= 400 else "ok" - span.set_attribute("http.response.status_code", status_int) + span.set_attribute(ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, status_int) if exc_info is None: # The Django Rest Framework WSGI test client, and likely other @@ -392,7 +397,7 @@ def _get_request_attributes( method = environ.get("REQUEST_METHOD") if method: - attributes["http.request.method"] = method.upper() + attributes[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] = method.upper() headers = _filter_headers(dict(_get_headers(environ))) for header, value in headers.items(): @@ -400,12 +405,12 @@ def _get_request_attributes( url_scheme = environ.get("wsgi.url_scheme") if url_scheme: - attributes["network.protocol.name"] = url_scheme + attributes[ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME] = url_scheme server_port = environ.get("SERVER_PORT") if server_port: try: - attributes["server.port"] = int(server_port) + attributes[ATTRIBUTE_NAMES.SERVER_PORT] = int(server_port) except ValueError: pass @@ -419,19 +424,19 @@ def _get_request_attributes( behaviour=client_options["data_collection"]["url_query_params"], ) if filtered_qs: - attributes["http.query"] = filtered_qs + attributes[ATTRIBUTE_NAMES.HTTP_QUERY] = filtered_qs path = environ.get("PATH_INFO", "") if path: - attributes["url.path"] = path + attributes[ATTRIBUTE_NAMES.URL_PATH] = path - attributes["url.full"] = get_request_url(environ, use_x_forwarded_for) + attributes[ATTRIBUTE_NAMES.URL_FULL] = get_request_url(environ, use_x_forwarded_for) if filtered_qs is not None: - attributes["url.full"] += f"?{filtered_qs}" + attributes[ATTRIBUTE_NAMES.URL_FULL] += f"?{filtered_qs}" if client_options["data_collection"]["user_info"]: client_ip = get_client_ip(environ) if client_ip: - attributes["client.address"] = client_ip + attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] = client_ip return attributes diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 9b8bd1761f..1c5809af7e 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -15,6 +15,7 @@ HTTPUnavailableForLegalReasons, ) from aiohttp.web_request import Request +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -686,7 +687,7 @@ async def handler(request): with sentry_sdk.start_span( name="/interactions/other-dogs/new-dog", attributes={ - "sentry.op": "greeting.sniff", + ATTRIBUTE_NAMES.SENTRY_OP: "greeting.sniff", }, ): client = await aiohttp_client(raw_server) @@ -695,7 +696,9 @@ async def handler(request): sentry_sdk.flush() spans = [item.payload for item in items] (request_span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == "http.client" + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "http.client" ) assert resp.request_info.headers[ @@ -868,9 +871,9 @@ async def hello(request): assert span["name"].startswith("GET") assert SPANDATA.CODE_LINENO not in span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in span["attributes"] assert SPANDATA.CODE_FILEPATH not in span["attributes"] - assert SPANDATA.CODE_FUNCTION not in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in span["attributes"] @pytest.mark.asyncio @@ -918,10 +921,10 @@ async def hello(request): assert span["name"].startswith("GET") - assert "code.line.number" in span["attributes"] - assert "code.namespace" in span["attributes"] - assert "code.file.path" in span["attributes"] - assert "code.function" in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in span["attributes"] @pytest.mark.asyncio @@ -960,25 +963,28 @@ async def handler_with_outgoing_request(request): assert span["name"].startswith("GET") - assert "code.line.number" in span["attributes"] - assert "code.namespace" in span["attributes"] - assert "code.file.path" in span["attributes"] - assert "code.function" in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in span["attributes"] - assert type(span["attributes"]["code.line.number"]) == int - assert span["attributes"]["code.line.number"] > 0 + assert type(span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - span["attributes"]["code.namespace"] + span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.aiohttp.test_aiohttp" ) - assert span["attributes"]["code.file.path"].endswith( + assert span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/aiohttp/test_aiohttp.py" ) - is_relative_path = span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep assert is_relative_path - assert span["attributes"]["code.function"] == "handler_with_outgoing_request" + assert ( + span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "handler_with_outgoing_request" + ) @pytest.mark.asyncio @@ -1022,20 +1028,27 @@ async def handler_with_outgoing_request(request): assert span["name"].startswith("GET") - assert "code.line.number" in span["attributes"] - assert "code.namespace" in span["attributes"] - assert "code.file.path" in span["attributes"] - assert "code.function" in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in span["attributes"] - assert type(span["attributes"]["code.line.number"]) == int - assert span["attributes"]["code.line.number"] > 0 - assert span["attributes"]["code.namespace"] == "aiohttp_helpers.helpers" - assert span["attributes"]["code.file.path"] == "aiohttp_helpers/helpers.py" + assert type(span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 + assert ( + span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "aiohttp_helpers.helpers" + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH] + == "aiohttp_helpers/helpers.py" + ) - is_relative_path = span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep assert is_relative_path - assert span["attributes"]["code.function"] == "get_request_with_client" + assert ( + span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "get_request_with_client" + ) @pytest.mark.asyncio @@ -1078,9 +1091,9 @@ async def handler_with_outgoing_request(request): assert span["name"].startswith("GET") assert SPANDATA.CODE_LINENO not in span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in span["attributes"] assert SPANDATA.CODE_FILEPATH not in span["attributes"] - assert SPANDATA.CODE_FUNCTION not in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in span["attributes"] @pytest.mark.asyncio @@ -1119,25 +1132,28 @@ async def handler_with_outgoing_request(request): assert span["name"].startswith("GET") - assert "code.line.number" in span["attributes"] - assert "code.namespace" in span["attributes"] - assert "code.file.path" in span["attributes"] - assert "code.function" in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in span["attributes"] - assert type(span["attributes"]["code.line.number"]) == int - assert span["attributes"]["code.line.number"] > 0 + assert type(span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - span["attributes"]["code.namespace"] + span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.aiohttp.test_aiohttp" ) - assert span["attributes"]["code.file.path"].endswith( + assert span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/aiohttp/test_aiohttp.py" ) - is_relative_path = span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep assert is_relative_path - assert span["attributes"]["code.function"] == "handler_with_outgoing_request" + assert ( + span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "handler_with_outgoing_request" + ) @pytest.mark.asyncio @@ -1174,8 +1190,8 @@ async def hello(request): sentry_sdk.flush() (span, segment) = [item.payload for item in items] - assert span["attributes"]["sentry.origin"] == "auto.http.aiohttp" - assert segment["attributes"]["sentry.origin"] == "auto.http.aiohttp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.aiohttp" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.aiohttp" @pytest.mark.parametrize( @@ -1327,25 +1343,29 @@ async def hello(request): assert server_span["is_segment"] is True assert server_span["name"] == "/" - assert server_span["attributes"]["sentry.op"] == "http.server" - assert server_span["attributes"]["sentry.origin"] == "auto.http.aiohttp" - assert server_span["attributes"]["http.response.status_code"] == 200 - assert server_span["attributes"]["sentry.segment.name.source"] == "route" + assert server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.aiohttp" + ) + assert server_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + ) assert server_span["status"] == "ok" # No query string on the request, so the attribute should be omitted. - assert "url.query" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in server_span["attributes"] # Request attributes derived directly from the aiohttp request. - assert server_span["attributes"]["http.request.method"] == "GET" + assert server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" - assert "client.address" in server_span["attributes"] - assert "user.ip_address" in server_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS in server_span["attributes"] - url_full = server_span["attributes"]["url.full"] + url_full = server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] assert url_full.startswith("http://127.0.0.1:") assert url_full.endswith("/") - url_path = server_span["attributes"]["url.path"] + url_path = server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] assert url_path == "/" # aiohttp's test client always sends a Host header; we assert it propagates @@ -1379,14 +1399,16 @@ async def hello(request): sentry_sdk.flush() (server_span,) = [item.payload for item in items] - assert server_span["attributes"]["sentry.origin"] == "auto.http.aiohttp" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.aiohttp" + ) if expect_ip: - assert server_span["attributes"]["client.address"] == "127.0.0.1" - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" else: - assert "client.address" not in server_span["attributes"] - assert "user.ip_address" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] @pytest.mark.asyncio @@ -1459,7 +1481,10 @@ async def hello(request): assert server_segment["name"] == "/{var}" assert server_segment["is_segment"] - assert server_segment["attributes"]["sentry.segment.name.source"] == "route" + assert ( + server_segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "route" + ) @pytest.mark.asyncio @@ -1494,7 +1519,7 @@ async def hello(request): sentry_sdk.flush() (segment,) = (item.payload for item in items if item.payload.get("is_segment")) - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == expected_route + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == expected_route @pytest.mark.asyncio @@ -1530,8 +1555,8 @@ async def hello(request): # The integration's generic Exception path reraises without recording # http.response.status_code on the server span. Span.__exit__ # observes the propagating exception and sets status to "error". - assert server_span["attributes"]["sentry.op"] == "http.server" - assert "http.response.status_code" not in server_span["attributes"] + assert server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE not in server_span["attributes"] assert server_span["status"] == "error" @@ -1559,8 +1584,8 @@ async def hello(request): assert len(items) == 1 (server_span,) = [item.payload for item in items] - assert server_span["attributes"]["sentry.op"] == "http.server" - assert server_span["attributes"]["http.response.status_code"] == 403 + assert server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert server_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 403 assert server_span["status"] == "error" @@ -1590,8 +1615,8 @@ async def hello(request): assert len(items) == 1 (server_span,) = [item.payload for item in items] - assert server_span["attributes"]["sentry.op"] == "http.server" - assert server_span["attributes"]["http.response.status_code"] == 302 + assert server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert server_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 302 assert server_span["status"] == "ok" @@ -1647,25 +1672,31 @@ async def hello(request): inner_client_span = items[0].payload server_span = items[1].payload - assert server_span["attributes"]["sentry.op"] == "http.server" + assert server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" assert inner_client_span["is_segment"] is False assert inner_client_span["name"].startswith("GET ") - assert inner_client_span["attributes"]["sentry.op"] == "http.client" - assert inner_client_span["attributes"]["sentry.origin"] == "auto.http.aiohttp" - assert inner_client_span["attributes"]["http.request.method"] == "GET" - assert inner_client_span["attributes"]["http.response.status_code"] == 200 + assert inner_client_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" + assert ( + inner_client_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.aiohttp" + ) + assert inner_client_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert ( + inner_client_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] + == 200 + ) assert inner_client_span["status"] == "ok" if expect_query: - assert inner_client_span["attributes"]["url.query"] == "foo=bar" + assert inner_client_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "foo=bar" else: - assert "url.query" not in inner_client_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in inner_client_span["attributes"] - url_full = inner_client_span["attributes"]["url.full"] + url_full = inner_client_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] assert url_full.startswith("http://127.0.0.1:") assert ("?foo=bar" in url_full) == expect_query - assert inner_client_span["attributes"]["url.path"] == "/" + assert inner_client_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/" @pytest.mark.asyncio @@ -1733,15 +1764,17 @@ async def hello(request): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["sentry.segment.name.source"] == "route" - assert "sentry.segment.name.source" not in child_span["attributes"] + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + ) + assert ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE not in child_span["attributes"] if expect_user_info: - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert child_span["attributes"]["user.ip_address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert child_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" else: - assert "user.ip_address" not in server_span["attributes"] - assert "user.ip_address" not in child_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in child_span["attributes"] _QUERY_PARAM_DATA_COLLECTION_CASES = [ @@ -1803,9 +1836,9 @@ async def hello(request): (server_span,) = [item.payload for item in items] if expected_query is None: - assert "url.query" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in server_span["attributes"] else: - assert server_span["attributes"]["url.query"] == expected_query + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.asyncio @@ -1849,9 +1882,11 @@ async def hello(request): inner_client_span = items[0].payload if expected_query is None: - assert "url.query" not in inner_client_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in inner_client_span["attributes"] else: - assert inner_client_span["attributes"]["url.query"] == expected_query + assert ( + inner_client_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query + ) @pytest.mark.asyncio @@ -1967,8 +2002,14 @@ async def hello(request): assert len(server_spans) == 1 assert len(child_spans) == 1 - assert server_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost - assert child_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost + assert ( + server_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] + == is_localhost + ) + assert ( + child_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] + == is_localhost + ) @pytest.mark.asyncio @@ -2001,8 +2042,10 @@ async def hello(request): assert len(child_spans) == 1 assert ( - server_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0" + server_spans[0]["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" ) assert ( - child_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0" + child_spans[0]["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" ) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index c00a1db15e..088ecc2e25 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -2,6 +2,7 @@ import pytest from async_asgi_testclient import TestClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -207,14 +208,17 @@ async def test_capture_transaction( assert span["is_segment"] is True assert span["name"] == "/some_url" - assert span["attributes"]["sentry.segment.name.source"] == "url" - assert span["attributes"]["sentry.op"] == "http.server" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "url" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" - assert span["attributes"]["network.protocol.name"] == "http" - assert span["attributes"]["http.request.method"] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME] == "http" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" assert span["attributes"]["http.request.header.host"] == ["localhost"] assert span["attributes"]["http.request.header.user-agent"] == ["ASGI-Test-Client"] - assert span["attributes"]["url.full"] == "http://localhost/some_url?somevalue=123" + assert ( + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == "http://localhost/some_url?somevalue=123" + ) @pytest.mark.asyncio @@ -433,7 +437,7 @@ async def test_websocket( assert span.type == "span" span = span.payload assert span["name"] == request_url - assert span["attributes"]["sentry.segment.name.source"] == "url" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "url" @pytest.mark.asyncio @@ -506,7 +510,7 @@ async def test_fallback_segment_name_and_source( span = items[0].payload assert span["name"] == "generic ASGI request" - assert span["attributes"]["sentry.segment.name.source"] == "route" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" def mock_asgi2_app(): @@ -781,7 +785,9 @@ async def test_get_request_attributes_url_with_filtered_host( attributes = items[0].payload["attributes"] assert attributes["http.request.header.host"] == ["[Filtered]"] - assert attributes["url.full"] == "http://example.com/foo?somevalue=123" + assert ( + attributes[ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/foo?somevalue=123" + ) @pytest.mark.asyncio @@ -807,7 +813,9 @@ async def test_get_request_attributes_url_with_headers_off( attributes = items[0].payload["attributes"] assert not any(key.startswith("http.request.header.") for key in attributes) - assert attributes["url.full"] == "http://example.com/foo?somevalue=123" + assert ( + attributes[ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/foo?somevalue=123" + ) QUERY_STRING = "token=abc&theme=dark&lang=en&session=xyz" @@ -880,16 +888,16 @@ async def test_get_request_attributes_query_data_collection( attributes = items[0].payload["attributes"] if expected_query is None: - assert "http.query" not in attributes + assert ATTRIBUTE_NAMES.HTTP_QUERY not in attributes else: - assert attributes["http.query"] == expected_query + assert attributes[ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query if expected_url_full is None: - assert "url.full" not in attributes - assert "url.path" not in attributes + assert ATTRIBUTE_NAMES.URL_FULL not in attributes + assert ATTRIBUTE_NAMES.URL_PATH not in attributes else: - assert attributes["url.full"] == expected_url_full - assert attributes["url.path"] == "/foo" + assert attributes[ATTRIBUTE_NAMES.URL_FULL] == expected_url_full + assert attributes[ATTRIBUTE_NAMES.URL_PATH] == "/foo" USER_INFO_CASES = [ @@ -939,9 +947,9 @@ async def test_get_request_attributes_client_address_user_info( attributes = items[0].payload["attributes"] if expect_ip: - assert attributes["client.address"] == "127.0.0.1" + assert attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert "client.address" not in attributes + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in attributes @pytest.mark.asyncio @@ -970,7 +978,7 @@ async def test_segment_name_and_source( span = items[0].payload assert span["name"] == "/message/123456" - assert span["attributes"]["sentry.segment.name.source"] == "url" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "url" @pytest.mark.asyncio @@ -1020,7 +1028,7 @@ async def test_custom_transaction_name( assert span["is_segment"] is True assert span["name"] == "foobar" - assert span["attributes"]["sentry.segment.name.source"] == "custom" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" @pytest.mark.asyncio @@ -1052,11 +1060,11 @@ async def wrapped_app(scope, receive, send): child_span, server_span = [item.payload for item in items] if expect_ip: - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert child_span["attributes"]["user.ip_address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert child_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" else: - assert "user.ip_address" not in server_span["attributes"] - assert "user.ip_address" not in child_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in child_span["attributes"] @pytest.mark.parametrize( @@ -1110,8 +1118,10 @@ async def wrapped_app(scope, receive, send): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["sentry.is_localhost"] is is_localhost - assert child_span["attributes"]["sentry.is_localhost"] is is_localhost + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost + ) + assert child_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost @pytest.mark.asyncio @@ -1134,5 +1144,11 @@ async def test_user_agent_original_attribute( child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" - assert child_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + ) + assert ( + child_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + ) diff --git a/tests/integrations/bottle/test_bottle.py b/tests/integrations/bottle/test_bottle.py index fb968e9224..c25c10e2c5 100644 --- a/tests/integrations/bottle/test_bottle.py +++ b/tests/integrations/bottle/test_bottle.py @@ -5,12 +5,12 @@ import pytest from bottle import Bottle, HTTPResponse, abort, redirect from bottle import debug as set_debug +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client from werkzeug.wrappers import Response import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.bottle import BottleIntegration from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.serializer import MAX_DATABAG_BREADTH @@ -453,7 +453,7 @@ def test_span_origin( spans = [item.payload for item in items] segment = spans[-1] assert segment["is_segment"] is True - assert segment["attributes"]["sentry.origin"] == "auto.http.bottle" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.bottle" def test_basic(sentry_init, capture_items): @@ -483,10 +483,10 @@ def hi(): assert segment["is_segment"] is True assert "parent_span_id" not in segment assert segment["status"] == "ok" - assert segment["attributes"]["sentry.op"] == "http.server" - assert segment["attributes"]["sentry.origin"] == "auto.http.bottle" - assert segment["attributes"]["http.request.method"] == "GET" - assert segment["attributes"]["http.response.status_code"] == 200 + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.bottle" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 assert segment["name"] == "/message" @@ -537,7 +537,10 @@ def named_hi(): assert segment["is_segment"] is True assert segment["name"].endswith(expected_name) - assert segment["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) def test_http_route( @@ -562,7 +565,7 @@ def hi_with_id(message_id): sentry_sdk.flush() (segment,) = (item.payload for item in items if item.payload.get("is_segment")) - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message/" @pytest.mark.parametrize("debug", (True, False), ids=["debug", "nodebug"]) @@ -656,7 +659,9 @@ def handle(): assert segment["is_segment"] is True assert segment["status"] == expected_span_status - assert segment["attributes"]["http.response.status_code"] == status_code + assert ( + segment["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == status_code + ) @pytest.mark.parametrize("raise_error", [True, False]) diff --git a/tests/integrations/django/asgi/test_asgi.py b/tests/integrations/django/asgi/test_asgi.py index f81553ded0..132936d51b 100644 --- a/tests/integrations/django/asgi/test_asgi.py +++ b/tests/integrations/django/asgi/test_asgi.py @@ -8,10 +8,11 @@ import django import pytest from channels.testing import HttpCommunicator +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.django.asgi import _asgi_middleware_mixin_factory from tests.integrations.django.myapp.asgi import channels_application @@ -154,7 +155,7 @@ async def test_http_route( sentry_sdk.flush() (segment,) = (item.payload for item in items if item.payload.get("is_segment")) - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/async_message" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/async_message" @pytest.mark.asyncio @@ -271,7 +272,11 @@ async def test_async_middleware_spans( # Filter out signal-receiver spans — their ordering depends on Django # module import order and is not what this middleware test verifies. - spans = [s for s in spans if s["attributes"].get("sentry.op") != OP.FUNCTION] + spans = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) != OP.FUNCTION + ] assert ( render_span_tree(spans) @@ -784,7 +789,7 @@ async def test_transaction_http_method_default( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[5]["attributes"]["http.request.method"] == "GET" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" @pytest.mark.parametrize("application", APPS) @@ -825,8 +830,8 @@ async def test_transaction_http_method_custom( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[5]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS" - assert spans[11]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "OPTIONS" + assert spans[11]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "HEAD" @pytest.mark.asyncio diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 4c6cd88f91..f360bd6816 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -22,10 +22,12 @@ except ImportError: from django.core.urlresolvers import reverse +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk import capture_exception, capture_message from sentry_sdk._compat import PY310 -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.django import ( DjangoIntegration, DjangoRequestExtractor, @@ -356,9 +358,9 @@ def test_materialized_user_captured( spans = [item.payload for item in items] (span,) = (span for span in spans if span["name"] == "/template-test") - assert span["attributes"][SPANDATA.USER_ID] == "1" - assert span["attributes"][SPANDATA.USER_EMAIL] == "lennon@thebeatles.com" - assert span["attributes"][SPANDATA.USER_NAME] == "john" + assert span["attributes"][ATTRIBUTE_NAMES.USER_ID] == "1" + assert span["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "lennon@thebeatles.com" + assert span["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "john" @pytest.mark.forked @@ -705,9 +707,9 @@ def test_django_connect_trace( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db": + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db": attributes = span.get("attributes") - assert attributes.get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" + assert attributes.get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" assert '- sentry.op="db": name="connect"' in render_span_tree(spans) @@ -790,18 +792,18 @@ def test_db_connection_span_data( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db": + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db": attributes = span.get("attributes") - assert attributes.get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" + assert attributes.get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" conn_params = connections["postgres"].get_connection_params() - assert attributes.get(SPANDATA.DB_NAMESPACE) is not None - assert attributes.get(SPANDATA.DB_NAMESPACE) == conn_params.get( + assert attributes.get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert attributes.get(ATTRIBUTE_NAMES.DB_NAMESPACE) == conn_params.get( "database" ) or conn_params.get("dbname") - assert attributes.get(SPANDATA.SERVER_ADDRESS) == os.environ.get( + assert attributes.get(ATTRIBUTE_NAMES.SERVER_ADDRESS) == os.environ.get( "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" ) - assert attributes.get(SPANDATA.SERVER_PORT) == os.environ.get( + assert attributes.get(ATTRIBUTE_NAMES.SERVER_PORT) == os.environ.get( "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" ) @@ -853,7 +855,10 @@ def test_segment_name( spans = [item.payload for item in items if item.type == "span"] assert spans[2]["is_segment"] is True - assert spans[2]["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) (event,) = (item.payload for item in items if item.type == "event") @@ -905,7 +910,7 @@ def test_http_route( sentry_sdk.flush() (segment,) = (item.payload for item in items if item.payload.get("is_segment")) - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message" def test_request_body( @@ -1576,10 +1581,10 @@ def test_signals_spans( """ ) - assert spans[0]["attributes"]["sentry.op"] == OP.FUNCTION + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION assert spans[0]["name"] == "django.db.reset_queries" - assert spans[1]["attributes"]["sentry.op"] == OP.FUNCTION + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION assert spans[1]["name"] == "django.db.close_old_connections" @@ -1641,13 +1646,13 @@ def test_signals_spans_filtering( """ ) - assert spans[0]["attributes"]["sentry.op"] == OP.FUNCTION + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION assert spans[0]["name"] == "django.db.reset_queries" - assert spans[1]["attributes"]["sentry.op"] == OP.FUNCTION + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION assert spans[1]["name"] == "django.db.close_old_connections" - assert spans[2]["attributes"]["sentry.op"] == OP.FUNCTION + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION assert spans[2]["name"] == "tests.integrations.django.myapp.signals.signal_handler" @@ -1789,11 +1794,11 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[-1]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[-1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" for span in spans: - assert span["attributes"]["sentry.origin"] == "auto.http.django" - if span["attributes"]["sentry.op"] == OP.FUNCTION: + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.FUNCTION: signal_span_found = True assert signal_span_found @@ -1820,7 +1825,7 @@ def test_transaction_http_method_default( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[2]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" def test_transaction_http_method_custom( @@ -1847,14 +1852,14 @@ def test_transaction_http_method_custom( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[2]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "OPTIONS" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "OPTIONS" client.head("/nomessage") sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[5]["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "HEAD" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "HEAD" def test_get_frame_name_when_in_lazy_object(): diff --git a/tests/integrations/django/test_cache_module.py b/tests/integrations/django/test_cache_module.py index 36dca9a02f..ce93747d36 100644 --- a/tests/integrations/django/test_cache_module.py +++ b/tests/integrations/django/test_cache_module.py @@ -10,6 +10,8 @@ except ImportError: from django.core.urlresolvers import reverse +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.django.caching import _get_span_description @@ -199,46 +201,46 @@ def test_cache_spans_middleware( sentry_sdk.flush() spans = [item.payload for item in items] # first_event - cache.get - assert spans[0]["attributes"]["sentry.op"] == "cache.get" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[0]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[0]["attributes"]["network.peer.address"] is not None - assert spans[0]["attributes"]["cache.key"][0].startswith( + assert spans[0]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_header." ) - assert not spans[0]["attributes"]["cache.hit"] - assert "cache.item_size" not in spans[0]["attributes"] + assert not spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in spans[0]["attributes"] # first_event - cache.put - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[1]["attributes"]["network.peer.address"] is not None - assert spans[1]["attributes"]["cache.key"][0].startswith( + assert spans[1]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_header." ) - assert "cache.hit" not in spans[1]["attributes"] - assert spans[1]["attributes"]["cache.item_size"] == 2 + assert ATTRIBUTE_NAMES.CACHE_HIT not in spans[1]["attributes"] + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 2 client.get(reverse("not_cached_view")) sentry_sdk.flush() spans = [item.payload for item in items] # second_event - cache.get - assert spans[4]["attributes"]["sentry.op"] == "cache.get" + assert spans[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[4]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[4]["attributes"]["network.peer.address"] is not None - assert spans[4]["attributes"]["cache.key"][0].startswith( + assert spans[4]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_header." ) - assert spans[4]["attributes"]["cache.hit"] - assert spans[4]["attributes"]["cache.item_size"] == 2 + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 2 # second_event - cache.get 2 - assert spans[5]["attributes"]["sentry.op"] == "cache.get" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[5]["name"].startswith("views.decorators.cache.cache_page.") - assert spans[5]["attributes"]["network.peer.address"] is not None - assert spans[5]["attributes"]["cache.key"][0].startswith( + assert spans[5]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_page." ) - assert spans[5]["attributes"]["cache.hit"] - assert spans[5]["attributes"]["cache.item_size"] == 58 + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 58 @pytest.mark.forked @@ -266,9 +268,9 @@ def test_cache_spans_enabled_by_default( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.op"] == "cache.get" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[0]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"].startswith("views.decorators.cache.cache_header.") @@ -296,36 +298,36 @@ def test_cache_spans_decorator( sentry_sdk.flush() spans = [item.payload for item in items] # first_event - cache.get - assert spans[0]["attributes"]["sentry.op"] == "cache.get" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[0]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[0]["attributes"]["network.peer.address"] is not None - assert spans[0]["attributes"]["cache.key"][0].startswith( + assert spans[0]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_header." ) - assert not spans[0]["attributes"]["cache.hit"] - assert "cache.item_size" not in spans[0]["attributes"] + assert not spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in spans[0]["attributes"] # first_event - cache.put - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"].startswith("views.decorators.cache.cache_header.") - assert spans[1]["attributes"]["network.peer.address"] is not None - assert spans[1]["attributes"]["cache.key"][0].startswith( + assert spans[1]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_header." ) - assert "cache.hit" not in spans[1]["attributes"] - assert spans[1]["attributes"]["cache.item_size"] == 2 + assert ATTRIBUTE_NAMES.CACHE_HIT not in spans[1]["attributes"] + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 2 client.get(reverse("cached_view")) sentry_sdk.flush() spans = [item.payload for item in items] # second_event - cache.get - assert spans[5]["attributes"]["sentry.op"] == "cache.get" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[5]["name"].startswith("views.decorators.cache.cache_page.") - assert spans[5]["attributes"]["network.peer.address"] is not None - assert spans[5]["attributes"]["cache.key"][0].startswith( + assert spans[5]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "views.decorators.cache.cache_page." ) - assert spans[5]["attributes"]["cache.hit"] - assert spans[5]["attributes"]["cache.item_size"] == 58 + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 58 @pytest.mark.forked @@ -352,23 +354,23 @@ def test_cache_spans_templatetag( sentry_sdk.flush() spans = [item.payload for item in items] # first_event - cache.get - assert spans[0]["attributes"]["sentry.op"] == "cache.get" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[0]["name"].startswith("template.cache.some_identifier.") - assert spans[0]["attributes"]["network.peer.address"] is not None - assert spans[0]["attributes"]["cache.key"][0].startswith( + assert spans[0]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "template.cache.some_identifier." ) - assert not spans[0]["attributes"]["cache.hit"] - assert "cache.item_size" not in spans[0]["attributes"] + assert not spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in spans[0]["attributes"] # first_event - cache.put - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"].startswith("template.cache.some_identifier.") - assert spans[1]["attributes"]["network.peer.address"] is not None - assert spans[1]["attributes"]["cache.key"][0].startswith( + assert spans[1]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "template.cache.some_identifier." ) - assert "cache.hit" not in spans[1]["attributes"] - assert spans[1]["attributes"]["cache.item_size"] == 51 + assert ATTRIBUTE_NAMES.CACHE_HIT not in spans[1]["attributes"] + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 51 client.get(reverse("view_with_cached_template_fragment")) sentry_sdk.flush() @@ -376,14 +378,14 @@ def test_cache_spans_templatetag( assert len(spans) == 5 # second_event - cache.get - assert spans[3]["attributes"]["sentry.op"] == "cache.get" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[3]["name"].startswith("template.cache.some_identifier.") - assert spans[3]["attributes"]["network.peer.address"] is not None - assert spans[3]["attributes"]["cache.key"][0].startswith( + assert spans[3]["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] is not None + assert spans[3]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY][0].startswith( "template.cache.some_identifier." ) - assert spans[3]["attributes"]["cache.hit"] - assert spans[3]["attributes"]["cache.item_size"] == 51 + assert spans[3]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[3]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 51 @pytest.mark.parametrize( @@ -450,9 +452,10 @@ def test_cache_spans_location_with_port( continue assert ( - span["attributes"]["network.peer.address"] == "redis://127.0.0.1" + span["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] + == "redis://127.0.0.1" ) # Note: the username/password are not included in the address - assert span["attributes"]["network.peer.port"] == 6379 + assert span["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6379 @pytest.mark.forked @@ -485,8 +488,11 @@ def test_cache_spans_location_without_port( if span["is_segment"] is True: continue - assert span["attributes"]["network.peer.address"] == "redis://example.com" - assert "network.peer.port" not in span["attributes"] + assert ( + span["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] + == "redis://example.com" + ) + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in span["attributes"] @pytest.mark.forked @@ -517,8 +523,8 @@ def test_cache_spans_location_with_cluster( for span in spans: # because it is a cluster we do not know what host is actually accessed, so we omit the data - assert "network.peer.address" not in span["attributes"].keys() - assert "network.peer.port" not in span["attributes"].keys() + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in span["attributes"].keys() + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in span["attributes"].keys() @pytest.mark.forked @@ -545,17 +551,17 @@ def test_cache_spans_item_size( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.op"] == "cache.get" - assert not spans[0]["attributes"]["cache.hit"] - assert "cache.item_size" not in spans[0]["attributes"] + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert not spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in spans[0]["attributes"] - assert spans[1]["attributes"]["sentry.op"] == "cache.put" - assert "cache.hit" not in spans[1]["attributes"] - assert spans[1]["attributes"]["cache.item_size"] == 2 + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" + assert ATTRIBUTE_NAMES.CACHE_HIT not in spans[1]["attributes"] + assert spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 2 - assert spans[2]["attributes"]["sentry.op"] == "cache.put" - assert "cache.hit" not in spans[2]["attributes"] - assert spans[2]["attributes"]["cache.item_size"] == 58 + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" + assert ATTRIBUTE_NAMES.CACHE_HIT not in spans[2]["attributes"] + assert spans[2]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 58 client.get(reverse("cached_view")) sentry_sdk.flush() @@ -563,13 +569,13 @@ def test_cache_spans_item_size( assert len(spans) == 7 - assert spans[4]["attributes"]["sentry.op"] == "cache.get" - assert spans[4]["attributes"]["cache.hit"] - assert spans[4]["attributes"]["cache.item_size"] == 2 + assert spans[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 2 - assert spans[5]["attributes"]["sentry.op"] == "cache.get" - assert spans[5]["attributes"]["cache.hit"] - assert spans[5]["attributes"]["cache.item_size"] == 58 + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 58 @pytest.mark.forked @@ -613,29 +619,29 @@ def test_cache_spans_get_custom_default( spans = [item.payload for item in items] assert len(spans) == 9 - assert spans[0]["attributes"]["sentry.op"] == "cache.put" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[0]["name"] == f"S{id}" - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"] == f"S{id + 1}" for span in (spans[2], spans[3]): - assert span["attributes"]["sentry.op"] == "cache.get" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert span["name"] == f"S{id}" - assert span["attributes"]["cache.hit"] - assert span["attributes"]["cache.item_size"] == 10 + assert span["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert span["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 10 for span in (spans[4], spans[5]): - assert span["attributes"]["sentry.op"] == "cache.get" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert span["name"] == f"S{id + 1}" - assert span["attributes"]["cache.hit"] - assert span["attributes"]["cache.item_size"] == 0 + assert span["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert span["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 0 for span in (spans[6], spans[7]): - assert span["attributes"]["sentry.op"] == "cache.get" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert span["name"] == f"S{id + 2}" - assert not span["attributes"]["cache.hit"] - assert "cache.item_size" not in span["attributes"] + assert not span["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in span["attributes"] @pytest.mark.forked @@ -671,32 +677,32 @@ def test_cache_spans_get_many( spans = [item.payload for item in items] assert len(spans) == 8 - assert spans[2]["attributes"]["sentry.op"] == "cache.get" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[2]["name"] == f"S{id}, S{id + 1}" - assert not spans[2]["attributes"]["cache.hit"] + assert not spans[2]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] - assert spans[0]["attributes"]["sentry.op"] == "cache.get" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[0]["name"] == f"S{id}" - assert not spans[0]["attributes"]["cache.hit"] + assert not spans[0]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] - assert spans[1]["attributes"]["sentry.op"] == "cache.get" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[1]["name"] == f"S{id + 1}" - assert not spans[1]["attributes"]["cache.hit"] + assert not spans[1]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] - assert spans[3]["attributes"]["sentry.op"] == "cache.put" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[3]["name"] == f"S{id}" - assert spans[6]["attributes"]["sentry.op"] == "cache.get" + assert spans[6]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[6]["name"] == f"S{id}, S{id + 1}" - assert spans[6]["attributes"]["cache.hit"] + assert spans[6]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] - assert spans[4]["attributes"]["sentry.op"] == "cache.get" + assert spans[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[4]["name"] == f"S{id}" - assert spans[4]["attributes"]["cache.hit"] + assert spans[4]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] - assert spans[5]["attributes"]["sentry.op"] == "cache.get" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[5]["name"] == f"S{id + 1}" - assert not spans[5]["attributes"]["cache.hit"] + assert not spans[5]["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] @pytest.mark.forked @@ -731,16 +737,16 @@ def test_cache_spans_set_many( spans = [item.payload for item in items] assert len(spans) == 5 - assert spans[2]["attributes"]["sentry.op"] == "cache.put" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[2]["name"] == f"S{id}, S{id + 1}" - assert spans[0]["attributes"]["sentry.op"] == "cache.put" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[0]["name"] == f"S{id}" - assert spans[1]["attributes"]["sentry.op"] == "cache.put" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert spans[1]["name"] == f"S{id + 1}" - assert spans[3]["attributes"]["sentry.op"] == "cache.get" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert spans[3]["name"] == f"S{id}" @@ -770,11 +776,11 @@ def test_span_origin_cache( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[1]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" for span in spans: - assert span["attributes"]["sentry.origin"] == "auto.http.django" - if span["attributes"]["sentry.op"].startswith("cache."): + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP].startswith("cache."): cache_span_found = True assert cache_span_found diff --git a/tests/integrations/django/test_data_scrubbing.py b/tests/integrations/django/test_data_scrubbing.py index baf11ae640..7bed57b0c5 100644 --- a/tests/integrations/django/test_data_scrubbing.py +++ b/tests/integrations/django/test_data_scrubbing.py @@ -1,8 +1,8 @@ import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.django import DjangoIntegration from tests.conftest import unpack_werkzeug_response, werkzeug_set_cookie from tests.integrations.django.myapp.wsgi import application @@ -201,9 +201,9 @@ def test_span_http_query_data_collection( (root_span,) = (span for span in spans if span["name"] == "/message") if expected_query is None: - assert SPANDATA.HTTP_QUERY not in root_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_QUERY not in root_span["attributes"] else: - assert root_span["attributes"][SPANDATA.HTTP_QUERY] == expected_query + assert root_span["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query @pytest.mark.forked @@ -248,11 +248,11 @@ def test_user_info_span_attributes_data_collection( root_span = spans[-1] if expect_ip: - assert root_span["attributes"][SPANDATA.USER_IP_ADDRESS] == "127.0.0.1" - assert root_span["attributes"]["client.address"] == "127.0.0.1" + assert root_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert root_span["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert SPANDATA.USER_IP_ADDRESS not in root_span["attributes"] - assert "client.address" not in root_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in root_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in root_span["attributes"] @pytest.mark.forked @@ -279,13 +279,13 @@ def test_user_identity_span_attributes_data_collection( (span,) = (s for s in spans if s["name"] == "/template-test") if expect_user: - assert span["attributes"][SPANDATA.USER_ID] == "1" - assert span["attributes"][SPANDATA.USER_EMAIL] == "lennon@thebeatles.com" - assert span["attributes"][SPANDATA.USER_NAME] == "john" + assert span["attributes"][ATTRIBUTE_NAMES.USER_ID] == "1" + assert span["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "lennon@thebeatles.com" + assert span["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "john" else: - assert SPANDATA.USER_ID not in span["attributes"] - assert SPANDATA.USER_EMAIL not in span["attributes"] - assert SPANDATA.USER_NAME not in span["attributes"] + assert ATTRIBUTE_NAMES.USER_ID not in span["attributes"] + assert ATTRIBUTE_NAMES.USER_EMAIL not in span["attributes"] + assert ATTRIBUTE_NAMES.USER_NAME not in span["attributes"] @pytest.mark.forked diff --git a/tests/integrations/django/test_db_query_data.py b/tests/integrations/django/test_db_query_data.py index dcdf3af23e..4f072517e5 100644 --- a/tests/integrations/django/test_db_query_data.py +++ b/tests/integrations/django/test_db_query_data.py @@ -11,10 +11,10 @@ except ImportError: from django.core.urlresolvers import reverse +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.tracing_utils import ( record_sql_queries, @@ -61,15 +61,15 @@ def test_query_source_disabled( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER not in attributes - assert SPANDATA.CODE_NAMESPACE not in attributes - assert SPANDATA.CODE_FILE_PATH not in attributes - assert SPANDATA.CODE_FUNCTION not in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in attributes break else: raise AssertionError("No db span found") @@ -111,15 +111,15 @@ def test_query_source_enabled( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes break else: raise AssertionError("No db span found") @@ -154,31 +154,35 @@ def test_query_source( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes - assert type(attributes.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert attributes.get(SPANDATA.CODE_LINE_NUMBER) > 0 + assert type(attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 assert ( - attributes.get(SPANDATA.CODE_NAMESPACE) + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "tests.integrations.django.myapp.views" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/django/myapp/views.py" ) - is_relative_path = attributes.get(SPANDATA.CODE_FILE_PATH)[0] != os.sep + is_relative_path = ( + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep + ) assert is_relative_path - assert attributes.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "postgres_select_orm" + ) break else: raise AssertionError("No db span found") @@ -220,25 +224,34 @@ def test_query_source_with_module_in_search_path( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes - assert type(attributes.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert attributes.get(SPANDATA.CODE_LINE_NUMBER) > 0 - assert attributes.get(SPANDATA.CODE_NAMESPACE) == "django_helpers.views" - assert attributes.get(SPANDATA.CODE_FILE_PATH) == "django_helpers/views.py" + assert type(attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "django_helpers.views" + ) + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH) + == "django_helpers/views.py" + ) - is_relative_path = attributes.get(SPANDATA.CODE_FILE_PATH)[0] != os.sep + is_relative_path = ( + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep + ) assert is_relative_path - assert attributes.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "postgres_select_orm" + ) break else: raise AssertionError("No db span found") @@ -274,38 +287,38 @@ def test_query_source_with_in_app_exclude( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes - assert type(attributes.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert attributes.get(SPANDATA.CODE_LINE_NUMBER) > 0 + assert type(attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 if DJANGO_VERSION >= (1, 11): assert ( - attributes.get(SPANDATA.CODE_NAMESPACE) + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "tests.integrations.django.myapp.settings" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/django/myapp/settings.py" ) - assert attributes.get(SPANDATA.CODE_FUNCTION) == "middleware" + assert attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "middleware" else: assert ( - attributes.get(SPANDATA.CODE_NAMESPACE) + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "tests.integrations.django.test_db_query_data" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/django/test_db_query_data.py" ) assert ( - attributes.get(SPANDATA.CODE_FUNCTION) + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "test_query_source_with_in_app_exclude" ) break @@ -343,27 +356,27 @@ def test_query_source_with_in_app_include( spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes - assert type(attributes.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert attributes.get(SPANDATA.CODE_LINE_NUMBER) > 0 + assert type(attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 assert ( - attributes.get(SPANDATA.CODE_NAMESPACE) + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "django.db.models.sql.compiler" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "django/db/models/sql/compiler.py" ) - assert attributes.get(SPANDATA.CODE_FUNCTION) == "execute_sql" + assert attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "execute_sql" break else: raise AssertionError("No db span found") @@ -423,15 +436,15 @@ def __exit__(self, type, value, traceback): spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER not in attributes - assert SPANDATA.CODE_NAMESPACE not in attributes - assert SPANDATA.CODE_FILE_PATH not in attributes - assert SPANDATA.CODE_FUNCTION not in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in attributes break else: raise AssertionError("No db span found") @@ -491,31 +504,35 @@ def __exit__(self, type, value, traceback): spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and "auth_user" in span.get( - "name" - ): + if span["attributes"].get( + ATTRIBUTE_NAMES.SENTRY_OP + ) == "db" and "auth_user" in span.get("name"): attributes = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in attributes - assert SPANDATA.CODE_NAMESPACE in attributes - assert SPANDATA.CODE_FILE_PATH in attributes - assert SPANDATA.CODE_FUNCTION in attributes + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in attributes + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in attributes + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in attributes + assert ATTRIBUTE_NAMES.CODE_FUNCTION in attributes - assert type(attributes.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert attributes.get(SPANDATA.CODE_LINE_NUMBER) > 0 + assert type(attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert attributes.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 assert ( - attributes.get(SPANDATA.CODE_NAMESPACE) + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "tests.integrations.django.myapp.views" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/django/myapp/views.py" ) - is_relative_path = attributes.get(SPANDATA.CODE_FILE_PATH)[0] != os.sep + is_relative_path = ( + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep + ) assert is_relative_path - assert attributes.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm" + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "postgres_select_orm" + ) break else: raise AssertionError("No db span found") @@ -546,13 +563,15 @@ def test_db_span_origin_execute( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[1]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" for span in spans: - if span["attributes"]["sentry.op"] == "db": - assert span["attributes"]["sentry.origin"] == "auto.db.django" + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "db": + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" else: - assert span["attributes"]["sentry.origin"] == "auto.http.django" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" + ) @pytest.mark.forked @@ -593,5 +612,5 @@ def test_db_span_origin_executemany( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[1]["attributes"]["sentry.origin"] == "manual" - assert spans[0]["attributes"]["sentry.origin"] == "auto.db.django" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" diff --git a/tests/integrations/django/test_db_transactions.py b/tests/integrations/django/test_db_transactions.py index 17bed097eb..8bcacf3efa 100644 --- a/tests/integrations/django/test_db_transactions.py +++ b/tests/integrations/django/test_db_transactions.py @@ -10,10 +10,11 @@ except ImportError: from django.core.urlresolvers import reverse +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client import sentry_sdk -from sentry_sdk.consts import SPANDATA, SPANNAME +from sentry_sdk.consts import SPANNAME from sentry_sdk.integrations.django import DjangoIntegration from tests.integrations.django.myapp.wsgi import application from tests.integrations.django.utils import pytest_mark_django_db_decorator @@ -142,16 +143,24 @@ def test_db_transaction_spans_disabled_no_autocommit( # Ensure operation is persisted assert User.objects.using("postgres").exists() - assert postgres_rollback["attributes"]["sentry.origin"] == "auto.http.django" - assert postgres_commit["attributes"]["sentry.origin"] == "auto.http.django" - assert sqlite_rollback["attributes"]["sentry.origin"] == "manual" - assert sqlite_commit["attributes"]["sentry.origin"] == "manual" + assert ( + postgres_rollback["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.django" + ) + assert ( + postgres_commit["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.django" + ) + assert sqlite_rollback["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert sqlite_commit["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT - or span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT + or span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(commit_spans) == 0 @@ -269,8 +278,10 @@ def test_db_transaction_spans_disabled_atomic( commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT - or span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT + or span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(commit_spans) == 0 @@ -303,30 +314,31 @@ def test_db_no_autocommit_execute( # Ensure operation is persisted assert User.objects.using("postgres").exists() - assert spans[5]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT ] assert len(commit_spans) == 1 commit_span = commit_spans[0] - assert commit_span["attributes"]["sentry.origin"] == "auto.db.django" + assert commit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" # Verify other database attributes - assert commit_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" conn_params = connections["postgres"].get_connection_params() - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") - assert commit_span["attributes"].get(SPANDATA.SERVER_ADDRESS) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" - ) - assert commit_span["attributes"].get(SPANDATA.SERVER_PORT) == os.environ.get( + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_ADDRESS + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.SERVER_PORT) == os.environ.get( "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" ) @@ -396,27 +408,28 @@ def test_db_no_autocommit_executemany( # Ensure operation is persisted assert User.objects.exists() - assert spans[2]["attributes"]["sentry.origin"] == "manual" - assert spans[0]["attributes"]["sentry.origin"] == "auto.db.django" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT ] assert len(commit_spans) == 1 commit_span = commit_spans[0] - assert commit_span["attributes"]["sentry.origin"] == "auto.db.django" + assert commit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" # Verify other database attributes - assert commit_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "sqlite" + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "sqlite" conn_params = connection.get_connection_params() - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] @@ -453,32 +466,37 @@ def test_db_no_autocommit_rollback_execute( # Ensure operation is rolled back assert not User.objects.using("postgres").exists() - assert spans[5]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" - conn_params = connections["postgres"].get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") - assert rollback_span["attributes"].get(SPANDATA.SERVER_ADDRESS) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" - ) - assert rollback_span["attributes"].get(SPANDATA.SERVER_PORT) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" + assert ( + rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" ) + conn_params = connections["postgres"].get_connection_params() + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_ADDRESS + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_PORT + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] assert len(insert_spans) == 1 @@ -546,26 +564,29 @@ def test_db_no_autocommit_rollback_executemany( # Ensure operation is rolled back assert not User.objects.exists() - assert spans[2]["attributes"]["sentry.origin"] == "manual" - assert spans[0]["attributes"]["sentry.origin"] == "auto.db.django" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "sqlite" + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "sqlite" conn_params = connection.get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] @@ -602,30 +623,31 @@ def test_db_atomic_execute( # Ensure operation is persisted assert User.objects.using("postgres").exists() - assert spans[5]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT ] assert len(commit_spans) == 1 commit_span = commit_spans[0] - assert commit_span["attributes"]["sentry.origin"] == "auto.db.django" + assert commit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" # Verify other database attributes - assert commit_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" conn_params = connections["postgres"].get_connection_params() - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") - assert commit_span["attributes"].get(SPANDATA.SERVER_ADDRESS) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" - ) - assert commit_span["attributes"].get(SPANDATA.SERVER_PORT) == os.environ.get( + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_ADDRESS + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.SERVER_PORT) == os.environ.get( "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" ) @@ -692,25 +714,26 @@ def test_db_atomic_executemany( # Ensure operation is persisted assert User.objects.exists() - assert spans[3]["attributes"]["sentry.origin"] == "manual" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" commit_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_COMMIT + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_COMMIT ] assert len(commit_spans) == 1 commit_span = commit_spans[0] - assert commit_span["attributes"]["sentry.origin"] == "auto.db.django" + assert commit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" # Verify other database attributes - assert commit_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "sqlite" + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "sqlite" conn_params = connection.get_connection_params() - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert commit_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") + assert commit_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert commit_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] @@ -747,32 +770,37 @@ def test_db_atomic_rollback_execute( # Ensure operation is rolled back assert not User.objects.using("postgres").exists() - assert spans[5]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" - conn_params = connections["postgres"].get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") - assert rollback_span["attributes"].get(SPANDATA.SERVER_ADDRESS) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" - ) - assert rollback_span["attributes"].get(SPANDATA.SERVER_PORT) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" + assert ( + rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" ) + conn_params = connections["postgres"].get_connection_params() + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_ADDRESS + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_PORT + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] @@ -839,26 +867,29 @@ def test_db_atomic_rollback_executemany( # Ensure operation is rolled back assert not User.objects.exists() - assert spans[3]["attributes"]["sentry.origin"] == "manual" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "sqlite" + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "sqlite" conn_params = connection.get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] @@ -895,32 +926,37 @@ def test_db_atomic_execute_exception( # Ensure operation is rolled back assert not User.objects.using("postgres").exists() - assert spans[5]["attributes"]["sentry.origin"] == "auto.http.django" + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.django" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "postgresql" - conn_params = connections["postgres"].get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") - assert rollback_span["attributes"].get(SPANDATA.SERVER_ADDRESS) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost" - ) - assert rollback_span["attributes"].get(SPANDATA.SERVER_PORT) == os.environ.get( - "SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432" + assert ( + rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "postgresql" ) + conn_params = connections["postgres"].get_connection_params() + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_ADDRESS + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_HOST", "localhost") + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.SERVER_PORT + ) == os.environ.get("SENTRY_PYTHON_TEST_POSTGRES_PORT", "5432") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] assert len(insert_spans) == 1 @@ -989,25 +1025,28 @@ def test_db_atomic_executemany_exception( # Ensure operation is rolled back assert not User.objects.exists() - assert spans[3]["attributes"]["sentry.origin"] == "manual" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" rollback_spans = [ span for span in spans - if span["attributes"].get(SPANDATA.DB_OPERATION_NAME) == SPANNAME.DB_ROLLBACK + if span["attributes"].get(ATTRIBUTE_NAMES.DB_OPERATION_NAME) + == SPANNAME.DB_ROLLBACK ] assert len(rollback_spans) == 1 rollback_span = rollback_spans[0] - assert rollback_span["attributes"]["sentry.origin"] == "auto.db.django" + assert ( + rollback_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.django" + ) # Verify other database attributes - assert rollback_span["attributes"].get(SPANDATA.DB_SYSTEM_NAME) == "sqlite" + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "sqlite" conn_params = connection.get_connection_params() - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) is not None - assert rollback_span["attributes"].get(SPANDATA.DB_NAMESPACE) == conn_params.get( - "database" - ) or conn_params.get("dbname") + assert rollback_span["attributes"].get(ATTRIBUTE_NAMES.DB_NAMESPACE) is not None + assert rollback_span["attributes"].get( + ATTRIBUTE_NAMES.DB_NAMESPACE + ) == conn_params.get("database") or conn_params.get("dbname") insert_spans = [span for span in spans if span["name"].startswith("INSERT INTO")] diff --git a/tests/integrations/django/test_tasks.py b/tests/integrations/django/test_tasks.py index b1b14abc36..effff751e5 100644 --- a/tests/integrations/django/test_tasks.py +++ b/tests/integrations/django/test_tasks.py @@ -1,6 +1,7 @@ import logging import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -73,14 +74,17 @@ def test_task_span_is_created( queue_submit_spans = [ span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PUBLISH + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ] assert len(queue_submit_spans) == 1 assert ( queue_submit_spans[0]["name"] == "tests.integrations.django.test_tasks.simple_task" ) - assert queue_submit_spans[0]["attributes"]["sentry.origin"] == "auto.http.django" + assert ( + queue_submit_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.django" + ) def test_task_enqueue_returns_result(sentry_init, immediate_backend): @@ -119,7 +123,7 @@ def test_task_enqueue_with_kwargs( queue_submit_spans = [ span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PUBLISH + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ] assert len(queue_submit_spans) == 1 assert queue_submit_spans[0]["name"] == "tests.integrations.django.test_tasks.greet" @@ -152,7 +156,7 @@ def test_task_error_reporting( queue_submit_spans = [ span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PUBLISH + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ] assert len(queue_submit_spans) == 1 @@ -185,7 +189,7 @@ def test_multiple_task_enqueues_create_multiple_spans( queue_submit_spans = [ span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PUBLISH + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ] assert len(queue_submit_spans) == 3 diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py index 176abe26c2..4830a6b7c4 100644 --- a/tests/integrations/falcon/test_falcon.py +++ b/tests/integrations/falcon/test_falcon.py @@ -4,9 +4,9 @@ import falcon import falcon.testing import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.falcon import FalconIntegration from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.utils import parse_version @@ -122,7 +122,10 @@ def test_segment_name_and_source( spans = [item.payload for item in items if item.type == "span"] spans = [span for span in spans if span["name"] == expected_transaction] assert len(spans) == 1 - assert spans[0]["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) assert event["transaction"] == expected_transaction assert event["transaction_info"] == {"source": expected_source} @@ -145,7 +148,9 @@ def test_http_route( sentry_sdk.flush() (segment,) = [item.payload for item in items if item.payload.get("is_segment")] - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/{message_id:int}" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message/{message_id:int}" + ) def test_unhandled_errors( @@ -709,7 +714,7 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.origin"] == "auto.http.falcon" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.falcon" def test_falcon_request_media(sentry_init): diff --git a/tests/integrations/fastapi/test_fastapi.py b/tests/integrations/fastapi/test_fastapi.py index d68b9cbf28..5d4027a5cf 100644 --- a/tests/integrations/fastapi/test_fastapi.py +++ b/tests/integrations/fastapi/test_fastapi.py @@ -20,10 +20,10 @@ ) from fastapi.middleware.trustedhost import TrustedHostMiddleware from fastapi.testclient import TestClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import SPANDATA from sentry_sdk.feature_flags import add_feature_flag from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.integrations.fastapi import FastApiIntegration @@ -156,10 +156,14 @@ async def test_request_info_json_body(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) - assert json.loads(server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA]) == { + assert json.loads( + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] + ) == { "some": "json", "for": "testing", "nested": {"numbers": 123}, @@ -197,13 +201,15 @@ async def test_formdata_request_body(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) # Going forward, the sanitization of data will need to happen within the `before_send_span` hooks # See https://sentry.slack.com/archives/C09RR0KD2N7/p1776951331206129?thread_ts=1776951227.440659&cid=C09RR0KD2N7 parsed_form_attribute = json.loads( - server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA] + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] ) assert parsed_form_attribute.keys() == PARSED_FORM.keys() assert parsed_form_attribute["username"] == PARSED_FORM["username"] @@ -243,12 +249,14 @@ async def test_request_body_too_big(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) # Because request is too big only the AnnotatedValue is extracted. assert ( - server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA] + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] == "[Exceeds maximum size]" ) @@ -279,9 +287,11 @@ async def test_formdata_request_body_data_collection_http_bodies_empty( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) - assert SPANDATA.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] @pytest.mark.asyncio @@ -323,18 +333,22 @@ async def test_request_body_data_collection( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) if expect_body: assert event["request"]["data"] == BODY_JSON assert ( - json.loads(server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA]) + json.loads( + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] + ) == BODY_JSON ) else: assert "data" not in event["request"] - assert SPANDATA.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] @pytest.mark.asyncio @@ -449,7 +463,7 @@ def test_active_thread_id(sentry_init, capture_items, endpoint): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert str(data["active"]) == segments[0]["attributes"]["thread.id"] + assert str(data["active"]) == segments[0]["attributes"][ATTRIBUTE_NAMES.THREAD_ID] @pytest.mark.asyncio @@ -523,7 +537,7 @@ def test_transaction_name( assert len(segments) == 1 segment = segments[0] assert segment["name"] == "/message/{message_id}" - assert segment["attributes"]["sentry.segment.name.source"] == "route" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" def test_http_route_with_prefix( @@ -558,8 +572,8 @@ async def get_user(user_id: int): assert len(segments) == 1 segment = segments[0] assert segment["name"] == "/api/users/{user_id}" - assert segment["attributes"]["sentry.segment.name.source"] == "route" - assert segment["attributes"]["http.route"] == "/api/users/{user_id}" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/api/users/{user_id}" def test_route_endpoint_equal_dependant_call(sentry_init): @@ -648,7 +662,7 @@ def test_transaction_name_in_middleware( assert len(segments) == 1 segment = segments[0] assert segment["name"] == "http://testserver/message/123456" - assert segment["attributes"]["sentry.segment.name.source"] == "url" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "url" @pytest.mark.skipif( @@ -680,7 +694,7 @@ def test_transaction_http_method_default(sentry_init, capture_items): sentry_sdk.flush() segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert segments[0]["attributes"]["http.request.method"] == "GET" + assert segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" @pytest.mark.skipif( @@ -720,8 +734,8 @@ def test_transaction_http_method_custom(sentry_init, capture_items): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 2 - assert segments[0]["attributes"]["http.request.method"] == "OPTIONS" - assert segments[1]["attributes"]["http.request.method"] == "HEAD" + assert segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "OPTIONS" + assert segments[1]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "HEAD" def test_request_url(sentry_init, capture_items): @@ -743,12 +757,14 @@ def test_request_url(sentry_init, capture_items): spans = [item.payload for item in items] (server_span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == "http.server" + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "http.server" ) - assert server_span["attributes"][SPANDATA.URL_FULL] == ( + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == ( "http://testserver/root/nomessage" ) - assert server_span["attributes"][SPANDATA.URL_PATH] == "/root/nomessage" + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/root/nomessage" @parametrize_test_configurable_status_codes diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index edd6456da5..737c4b332f 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -14,6 +14,7 @@ ) from flask.views import View from flask_login import LoginManager, login_user +from sentry_conventions.attributes import ATTRIBUTE_NAMES from sentry_sdk.traces import SpanStatus @@ -29,7 +30,6 @@ capture_message, set_tag, ) -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.serializer import MAX_DATABAG_BREADTH from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES @@ -129,7 +129,10 @@ def test_segment_name_and_source( assert len(spans) == 1 (segment,) = spans assert segment["name"] == expected_transaction - assert segment["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) def test_http_route( @@ -149,7 +152,9 @@ def test_http_route( sentry_sdk.flush() (segment,) = (item.payload for item in items if item.payload.get("is_segment")) - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message/" + ) @pytest.mark.parametrize("debug", (True, False)) @@ -294,11 +299,11 @@ def login(): segment = next(s for s in spans if s["name"] == "/message") if expect_user and user_id is not None: - assert segment["attributes"]["user.id"] == str(user_id) - assert segment["attributes"]["user.email"] == "user@example.com" - assert segment["attributes"]["user.name"] == "testuser" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_ID] == str(user_id) + assert segment["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "user@example.com" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "testuser" else: - assert "user.id" not in segment.get("attributes", {}) + assert ATTRIBUTE_NAMES.USER_ID not in segment.get("attributes", {}) @pytest.mark.parametrize("max_value_length", [1024, None]) @@ -827,7 +832,7 @@ def hi_tx(): assert segment["name"] == "/message_tx" assert segment["status"] == SpanStatus.OK - assert segment["attributes"]["sentry.origin"] == "auto.http.flask" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.flask" assert message_event["message"] == "hi" assert message_event["transaction"] == "/message_tx" @@ -1013,7 +1018,7 @@ def test_span_origin(sentry_init, app, capture_items): spans = [i.payload for i in items] assert len(spans) == 1 (segment,) = spans - assert segment["attributes"]["sentry.origin"] == "auto.http.flask" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.flask" def test_segment_http_method_default( @@ -1046,7 +1051,7 @@ def test_segment_http_method_default( spans = [i.payload for i in items] assert len(spans) == 1 (segment,) = spans - assert segment["attributes"]["http.request.method"] == "GET" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" def test_segment_http_method_custom( @@ -1082,7 +1087,9 @@ def test_segment_http_method_custom( spans = [i.payload for i in items] (options_segment,) = spans - assert options_segment["attributes"]["http.request.method"] == "OPTIONS" + assert ( + options_segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "OPTIONS" + ) response = client.head("/nomessage") assert response.status_code == 200 @@ -1092,7 +1099,7 @@ def test_segment_http_method_custom( assert len(spans) == 2 (_, head_segment) = spans - assert head_segment["attributes"]["http.request.method"] == "HEAD" + assert head_segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "HEAD" @pytest.mark.parametrize( @@ -1187,9 +1194,9 @@ def test_span_http_query_data_collection( (segment,) = spans if expected_query is None: - assert SPANDATA.HTTP_QUERY not in segment["attributes"] + assert ATTRIBUTE_NAMES.HTTP_QUERY not in segment["attributes"] else: - assert segment["attributes"][SPANDATA.HTTP_QUERY] == expected_query + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query def test_empty_query_string_is_dropped_with_data_collection( @@ -1231,11 +1238,11 @@ def test_user_info_span_attributes_data_collection( (segment,) = spans if expect_ip: - assert segment["attributes"][SPANDATA.USER_IP_ADDRESS] == "127.0.0.1" - assert segment["attributes"]["client.address"] == "127.0.0.1" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert segment["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert SPANDATA.USER_IP_ADDRESS not in segment["attributes"] - assert "client.address" not in segment["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in segment["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in segment["attributes"] @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -1385,13 +1392,13 @@ def login(): segment = next(s for s in spans if s["name"] == "/message") if expect_user: - assert segment["attributes"]["user.id"] == "42" - assert segment["attributes"]["user.email"] == "user@example.com" - assert segment["attributes"]["user.name"] == "testuser" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_ID] == "42" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "user@example.com" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "testuser" else: - assert "user.id" not in segment.get("attributes", {}) - assert "user.email" not in segment.get("attributes", {}) - assert "user.name" not in segment.get("attributes", {}) + assert ATTRIBUTE_NAMES.USER_ID not in segment.get("attributes", {}) + assert ATTRIBUTE_NAMES.USER_EMAIL not in segment.get("attributes", {}) + assert ATTRIBUTE_NAMES.USER_NAME not in segment.get("attributes", {}) @pytest.mark.parametrize( diff --git a/tests/integrations/graphene/test_graphene.py b/tests/integrations/graphene/test_graphene.py index 1fca8f13d0..e43041ba93 100644 --- a/tests/integrations/graphene/test_graphene.py +++ b/tests/integrations/graphene/test_graphene.py @@ -3,6 +3,7 @@ from fastapi.testclient import TestClient from flask import Flask, jsonify, request from graphene import ObjectType, Schema, String +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -211,14 +212,17 @@ def graphql_server_sync(): graphql_span, flask_segment = spans assert graphql_span["name"] == query["operationName"] - assert graphql_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert graphql_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert ( - graphql_span["attributes"]["graphql.operation.name"] == query["operationName"] + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] + == query["operationName"] ) - assert graphql_span["attributes"]["graphql.operation.type"] == "query" + assert graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" assert graphql_span["is_segment"] is False - assert graphql_span["attributes"]["graphql.document"] == query["query"] + assert ( + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query["query"] + ) assert flask_segment["is_segment"] is True assert graphql_span["parent_span_id"] == flask_segment["span_id"] @@ -265,17 +269,21 @@ def graphql_server_sync(): graphql_span, flask_segment = spans assert graphql_span["name"] == query["operationName"] - assert graphql_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert graphql_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert ( - graphql_span["attributes"]["graphql.operation.name"] == query["operationName"] + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] + == query["operationName"] ) - assert graphql_span["attributes"]["graphql.operation.type"] == "query" + assert graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" assert graphql_span["is_segment"] is False if expect_document: - assert graphql_span["attributes"]["graphql.document"] == query["query"] + assert ( + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] + == query["query"] + ) else: - assert "graphql.document" not in graphql_span["attributes"] + assert ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT not in graphql_span["attributes"] assert flask_segment["is_segment"] is True assert graphql_span["parent_span_id"] == flask_segment["span_id"] @@ -328,17 +336,21 @@ async def graphql_server_async(request: Request): graphql_span, segment = spans assert graphql_span["name"] == query["operationName"] - assert graphql_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert graphql_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert ( - graphql_span["attributes"]["graphql.operation.name"] == query["operationName"] + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] + == query["operationName"] ) - assert graphql_span["attributes"]["graphql.operation.type"] == "query" + assert graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" assert graphql_span["is_segment"] is False if expect_document: - assert graphql_span["attributes"]["graphql.document"] == query["query"] + assert ( + graphql_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] + == query["query"] + ) else: - assert "graphql.document" not in graphql_span["attributes"] + assert ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT not in graphql_span["attributes"] assert segment["is_segment"] is True assert graphql_span["parent_span_id"] == segment["span_id"] diff --git a/tests/integrations/litestar/test_litestar.py b/tests/integrations/litestar/test_litestar.py index e561136240..8ef3703bea 100644 --- a/tests/integrations/litestar/test_litestar.py +++ b/tests/integrations/litestar/test_litestar.py @@ -12,6 +12,7 @@ from litestar.middleware.rate_limit import RateLimitConfig from litestar.middleware.session.server_side import ServerSideSessionConfig from litestar.testing import TestClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -170,7 +171,10 @@ def test_segment_name_and_source( spans = [span for span in spans if expected_tx_name in span["name"]] assert len(spans) == 1 - assert spans[0]["attributes"]["sentry.segment.name.source"] == "component" + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) def test_middleware_spans( @@ -209,13 +213,14 @@ def test_middleware_spans( litestar_spans = ( span for span in spans - if span["attributes"]["sentry.op"] == OP.MIDDLEWARE and span["name"] in expected + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MIDDLEWARE + and span["name"] in expected ) for span in litestar_spans: assert span["name"] not in found found.add(span["name"]) - assert span["name"] == span["attributes"]["middleware.name"] + assert span["name"] == span["attributes"][ATTRIBUTE_NAMES.MIDDLEWARE_NAME] def test_middleware_callback_spans( @@ -250,8 +255,8 @@ async def do_stuff(message): "name": "SampleMiddleware", "attributes": ApproxDict( { - "middleware.name": "SampleMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SampleMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, }, ), }, @@ -259,8 +264,8 @@ async def do_stuff(message): "name": "SentryAsgiMiddleware._run_app.._sentry_wrapped_send", "attributes": ApproxDict( { - "middleware.name": "SampleMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SampleMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, } ), }, @@ -268,8 +273,8 @@ async def do_stuff(message): "name": "SentryAsgiMiddleware._run_app.._sentry_wrapped_send", "attributes": ApproxDict( { - "middleware.name": "SampleMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SampleMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, } ), }, @@ -284,7 +289,9 @@ def is_matching_span(expected_span, actual_span): sentry_sdk.flush() spans = [item.payload for item in items] actual_litestar_spans = list( - span for span in spans if OP.MIDDLEWARE in span["attributes"].get("sentry.op") + span + for span in spans + if OP.MIDDLEWARE in span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) ) assert len(actual_litestar_spans) == 3 @@ -365,8 +372,8 @@ async def my_send(*args, **kwargs): "name": "SamplePartialReceiveSendMiddleware", "attributes": ApproxDict( { - "middleware.name": "SamplePartialReceiveSendMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SamplePartialReceiveSendMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, } ), }, @@ -374,8 +381,8 @@ async def my_send(*args, **kwargs): "name": "TestClientTransport.create_receive..receive", "attributes": ApproxDict( { - "middleware.name": "SamplePartialReceiveSendMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SamplePartialReceiveSendMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, } ), }, @@ -383,8 +390,8 @@ async def my_send(*args, **kwargs): "name": "SentryAsgiMiddleware._run_app.._sentry_wrapped_send", "attributes": ApproxDict( { - "middleware.name": "SamplePartialReceiveSendMiddleware", - "sentry.op": OP.MIDDLEWARE, + ATTRIBUTE_NAMES.MIDDLEWARE_NAME: "SamplePartialReceiveSendMiddleware", + ATTRIBUTE_NAMES.SENTRY_OP: OP.MIDDLEWARE, } ), }, @@ -397,7 +404,9 @@ def is_matching_span(expected_span, actual_span): ) actual_litestar_spans = list( - span for span in spans if OP.MIDDLEWARE in span["attributes"].get("sentry.op") + span + for span in spans + if OP.MIDDLEWARE in span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) ) assert len(actual_litestar_spans) == 3 @@ -439,9 +448,9 @@ def test_span_origin( spans = [item.payload for item in items] for span in spans: - if span["attributes"]["sentry.origin"] == "auto.http.httpx": + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.httpx": continue - assert span["attributes"]["sentry.origin"] == "auto.http.litestar" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.litestar" @pytest.mark.parametrize( diff --git a/tests/integrations/pyramid/test_pyramid.py b/tests/integrations/pyramid/test_pyramid.py index 86113ff2de..94331a8c58 100644 --- a/tests/integrations/pyramid/test_pyramid.py +++ b/tests/integrations/pyramid/test_pyramid.py @@ -6,11 +6,11 @@ import pytest from pyramid.authorization import ACLAuthorizationPolicy from pyramid.response import Response +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client import sentry_sdk from sentry_sdk import add_breadcrumb, capture_message -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.pyramid import PyramidIntegration from sentry_sdk.serializer import MAX_DATABAG_BREADTH from sentry_sdk.traces import SpanStatus @@ -146,7 +146,10 @@ def test_segment_name_and_source( assert len(spans) == 1 (segment,) = spans assert segment["name"] == expected_transaction - assert segment["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) def test_http_route( @@ -167,7 +170,7 @@ def test_http_route( sentry_sdk.flush() (segment,) = [item.payload for item in items if item.payload.get("is_segment")] - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/{message_id}" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message/{message_id}" @pytest.mark.parametrize("max_value_length", [1024, None]) @@ -499,7 +502,7 @@ def tracing_error(request): assert segment["name"] == "/tracing-error" assert segment["status"] == SpanStatus.ERROR - assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyramid" assert error_event["exception"]["values"][-1]["type"] == "ZeroDivisionError" assert error_event["exception"]["values"][-1]["mechanism"]["type"] == "pyramid" @@ -526,7 +529,7 @@ def test_span_origin( spans = [i.payload for i in items if i.type == "span"] assert len(spans) == 1 (segment,) = spans - assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyramid" @pytest.mark.parametrize( @@ -565,9 +568,9 @@ def authenticated_userid(self, request): (segment,) = spans if expect_user: - assert segment["attributes"]["user.id"] == "123-abc" + assert segment["attributes"][ATTRIBUTE_NAMES.USER_ID] == "123-abc" else: - assert "user.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.USER_ID not in segment["attributes"] @pytest.mark.parametrize( diff --git a/tests/integrations/quart/test_quart.py b/tests/integrations/quart/test_quart.py index cb8856a205..c31c913ff8 100644 --- a/tests/integrations/quart/test_quart.py +++ b/tests/integrations/quart/test_quart.py @@ -4,6 +4,7 @@ import threading import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk import sentry_sdk.integrations.quart as quart_sentry @@ -12,7 +13,6 @@ capture_message, set_tag, ) -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.utils import parse_version from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES @@ -168,7 +168,7 @@ async def test_http_route( sentry_sdk.flush() (segment,) = [item.payload for item in items if item.payload.get("is_segment")] - assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == "/message/" @pytest.mark.asyncio @@ -626,7 +626,7 @@ async def test_span_origin(sentry_init, capture_items): (span,) = [item.payload for item in items] - assert span["attributes"]["sentry.origin"] == "auto.http.quart" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.quart" @pytest.mark.asyncio @@ -651,9 +651,9 @@ async def test_basic(sentry_init, capture_items): assert segment["is_segment"] is True assert "parent_span_id" not in segment assert segment["status"] == "ok" - assert segment["attributes"]["sentry.op"] == "http.server" - assert segment["attributes"]["sentry.origin"] == "auto.http.quart" - assert segment["attributes"]["http.request.method"] == "GET" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.quart" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" assert segment["name"] == "/message" @@ -691,7 +691,10 @@ async def test_segment_name_and_source( segment = spans[0] assert segment["is_segment"] is True assert segment["name"] == expected_name - assert segment["attributes"]["sentry.segment.name.source"] == expected_source + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == expected_source + ) @pytest.mark.asyncio @@ -759,21 +762,22 @@ async def test_request_attributes( assert len(spans) == 1 segment = spans[0] - assert segment["attributes"]["http.request.method"] == "GET" + assert segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" assert "http.request.header.host" in segment["attributes"] assert ( - segment["attributes"]["url.full"] == "http://localhost/message?foo=bar&baz=qux" + segment["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == "http://localhost/message?foo=bar&baz=qux" ) - assert segment["attributes"]["url.path"] == "/message" - assert segment["attributes"]["url.query"] == "foo=bar&baz=qux" + assert segment["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/message" + assert segment["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "foo=bar&baz=qux" if expect_user_info: - assert "client.address" in segment["attributes"] - assert "user.ip_address" in segment["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS in segment["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS in segment["attributes"] else: - assert "client.address" not in segment["attributes"] - assert "user.ip_address" not in segment["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in segment["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in segment["attributes"] @pytest.mark.parametrize( @@ -926,9 +930,9 @@ async def login(): segment = next(s for s in spans if s["name"] == "/message") if expect_user_info and user_id is not None: - assert segment["attributes"]["user.id"] == user_id + assert segment["attributes"][ATTRIBUTE_NAMES.USER_ID] == user_id else: - assert "user.id" not in segment.get("attributes", {}) + assert ATTRIBUTE_NAMES.USER_ID not in segment.get("attributes", {}) _QUERY_PARAM_DATA_COLLECTION_CASES = [ @@ -985,13 +989,16 @@ async def test_url_query(sentry_init, capture_items, data_collection, expected_q segment = spans[0] if expected_query is None: - assert "url.query" not in segment["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in segment["attributes"] # When the filtered query string is empty, url.full carries the # base URL only (no query). - assert segment["attributes"]["url.full"] == "http://localhost/message" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == "http://localhost/message" + ) else: - assert segment["attributes"]["url.query"] == expected_query - assert segment["attributes"]["url.full"] == ( + assert segment["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query + assert segment["attributes"][ATTRIBUTE_NAMES.URL_FULL] == ( f"http://localhost/message?{expected_query}" ) @@ -1015,10 +1022,13 @@ async def test_url_query_multi_and_blank_values(sentry_init, capture_items): assert len(spans) == 1 segment = spans[0] - query = segment["attributes"]["url.query"] + query = segment["attributes"][ATTRIBUTE_NAMES.URL_QUERY] # Repeated keys are preserved and blank values are kept. assert "foo=1" in query assert "foo=2" in query assert "empty=" in query # url.full carries the same filtered query string. - assert segment["attributes"]["url.full"] == f"http://localhost/message?{query}" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == f"http://localhost/message?{query}" + ) diff --git a/tests/integrations/sanic/test_sanic.py b/tests/integrations/sanic/test_sanic.py index e1acb61af7..b292353aac 100644 --- a/tests/integrations/sanic/test_sanic.py +++ b/tests/integrations/sanic/test_sanic.py @@ -10,6 +10,7 @@ from sanic import __version__ as SANIC_VERSION_RAW from sanic.exceptions import SanicException from sanic.response import HTTPResponse +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -440,7 +441,8 @@ def test_transactions( segments = [ i.payload for i in items - if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic" + if i.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.sanic" and i.payload["is_segment"] ] assert len(segments) <= 1 @@ -451,20 +453,23 @@ def test_transactions( if segment is not None: assert segment["name"] == test_config.expected_transaction_name assert ( - segment["attributes"]["sentry.segment.name.source"] + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == test_config.expected_source ) attrs = segment["attributes"] - assert attrs["http.request.method"] == "GET" - assert attrs["network.protocol.name"] == "http" + assert attrs[ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert attrs[ATTRIBUTE_NAMES.NETWORK_PROTOCOL_NAME] == "http" header_keys = { key[len("http.request.header.") :] for key in attrs if key.startswith("http.request.header.") } assert header_keys >= {"accept", "accept-encoding", "host", "user-agent"} - assert attrs["http.response.status_code"] == test_config.expected_status + assert ( + attrs[ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] + == test_config.expected_status + ) assert segment["status"] == ( "error" if test_config.expected_status >= 400 else "ok" ) @@ -472,13 +477,16 @@ def test_transactions( expected_url = ( test_config.url if expect_query else test_config.url.split("?", 1)[0] ) - assert attrs["url.full"].endswith(expected_url) - assert attrs["url.path"] == test_config.url.split("?")[0] + assert attrs[ATTRIBUTE_NAMES.URL_FULL].endswith(expected_url) + assert attrs[ATTRIBUTE_NAMES.URL_PATH] == test_config.url.split("?")[0] if "?" in test_config.url: if expect_query: - assert attrs["http.query"] == test_config.url.split("?", 1)[1] + assert ( + attrs[ATTRIBUTE_NAMES.HTTP_QUERY] + == test_config.url.split("?", 1)[1] + ) else: - assert "http.query" not in attrs + assert ATTRIBUTE_NAMES.HTTP_QUERY not in attrs def test_span_origin(sentry_init, app, capture_items): @@ -498,9 +506,10 @@ def test_span_origin(sentry_init, app, capture_items): (segment,) = [ i.payload for i in items - if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic" + if i.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.sanic" ] - assert segment["attributes"]["sentry.origin"] == "auto.http.sanic" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.sanic" @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -536,11 +545,11 @@ def child_span_handler(request): child_span, server_span = [item.payload for item in items] if expect_ip: - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert child_span["attributes"]["user.ip_address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert child_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" else: - assert "user.ip_address" not in server_span["attributes"] - assert "user.ip_address" not in child_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in child_span["attributes"] @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -570,13 +579,14 @@ def test_client_address_span_attribute_data_collection( (server_span,) = [ item.payload for item in items - if item.payload["attributes"].get("sentry.origin") == "auto.http.sanic" + if item.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.sanic" ] if expect_ip: - assert server_span["attributes"]["client.address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert "client.address" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in server_span["attributes"] _QUERY_PARAM_DATA_COLLECTION_CASES = [ @@ -632,20 +642,21 @@ def test_url_query_data_collection( (server_span,) = [ i.payload for i in items - if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic" + if i.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.sanic" and i.payload["is_segment"] ] if expected_query is None: - assert "http.query" not in server_span["attributes"] - assert server_span["attributes"]["url.full"].endswith("/message") - assert server_span["attributes"]["url.path"].endswith("/message") + assert ATTRIBUTE_NAMES.HTTP_QUERY not in server_span["attributes"] + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith("/message") + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH].endswith("/message") else: - assert server_span["attributes"]["http.query"] == expected_query - assert server_span["attributes"]["url.full"].endswith( + assert server_span["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith( f"/message?{expected_query}" ) - assert server_span["attributes"]["url.path"].endswith("/message") + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH].endswith("/message") @pytest.mark.parametrize( @@ -820,8 +831,10 @@ def child_span_handler_localhost(request): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["sentry.is_localhost"] is is_localhost - assert child_span["attributes"]["sentry.is_localhost"] is is_localhost + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost + ) + assert child_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost def test_user_agent_attribute(sentry_init, app, capture_items): @@ -850,5 +863,11 @@ def child_span_handler_ua(request): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" - assert child_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + ) + assert ( + child_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + ) diff --git a/tests/integrations/starlette/test_starlette.py b/tests/integrations/starlette/test_starlette.py index f74b5ab447..cdebe662b9 100644 --- a/tests/integrations/starlette/test_starlette.py +++ b/tests/integrations/starlette/test_starlette.py @@ -10,6 +10,7 @@ import pytest import starlette +from sentry_conventions.attributes import ATTRIBUTE_NAMES from starlette.authentication import ( AuthCredentials, AuthenticationBackend, @@ -25,7 +26,7 @@ import sentry_sdk from sentry_sdk import capture_message, get_baggage, get_traceparent from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.integrations.logging import LoggingIntegration from sentry_sdk.integrations.starlette import ( @@ -318,10 +319,14 @@ async def test_request_info_json_body(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) - assert json.loads(server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA]) == { + assert json.loads( + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] + ) == { "some": "json", "for": "testing", "nested": {"numbers": 123}, @@ -358,13 +363,15 @@ async def test_formdata_request_body(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) # Going forward, the sanitization of data will need to happen within the `before_send_span` hooks # See https://sentry.slack.com/archives/C09RR0KD2N7/p1776951331206129?thread_ts=1776951227.440659&cid=C09RR0KD2N7 parsed_form_attribute = json.loads( - server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA] + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] ) assert parsed_form_attribute.keys() == PARSED_FORM.keys() assert parsed_form_attribute["username"] == PARSED_FORM["username"] @@ -404,12 +411,14 @@ async def test_request_body_too_big(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) # Because request is too big only the AnnotatedValue is extracted. assert ( - server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA] + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] == "[Exceeds maximum size]" ) @@ -440,9 +449,11 @@ async def test_formdata_request_body_data_collection_http_bodies_empty( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) - assert SPANDATA.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] @pytest.mark.asyncio @@ -484,18 +495,22 @@ async def test_request_body_data_collection( sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) if expect_body: assert event["request"]["data"] == BODY_JSON assert ( - json.loads(server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA]) + json.loads( + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] + ) == BODY_JSON ) else: assert "data" not in event["request"] - assert SPANDATA.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] @pytest.mark.asyncio @@ -526,10 +541,12 @@ async def test_request_info_cookies_off(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items if item.type == "span"] server_span = next( - span for span in spans if span["attributes"]["sentry.op"] == "http.server" + span + for span in spans + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" ) assert ( - json.loads(server_span["attributes"][SPANDATA.HTTP_REQUEST_BODY_DATA]) + json.loads(server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA]) == BODY_JSON ) @@ -677,10 +694,13 @@ def test_query_string_data_collection( if expected_query_string is None: assert "query_string" not in event["request"] - assert SPANDATA.HTTP_QUERY not in segments[0]["attributes"] + assert ATTRIBUTE_NAMES.HTTP_QUERY not in segments[0]["attributes"] else: assert event["request"]["query_string"] == expected_query_string - assert segments[0]["attributes"][SPANDATA.HTTP_QUERY] == expected_query_string + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] + == expected_query_string + ) @pytest.mark.parametrize( @@ -722,18 +742,18 @@ def test_span_http_query_data_collection( item.payload for item in items if item.payload.get("is_segment") - and item.payload["attributes"].get("sentry.op") == "http.server" + and item.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "http.server" ] (segment,) = segments attributes = segment["attributes"] if expected_query is None: - assert SPANDATA.HTTP_QUERY not in attributes + assert ATTRIBUTE_NAMES.HTTP_QUERY not in attributes else: - assert attributes[SPANDATA.HTTP_QUERY] == expected_query + assert attributes[ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query - assert attributes["url.full"] == expected_url_full - assert attributes["url.path"] == "/message" + assert attributes[ATTRIBUTE_NAMES.URL_FULL] == expected_url_full + assert attributes[ATTRIBUTE_NAMES.URL_PATH] == "/message" # TestClient provides ("testclient", 50000) as the scope's client. @@ -782,9 +802,9 @@ def test_user_info_data_collection( attributes = items[0].payload["attributes"] if expected_ip is NO_USER_INFO: - assert SPANDATA.CLIENT_ADDRESS not in attributes + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in attributes else: - assert attributes[SPANDATA.CLIENT_ADDRESS] == expected_ip + assert attributes[ATTRIBUTE_NAMES.CLIENT_ADDRESS] == expected_ip @pytest.mark.parametrize( @@ -857,10 +877,16 @@ async def hosted_endpoint(request): # Starlette starting setting scope["route"] with https://github.com/Kludex/starlette/commit/9c594b56e8da9c3d8c35baa40f357e7bc94f6d02 if STARLETTE_VERSION >= (1, 7): assert segments[0]["name"].endswith("/users/{user_id}") - assert segments[0]["attributes"]["sentry.segment.name.source"] == "route" + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "route" + ) else: assert segments[0]["name"].endswith("/users/123456") - assert segments[0]["attributes"]["sentry.segment.name.source"] == "url" + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "url" + ) @pytest.mark.parametrize( @@ -1052,11 +1078,14 @@ def test_middleware_spans(sentry_init, capture_items): assert len(middleware_spans) == len(expected_middleware_spans) assert segment["is_segment"] is True - assert segment["attributes"]["sentry.op"] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" idx = 0 for idx, span in enumerate(middleware_spans): - assert span["attributes"]["middleware.name"] == expected_middleware_spans[idx] + assert ( + span["attributes"][ATTRIBUTE_NAMES.MIDDLEWARE_NAME] + == expected_middleware_spans[idx] + ) def test_middleware_spans_disabled(sentry_init, capture_items): @@ -1085,7 +1114,7 @@ def test_middleware_spans_disabled(sentry_init, capture_items): assert len(middleware_spans) == 0 assert segment["is_segment"] is True - assert segment["attributes"]["sentry.op"] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" def test_middleware_callback_spans(sentry_init, capture_items): @@ -1164,12 +1193,15 @@ def test_middleware_callback_spans(sentry_init, capture_items): assert len(middleware_spans) == len(expected) assert segment["is_segment"] is True - assert segment["attributes"]["sentry.op"] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" for span, exp in zip(middleware_spans, expected): - assert span["attributes"]["sentry.op"] == exp["op"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == exp["op"] assert span["name"] == exp["description"] - assert span["attributes"]["middleware.name"] == exp["middleware_name"] + assert ( + span["attributes"][ATTRIBUTE_NAMES.MIDDLEWARE_NAME] + == exp["middleware_name"] + ) def test_middleware_receive_send(sentry_init): @@ -1251,10 +1283,10 @@ def test_middleware_partial_receive_send(sentry_init, capture_items): ] assert segment["is_segment"] is True - assert segment["attributes"]["sentry.op"] == "http.server" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" for span, exp in zip(middleware_spans, expected): - assert span["attributes"]["sentry.op"] == exp["op"] + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == exp["op"] assert span["name"].startswith(exp["description"]) @@ -1315,7 +1347,7 @@ def test_active_thread_id(sentry_init, capture_items, endpoint): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert str(data["active"]) == segments[0]["attributes"]["thread.id"] + assert str(data["active"]) == segments[0]["attributes"][ATTRIBUTE_NAMES.THREAD_ID] @pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"]) @@ -1338,8 +1370,10 @@ def test_http_route(sentry_init, capture_items, endpoint): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 assert segments[0]["name"] == endpoint - assert segments[0]["attributes"]["sentry.segment.name.source"] == "route" - assert segments[0]["attributes"]["http.route"] == endpoint + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + ) + assert segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_ROUTE] == endpoint @pytest.mark.parametrize("endpoint", ["/sync/thread_ids", "/async/thread_ids"]) @@ -1362,7 +1396,9 @@ def test_segment_name_is_route_resolved_name_static( segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 assert segments[0]["name"] == endpoint - assert segments[0]["attributes"]["sentry.segment.name.source"] == "route" + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + ) def test_original_request_not_scrubbed(sentry_init, capture_events): @@ -1460,7 +1496,9 @@ def test_transaction_name( segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 assert segments[0]["name"] == "/message/{message_id}" - assert segments[0]["attributes"]["sentry.segment.name.source"] == "route" + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + ) def test_transaction_name_in_traces_sampler( @@ -1525,7 +1563,9 @@ def test_transaction_name_in_middleware( segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 assert segments[0]["name"] == "http://testserver/message/123456" - assert segments[0]["attributes"]["sentry.segment.name.source"] == "url" + assert ( + segments[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "url" + ) def test_span_origin(sentry_init, capture_items): @@ -1550,7 +1590,10 @@ def test_span_origin(sentry_init, capture_items): assert len(items) > 0 for item in items: - assert item.payload["attributes"]["sentry.origin"] == "auto.http.starlette" + assert ( + item.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.starlette" + ) @pytest.mark.skipif( @@ -1581,7 +1624,7 @@ def test_segment_http_method_default(sentry_init, capture_items): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert segments[0]["attributes"]["http.request.method"] == "GET" + assert segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" def test_request_url(sentry_init, capture_items): @@ -1603,12 +1646,14 @@ def test_request_url(sentry_init, capture_items): spans = [item.payload for item in items] (server_span,) = ( - span for span in spans if span["attributes"].get("sentry.op") == "http.server" + span + for span in spans + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "http.server" ) - assert server_span["attributes"][SPANDATA.URL_FULL] == ( + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == ( "http://testserver/root/nomessage" ) - assert server_span["attributes"][SPANDATA.URL_PATH] == "/root/nomessage" + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/root/nomessage" @pytest.mark.skipif( @@ -1642,8 +1687,8 @@ def test_segment_http_method_custom(sentry_init, capture_items): segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 2 - assert segments[0]["attributes"]["http.request.method"] == "OPTIONS" - assert segments[1]["attributes"]["http.request.method"] == "HEAD" + assert segments[0]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "OPTIONS" + assert segments[1]["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "HEAD" @parametrize_test_configurable_status_codes diff --git a/tests/integrations/starlite/test_starlite.py b/tests/integrations/starlite/test_starlite.py index f05574df2d..e305ff85d4 100644 --- a/tests/integrations/starlite/test_starlite.py +++ b/tests/integrations/starlite/test_starlite.py @@ -4,6 +4,7 @@ from typing import Any, Dict import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from starlite import AbstractMiddleware, Controller, LoggingConfig, Starlite, get, post from starlite.middleware import LoggingMiddlewareConfig, RateLimitConfig from starlite.middleware.session.memory_backend import MemoryBackendConfig @@ -162,7 +163,10 @@ def test_transaction_name_and_source( spans = [item.payload for item in items] spans = [span for span in spans if expected_tx_name in span["name"]] assert len(spans) == 1 - assert spans[0]["attributes"]["sentry.segment.name.source"] == "component" + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) def test_middleware_spans(sentry_init, capture_items): @@ -197,7 +201,8 @@ def test_middleware_spans(sentry_init, capture_items): middleware_spans = [ item.payload for item in items - if item.payload.get("attributes", {}).get("sentry.op", "") == OP.MIDDLEWARE + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP, "") + == OP.MIDDLEWARE and item.payload.get("name") in expected ] assert len(middleware_spans) == 3 @@ -206,7 +211,7 @@ def test_middleware_spans(sentry_init, capture_items): for span in middleware_spans: assert span["name"] not in found found.add(span["name"]) - assert span["name"] == span["attributes"]["middleware.name"] + assert span["name"] == span["attributes"][ATTRIBUTE_NAMES.MIDDLEWARE_NAME] def test_middleware_callback_spans(sentry_init, capture_items): @@ -251,13 +256,14 @@ async def do_stuff(message): actual_starlite_spans = [ item.payload for item in items - if OP.MIDDLEWARE in item.payload.get("attributes", {}).get("sentry.op", "") + if OP.MIDDLEWARE + in item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP, "") ] assert len(actual_starlite_spans) == 3 def is_matching_span(expected_span, actual_span): return ( - expected_span["op"] == actual_span["attributes"]["sentry.op"] + expected_span["op"] == actual_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] and expected_span["description"] == actual_span["name"] ) @@ -344,13 +350,14 @@ async def my_send(*args, **kwargs): actual_starlite_spans = [ item.payload for item in items - if OP.MIDDLEWARE in item.payload.get("attributes", {}).get("sentry.op", "") + if OP.MIDDLEWARE + in item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP, "") ] assert len(actual_starlite_spans) == 3 def is_matching_span(expected_span, actual_span): return expected_span["op"] == actual_span["attributes"][ - "sentry.op" + ATTRIBUTE_NAMES.SENTRY_OP ] and actual_span["name"].startswith(expected_span["description"]) for expected_span in expected_starlite_spans: @@ -390,11 +397,15 @@ def test_span_origin(sentry_init, capture_items): starlite_items = [ item for item in items - if item.payload.get("attributes", {}).get("sentry.op", "") == OP.MIDDLEWARE + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP, "") + == OP.MIDDLEWARE ] assert len(starlite_items) > 0 for item in starlite_items: - assert item.payload["attributes"]["sentry.origin"] == "auto.http.starlite" + assert ( + item.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.starlite" + ) @pytest.mark.parametrize( diff --git a/tests/integrations/strawberry/test_strawberry.py b/tests/integrations/strawberry/test_strawberry.py index 95c242ee6c..bee5527689 100644 --- a/tests/integrations/strawberry/test_strawberry.py +++ b/tests/integrations/strawberry/test_strawberry.py @@ -1,6 +1,7 @@ from typing import AsyncGenerator, Optional import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk @@ -512,24 +513,26 @@ def test_capture_segment_on_error( assert segment["is_segment"] is True assert segment["name"] == "ErrorQuery" - assert segment["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY - assert query_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert query_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert query_span["name"] == "query ErrorQuery" - assert query_span["attributes"]["graphql.operation.type"] == "query" - assert query_span["attributes"]["graphql.operation.name"] == "ErrorQuery" + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" + assert ( + query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] == "ErrorQuery" + ) - assert query_span["attributes"]["graphql.document"] == query + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query - assert parse_span["attributes"]["sentry.op"] == OP.GRAPHQL_PARSE + assert parse_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_PARSE assert parse_span["name"] == "parsing" assert parse_span["parent_span_id"] == query_span["span_id"] - assert validate_span["attributes"]["sentry.op"] == OP.GRAPHQL_VALIDATE + assert validate_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_VALIDATE assert validate_span["name"] == "validation" assert validate_span["parent_span_id"] == query_span["span_id"] - assert resolve_span["attributes"]["sentry.op"] == OP.GRAPHQL_RESOLVE + assert resolve_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_RESOLVE assert resolve_span["name"] == "resolving Query.error" assert resolve_span["parent_span_id"] == query_span["span_id"] @@ -569,24 +572,27 @@ def test_capture_segment_on_success( assert segment["is_segment"] is True assert segment["name"] == "GreetingQuery" - assert segment["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY - assert query_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert query_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert query_span["name"] == "query GreetingQuery" - assert query_span["attributes"]["graphql.operation.type"] == "query" - assert query_span["attributes"]["graphql.operation.name"] == "GreetingQuery" + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" + assert ( + query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] + == "GreetingQuery" + ) - assert query_span["attributes"]["graphql.document"] == query + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query - assert parse_span["attributes"]["sentry.op"] == OP.GRAPHQL_PARSE + assert parse_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_PARSE assert parse_span["name"] == "parsing" assert parse_span["parent_span_id"] == query_span["span_id"] - assert validate_span["attributes"]["sentry.op"] == OP.GRAPHQL_VALIDATE + assert validate_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_VALIDATE assert validate_span["name"] == "validation" assert validate_span["parent_span_id"] == query_span["span_id"] - assert resolve_span["attributes"]["sentry.op"] == OP.GRAPHQL_RESOLVE + assert resolve_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_RESOLVE assert resolve_span["name"] == "resolving Query.hello" assert resolve_span["parent_span_id"] == query_span["span_id"] @@ -627,22 +633,22 @@ def test_segment_no_operation_name( assert segment["is_segment"] is True assert segment["name"] == "/graphql" - assert query_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY + assert query_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY assert query_span["name"] == "query" - assert query_span["attributes"]["graphql.operation.type"] == "query" - assert "graphql.operation.name" not in query_span["attributes"] + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" + assert ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME not in query_span["attributes"] - assert query_span["attributes"]["graphql.document"] == query + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query - assert parse_span["attributes"]["sentry.op"] == OP.GRAPHQL_PARSE + assert parse_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_PARSE assert parse_span["name"] == "parsing" assert parse_span["parent_span_id"] == query_span["span_id"] - assert validate_span["attributes"]["sentry.op"] == OP.GRAPHQL_VALIDATE + assert validate_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_VALIDATE assert validate_span["name"] == "validation" assert validate_span["parent_span_id"] == query_span["span_id"] - assert resolve_span["attributes"]["sentry.op"] == OP.GRAPHQL_RESOLVE + assert resolve_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_RESOLVE assert resolve_span["name"] == "resolving Query.hello" assert resolve_span["parent_span_id"] == query_span["span_id"] @@ -682,24 +688,27 @@ def test_segment_mutation( assert segment["is_segment"] is True assert segment["name"] == "Change" - assert segment["attributes"]["sentry.op"] == OP.GRAPHQL_MUTATION + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_MUTATION - assert mutation_span["attributes"]["sentry.op"] == OP.GRAPHQL_MUTATION + assert mutation_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_MUTATION assert mutation_span["name"] == "mutation" - assert mutation_span["attributes"]["graphql.operation.type"] == "mutation" - assert "graphql.operation.name" not in mutation_span["attributes"] + assert ( + mutation_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] + == "mutation" + ) + assert ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME not in mutation_span["attributes"] - assert mutation_span["attributes"]["graphql.document"] == query + assert mutation_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query - assert parse_span["attributes"]["sentry.op"] == OP.GRAPHQL_PARSE + assert parse_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_PARSE assert parse_span["name"] == "parsing" assert parse_span["parent_span_id"] == mutation_span["span_id"] - assert validate_span["attributes"]["sentry.op"] == OP.GRAPHQL_VALIDATE + assert validate_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_VALIDATE assert validate_span["name"] == "validation" assert validate_span["parent_span_id"] == mutation_span["span_id"] - assert resolve_span["attributes"]["sentry.op"] == OP.GRAPHQL_RESOLVE + assert resolve_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_RESOLVE assert resolve_span["name"] == "resolving Mutation.change" assert resolve_span["parent_span_id"] == mutation_span["span_id"] @@ -754,15 +763,18 @@ def test_graphql_span_data_collection( assert len(spans) == 5 query_span = spans[3] - assert query_span["attributes"]["sentry.op"] == OP.GRAPHQL_QUERY - assert query_span["attributes"]["graphql.operation.type"] == "query" + assert query_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRAPHQL_QUERY + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_TYPE] == "query" # operation.name is always set when an operation name is present - assert query_span["attributes"]["graphql.operation.name"] == "GreetingQuery" + assert ( + query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_OPERATION_NAME] + == "GreetingQuery" + ) if expect_document: - assert query_span["attributes"]["graphql.document"] == query + assert query_span["attributes"][ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT] == query else: - assert "graphql.document" not in query_span["attributes"] + assert ATTRIBUTE_NAMES.GRAPHQL_DOCUMENT not in query_span["attributes"] @parameterize_strawberry_test @@ -844,12 +856,18 @@ def test_span_origin_for_gql_mutations_and_related_ops( assert segment["is_segment"] is True if is_flask: - assert segment["attributes"]["sentry.origin"] == "auto.http.flask" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.flask" else: - assert segment["attributes"]["sentry.origin"] == "auto.http.starlette" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.starlette" + ) for span in (parse_span, validate_span, resolve_span, mutation_span): - assert span["attributes"]["sentry.origin"] == "auto.graphql.strawberry" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.graphql.strawberry" + ) @parameterize_strawberry_test @@ -892,12 +910,18 @@ def test_span_origin_for_gql_query_ops( assert segment["is_segment"] is True if is_flask: - assert segment["attributes"]["sentry.origin"] == "auto.http.flask" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.flask" else: - assert segment["attributes"]["sentry.origin"] == "auto.http.starlette" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.starlette" + ) for span in (parse_span, validate_span, resolve_span, query_span): - assert span["attributes"]["sentry.origin"] == "auto.graphql.strawberry" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.graphql.strawberry" + ) @parameterize_strawberry_test @@ -940,9 +964,15 @@ def test_span_origin3( assert segment["is_segment"] is True if is_flask: - assert segment["attributes"]["sentry.origin"] == "auto.http.flask" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.flask" else: - assert segment["attributes"]["sentry.origin"] == "auto.http.starlette" + assert ( + segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.starlette" + ) for span in (parse_span, validate_span, resolve_span, subscription_span): - assert span["attributes"]["sentry.origin"] == "auto.graphql.strawberry" + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.graphql.strawberry" + ) diff --git a/tests/integrations/tornado/test_tornado.py b/tests/integrations/tornado/test_tornado.py index c9f6b7dce2..dbfbb786ec 100644 --- a/tests/integrations/tornado/test_tornado.py +++ b/tests/integrations/tornado/test_tornado.py @@ -1,13 +1,13 @@ import json import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from tornado.testing import AsyncHTTPTestCase from tornado.web import Application, HTTPError, RequestHandler import sentry_sdk from sentry_sdk import capture_message from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.tornado import TornadoIntegration from tests.integrations.utils import ( DATA_COLLECTION_REMOTE_ADDR_CASES, @@ -230,14 +230,16 @@ def test_url_query_data_collection( (server_span,) = [item.payload for item in items] if expected_query is None: - assert "url.query" not in server_span["attributes"] - assert server_span["attributes"]["url.full"].endswith("/hi") - assert server_span["attributes"]["url.path"] == "/hi" + assert ATTRIBUTE_NAMES.URL_QUERY not in server_span["attributes"] + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith("/hi") + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/hi" else: - assert server_span["attributes"]["url.query"] == expected_query - assert server_span["attributes"]["url.full"].endswith(f"/hi?{expected_query}") - assert server_span["attributes"]["url.full"].startswith("http://") - assert server_span["attributes"]["url.path"] == "/hi" + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith( + f"/hi?{expected_query}" + ) + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].startswith("http://") + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/hi" def test_url_query_data_collection_no_query_string( @@ -258,9 +260,9 @@ def test_url_query_data_collection_no_query_string( (server_span,) = [item.payload for item in items] - assert "url.query" not in server_span["attributes"] - assert server_span["attributes"]["url.full"].endswith("/hi") - assert server_span["attributes"]["url.path"] == "/hi" + assert ATTRIBUTE_NAMES.URL_QUERY not in server_span["attributes"] + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith("/hi") + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/hi" def test_url_query_data_collection_repeated_and_blank_params( @@ -281,7 +283,7 @@ def test_url_query_data_collection_repeated_and_blank_params( (server_span,) = [item.payload for item in items] - assert server_span["attributes"]["url.query"] == "a=1&a=2&b=" + assert server_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "a=1&a=2&b=" @pytest.mark.parametrize( @@ -323,9 +325,11 @@ def test_request_body_data_collection( (server_span,) = [item.payload for item in items] if expect_body: - assert server_span["attributes"]["http.request.body.data"] == "heyoo" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] == "heyoo" + ) else: - assert "http.request.body.data" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] def test_oversized_request_body_not_annotated_data_collection( @@ -352,7 +356,7 @@ def test_oversized_request_body_not_annotated_data_collection( (server_span,) = [item.payload for item in items] - assert "http.request.body.data" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA not in server_span["attributes"] @pytest.mark.parametrize( @@ -475,21 +479,32 @@ def test_transactions( else "tests.integrations.tornado.test_tornado.CrashingHandler.post" ) assert server_segment["name"] == expected_handler - assert server_segment["attributes"]["sentry.segment.name.source"] == "component" - assert server_segment["attributes"]["http.request.method"] == "POST" - assert server_segment["attributes"]["http.request.body.data"] == "heyoo" - assert server_segment["attributes"]["http.response.status_code"] == code + assert ( + server_segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "component" + ) + assert server_segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "POST" + assert ( + server_segment["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_BODY_DATA] == "heyoo" + ) + assert ( + server_segment["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == code + ) assert server_segment["status"] == ("ok" if code == 200 else "error") assert client_segment["trace_id"] == server_segment["trace_id"] - assert server_segment["attributes"]["url.path"] == "/hi" + assert server_segment["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/hi" if expect_query: - assert server_segment["attributes"]["url.query"] == "foo=bar" - assert server_segment["attributes"]["url.full"].endswith("/hi?foo=bar") - assert server_segment["attributes"]["url.full"].startswith("http://") + assert server_segment["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "foo=bar" + assert server_segment["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith( + "/hi?foo=bar" + ) + assert server_segment["attributes"][ATTRIBUTE_NAMES.URL_FULL].startswith( + "http://" + ) else: - assert "url.query" not in server_segment["attributes"] - assert server_segment["attributes"]["url.full"].endswith("/hi") + assert ATTRIBUTE_NAMES.URL_QUERY not in server_segment["attributes"] + assert server_segment["attributes"][ATTRIBUTE_NAMES.URL_FULL].endswith("/hi") def test_400_not_logged(tornado_testcase, sentry_init, capture_events): @@ -794,7 +809,7 @@ def test_span_origin( sentry_sdk.flush() (segment,) = [i.payload for i in items] - assert segment["attributes"]["sentry.origin"] == "auto.http.tornado" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.tornado" @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -817,11 +832,11 @@ def test_user_ip_address_on_all_spans( child_span, server_span = [item.payload for item in items] if expect_ip: - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert child_span["attributes"]["user.ip_address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert child_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" else: - assert "user.ip_address" not in server_span["attributes"] - assert "user.ip_address" not in child_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in child_span["attributes"] @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -844,13 +859,14 @@ def test_client_address_span_attribute_data_collection( (server_span,) = [ item.payload for item in items - if item.payload["attributes"].get("sentry.origin") == "auto.http.tornado" + if item.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.tornado" ] if expect_ip: - assert server_span["attributes"]["client.address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert "client.address" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in server_span["attributes"] @pytest.mark.parametrize( @@ -894,8 +910,14 @@ def test_is_localhost_span_attribute( assert len(server_spans) == 1 assert len(child_spans) == 1 - assert server_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost - assert child_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost + assert ( + server_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] + == is_localhost + ) + assert ( + child_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] + == is_localhost + ) def test_user_agent_attribute(tornado_testcase, sentry_init, capture_items): @@ -918,10 +940,12 @@ def test_user_agent_attribute(tornado_testcase, sentry_init, capture_items): assert len(child_spans) == 1 assert ( - server_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0" + server_spans[0]["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" ) assert ( - child_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0" + child_spans[0]["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" ) diff --git a/tests/integrations/wsgi/test_wsgi.py b/tests/integrations/wsgi/test_wsgi.py index f36e71bdfc..7b43e05e96 100644 --- a/tests/integrations/wsgi/test_wsgi.py +++ b/tests/integrations/wsgi/test_wsgi.py @@ -2,6 +2,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from werkzeug.test import Client import sentry_sdk @@ -216,22 +217,25 @@ def dogpark(environ, start_response): assert span["is_segment"] is True assert span["name"] == "generic WSGI request" - assert span["attributes"]["sentry.op"] == "http.server" - assert span["attributes"]["sentry.segment.name.source"] == "route" - assert span["attributes"]["http.request.method"] == "GET" - assert span["attributes"]["http.response.status_code"] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.server" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "route" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 assert span["status"] == "ok" - assert span["attributes"]["url.path"] == "/dogs/are/great" + assert span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/dogs/are/great" if expect_query: assert ( - span["attributes"]["url.full"] + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == "http://localhost/dogs/are/great?toy=tennisball" ) - assert span["attributes"]["http.query"] == "toy=tennisball" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] == "toy=tennisball" else: - assert span["attributes"]["url.full"] == "http://localhost/dogs/are/great" - assert "http.query" not in span["attributes"] + assert ( + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == "http://localhost/dogs/are/great" + ) + assert ATTRIBUTE_NAMES.HTTP_QUERY not in span["attributes"] def test_has_trace_if_performance_enabled( @@ -516,7 +520,7 @@ def dogpark(environ, start_response): sentry_sdk.flush() assert len(items) == 1 - assert items[0].payload["attributes"]["sentry.origin"] == "manual" + assert items[0].payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" def test_span_origin_custom(sentry_init, capture_items): @@ -540,7 +544,10 @@ def dogpark(environ, start_response): sentry_sdk.flush() assert len(items) == 1 - assert items[0].payload["attributes"]["sentry.origin"] == "auto.dogpark.deluxe" + assert ( + items[0].payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.dogpark.deluxe" + ) @pytest.mark.parametrize( @@ -905,15 +912,18 @@ def dogpark(environ, start_response): (span,) = [item.payload for item in items] if expected_query is None: - assert "http.query" not in span["attributes"] - assert span["attributes"]["url.full"] == "http://localhost/dogs/are/great" + assert ATTRIBUTE_NAMES.HTTP_QUERY not in span["attributes"] + assert ( + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] + == "http://localhost/dogs/are/great" + ) else: - assert span["attributes"]["http.query"] == expected_query + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_QUERY] == expected_query assert ( - span["attributes"]["url.full"] + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == f"http://localhost/dogs/are/great?{expected_query}" ) - assert span["attributes"]["url.path"] == "/dogs/are/great" + assert span["attributes"][ATTRIBUTE_NAMES.URL_PATH] == "/dogs/are/great" @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -942,13 +952,13 @@ def dogpark(environ, start_response): child_span, server_span = [item.payload for item in items] if expect_ip: - assert server_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert child_span["attributes"]["user.ip_address"] == "127.0.0.1" - assert server_span["attributes"]["client.address"] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert child_span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" + assert server_span["attributes"][ATTRIBUTE_NAMES.CLIENT_ADDRESS] == "127.0.0.1" else: - assert "user.ip_address" not in server_span["attributes"] - assert "user.ip_address" not in child_span["attributes"] - assert "client.address" not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in server_span["attributes"] + assert ATTRIBUTE_NAMES.USER_IP_ADDRESS not in child_span["attributes"] + assert ATTRIBUTE_NAMES.CLIENT_ADDRESS not in server_span["attributes"] @pytest.mark.parametrize("data_collection, expect_ip", DATA_COLLECTION_USER_INFO_CASES) @@ -1037,8 +1047,10 @@ def dogpark(environ, start_response): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["sentry.is_localhost"] is is_localhost - assert child_span["attributes"]["sentry.is_localhost"] is is_localhost + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost + ) + assert child_span["attributes"][ATTRIBUTE_NAMES.SENTRY_IS_LOCALHOST] is is_localhost def test_user_agent_attribute(sentry_init, capture_items): @@ -1063,5 +1075,11 @@ def dogpark(environ, start_response): child_span, server_span = [item.payload for item in items] - assert server_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" - assert child_span["attributes"]["user_agent.original"] == "TestBrowser/1.0" + assert ( + server_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + ) + assert ( + child_span["attributes"][ATTRIBUTE_NAMES.USER_AGENT_ORIGINAL] + == "TestBrowser/1.0" + )