From 784646b7b599a6aa13d3b967723fee28cfe1626f Mon Sep 17 00:00:00 2001 From: srothh Date: Wed, 25 Nov 2026 15:16:52 +0100 Subject: [PATCH 1/2] ref(attributes): Enforce sentry-conventions in network integrations Enforce sentry-conventions on attribute names in network integrations (grpc,httpx,httpx2,preqwest,socket,stdlib) GH-8323 PY-3090 --- sentry_sdk/integrations/grpc/aio/client.py | 20 ++++++++----- sentry_sdk/integrations/grpc/aio/server.py | 8 +++-- sentry_sdk/integrations/grpc/client.py | 20 ++++++++----- sentry_sdk/integrations/grpc/server.py | 8 +++-- sentry_sdk/integrations/httpx.py | 22 +++++++++----- sentry_sdk/integrations/httpx2.py | 22 +++++++++----- sentry_sdk/integrations/pyreqwest.py | 12 ++++---- sentry_sdk/integrations/socket.py | 24 ++++++++------- sentry_sdk/integrations/stdlib.py | 34 ++++++++++++---------- 9 files changed, 100 insertions(+), 70 deletions(-) diff --git a/sentry_sdk/integrations/grpc/aio/client.py b/sentry_sdk/integrations/grpc/aio/client.py index 67f85ef167..fe2aec4019 100644 --- a/sentry_sdk/integrations/grpc/aio/client.py +++ b/sentry_sdk/integrations/grpc/aio/client.py @@ -1,7 +1,9 @@ from typing import Any, AsyncIterable, Callable, Union +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 import DidNotEnable from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN @@ -58,9 +60,9 @@ async def intercept_unary_unary( with sentry_sdk.start_span( name="unary unary call to %s" % method.decode(), attributes={ - "sentry.op": OP.GRPC_CLIENT, - "sentry.origin": SPAN_ORIGIN, - SPANDATA.RPC_METHOD: method.decode(), + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.RPC_METHOD: method.decode(), }, ) as span: client_call_details = self._update_client_call_details_metadata_from_scope( @@ -69,7 +71,9 @@ async def intercept_unary_unary( response = await continuation(client_call_details, request) status_code = await response.code() - span.set_attribute(SPANDATA.RPC_RESPONSE_STATUS_CODE, status_code.name) + span.set_attribute( + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE, status_code.name + ) return response @@ -95,9 +99,9 @@ async def intercept_unary_stream( with sentry_sdk.start_span( name="unary stream call to %s" % method.decode(), attributes={ - "sentry.op": OP.GRPC_CLIENT, - "sentry.origin": SPAN_ORIGIN, - SPANDATA.RPC_METHOD: method.decode(), + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.RPC_METHOD: method.decode(), }, ): client_call_details = self._update_client_call_details_metadata_from_scope( diff --git a/sentry_sdk/integrations/grpc/aio/server.py b/sentry_sdk/integrations/grpc/aio/server.py index b6bc8a6b40..bca5eecd7d 100644 --- a/sentry_sdk/integrations/grpc/aio/server.py +++ b/sentry_sdk/integrations/grpc/aio/server.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable @@ -58,9 +60,9 @@ async def wrapped(request: "Any", context: "ServicerContext") -> "Any": with sentry_sdk.start_span( name=name, attributes={ - "sentry.op": OP.GRPC_SERVER, - "sentry.segment.name.source": SegmentNameSource.CUSTOM.value, - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_SERVER, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.CUSTOM.value, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, }, parent_span=None, ): diff --git a/sentry_sdk/integrations/grpc/client.py b/sentry_sdk/integrations/grpc/client.py index 82e2190f40..1618c986a3 100644 --- a/sentry_sdk/integrations/grpc/client.py +++ b/sentry_sdk/integrations/grpc/client.py @@ -1,7 +1,9 @@ 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 import DidNotEnable from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN @@ -38,9 +40,9 @@ def intercept_unary_unary( with sentry_sdk.start_span( name="unary unary call to %s" % method, attributes={ - "sentry.op": OP.GRPC_CLIENT, - "sentry.origin": SPAN_ORIGIN, - SPANDATA.RPC_METHOD: method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.RPC_METHOD: method, }, ) as span: client_call_details = self._update_client_call_details_metadata_from_scope( @@ -48,7 +50,9 @@ def intercept_unary_unary( ) response = continuation(client_call_details, request) - span.set_attribute(SPANDATA.RPC_RESPONSE_STATUS_CODE, response.code().name) + span.set_attribute( + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE, response.code().name + ) return response @@ -69,9 +73,9 @@ def intercept_unary_stream( with sentry_sdk.start_span( name="unary stream call to %s" % method, attributes={ - "sentry.op": OP.GRPC_CLIENT, - "sentry.origin": SPAN_ORIGIN, - SPANDATA.RPC_METHOD: method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.RPC_METHOD: method, }, ): client_call_details = self._update_client_call_details_metadata_from_scope( diff --git a/sentry_sdk/integrations/grpc/server.py b/sentry_sdk/integrations/grpc/server.py index 79b05cb275..58cd1bb427 100644 --- a/sentry_sdk/integrations/grpc/server.py +++ b/sentry_sdk/integrations/grpc/server.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable @@ -51,9 +53,9 @@ def behavior(request: "Message", context: "ServicerContext") -> "Message": with sentry_sdk.start_span( name=name, attributes={ - "sentry.op": OP.GRPC_SERVER, - "sentry.segment.name.source": SegmentNameSource.CUSTOM.value, - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: OP.GRPC_SERVER, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.CUSTOM.value, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, }, parent_span=None, ): diff --git a/sentry_sdk/integrations/httpx.py b/sentry_sdk/integrations/httpx.py index a3a79a67d5..2f56a10e9a 100644 --- a/sentry_sdk/integrations/httpx.py +++ b/sentry_sdk/integrations/httpx.py @@ -1,6 +1,8 @@ from contextlib import nullcontext 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.integrations import DidNotEnable, Integration, _check_minimum_version @@ -71,9 +73,9 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": HttpxIntegration.origin, - "http.request.method": request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: HttpxIntegration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, }, ) @@ -85,7 +87,9 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": if span is not None: span.status = "error" if rv.status_code >= 400 else "ok" - span.set_attribute("http.response.status_code", rv.status_code) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, rv.status_code + ) finally: if span is not None: span.set_attributes(url_attributes) @@ -142,9 +146,9 @@ async def send( parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": HttpxIntegration.origin, - "http.request.method": request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: HttpxIntegration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, }, ) @@ -158,7 +162,9 @@ async def send( if span is not None: span.status = "error" if rv.status_code >= 400 else "ok" - span.set_attribute("http.response.status_code", rv.status_code) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, rv.status_code + ) finally: if span is not None: span.set_attributes(url_attributes) diff --git a/sentry_sdk/integrations/httpx2.py b/sentry_sdk/integrations/httpx2.py index 2f382601a3..bebb728328 100644 --- a/sentry_sdk/integrations/httpx2.py +++ b/sentry_sdk/integrations/httpx2.py @@ -1,6 +1,8 @@ from contextlib import nullcontext 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.integrations import DidNotEnable, Integration, _check_minimum_version @@ -71,9 +73,9 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": Httpx2Integration.origin, - "http.request.method": request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: Httpx2Integration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, }, ) @@ -85,7 +87,9 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": if span is not None: span.status = "error" if rv.status_code >= 400 else "ok" - span.set_attribute("http.response.status_code", rv.status_code) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, rv.status_code + ) finally: if span is not None: span.set_attributes(url_attributes) @@ -142,9 +146,9 @@ async def send( parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": Httpx2Integration.origin, - "http.request.method": request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: Httpx2Integration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, }, ) @@ -158,7 +162,9 @@ async def send( if span is not None: span.status = "error" if rv.status_code >= 400 else "ok" - span.set_attribute("http.response.status_code", rv.status_code) + span.set_attribute( + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, rv.status_code + ) finally: if span is not None: span.set_attributes(url_attributes) diff --git a/sentry_sdk/integrations/pyreqwest.py b/sentry_sdk/integrations/pyreqwest.py index 4c90414095..3f121a0ed5 100644 --- a/sentry_sdk/integrations/pyreqwest.py +++ b/sentry_sdk/integrations/pyreqwest.py @@ -1,6 +1,8 @@ from contextlib import contextmanager from typing import TYPE_CHECKING, Any, Generator +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -124,9 +126,9 @@ def _sentry_pyreqwest_span( with sentry_sdk.start_span( name=f"{request.method} {parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE}", attributes={ - "sentry.op": OP.HTTP_CLIENT, - "sentry.origin": PyreqwestIntegration.origin, - SPANDATA.HTTP_REQUEST_METHOD: request.method, + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: PyreqwestIntegration.origin, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: request.method, }, ) as span: for key, value in url_attributes.items(): @@ -163,7 +165,7 @@ async def sentry_async_middleware( if span is not None: span.status = "error" if response.status >= 400 else "ok" span.set_attribute( - SPANDATA.HTTP_STATUS_CODE, + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, response.status, ) @@ -201,7 +203,7 @@ def sentry_sync_middleware( if span is not None: span.status = "error" if response.status >= 400 else "ok" span.set_attribute( - SPANDATA.HTTP_STATUS_CODE, + ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, response.status, ) diff --git a/sentry_sdk/integrations/socket.py b/sentry_sdk/integrations/socket.py index 10ea5501c4..7b2070ecbf 100644 --- a/sentry_sdk/integrations/socket.py +++ b/sentry_sdk/integrations/socket.py @@ -1,8 +1,10 @@ import socket +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk._types import MYPY -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations import Integration if MYPY: @@ -61,13 +63,13 @@ def create_connection( with sentry_sdk.start_span( name=_get_span_description(address[0], address[1]), attributes={ - "sentry.op": OP.SOCKET_CONNECTION, - "sentry.origin": SocketIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.SOCKET_CONNECTION, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SocketIntegration.origin, }, ) as span: if address[0] is not None: - span.set_attribute(SPANDATA.SERVER_ADDRESS, address[0]) - span.set_attribute(SPANDATA.SERVER_PORT, address[1]) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, address[0]) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, address[1]) return real_create_connection( address=address, timeout=timeout, source_address=source_address @@ -98,22 +100,22 @@ def getaddrinfo( with sentry_sdk.start_span( name=_get_span_description(host, port), attributes={ - "sentry.op": OP.SOCKET_DNS, - "sentry.origin": SocketIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.SOCKET_DNS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SocketIntegration.origin, }, ) as span: if isinstance(host, str): - span.set_attribute(SPANDATA.SERVER_ADDRESS, host) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, host) elif isinstance(host, bytes): span.set_attribute( - SPANDATA.SERVER_ADDRESS, host.decode(errors="replace") + ATTRIBUTE_NAMES.SERVER_ADDRESS, host.decode(errors="replace") ) if isinstance(port, int): - span.set_attribute(SPANDATA.SERVER_PORT, port) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, port) elif port is not None: try: - span.set_attribute(SPANDATA.SERVER_PORT, int(port)) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, int(port)) except (ValueError, TypeError): pass diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index d52566be04..c87acb50f9 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -5,6 +5,8 @@ from http.client import HTTPConnection, HTTPResponse from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import _SENTRY_HEADER_NAMES, OP, SPANDATA from sentry_sdk.integrations import Integration @@ -297,7 +299,7 @@ def putrequest( if parent_span is not None: is_inactive_boto3_span = ( client.get_integration("boto3") is not None - and parent_span.get_attributes().get(SPANDATA.SENTRY_ORIGIN) + and parent_span.get_attributes().get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) == getattr(client.get_integration("boto3"), "origin", None) and not getattr(parent_span, "active", True) ) @@ -309,9 +311,9 @@ def putrequest( parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, ), attributes={ - "sentry.origin": "auto.http.stdlib.httplib", - "sentry.op": OP.HTTP_CLIENT, - SPANDATA.HTTP_REQUEST_METHOD: method, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.http.stdlib.httplib", + ATTRIBUTE_NAMES.SENTRY_OP: OP.HTTP_CLIENT, + ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD: method, }, # boto3 integration owns span's lifecycle; keep child inactive so it # can't restore boto3 span later on. @@ -325,8 +327,8 @@ def putrequest( # for proxies, these point to the proxy host/port if tunnel_host: if span: - span.set_attribute(SPANDATA.NETWORK_PEER_ADDRESS, self.host) - span.set_attribute(SPANDATA.NETWORK_PEER_PORT, self.port) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS, self.host) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_PORT, self.port) breadcrumb.update( { @@ -391,7 +393,7 @@ def getresponse(self: "HTTPConnection", *args: "Any", **kwargs: "Any") -> "Any": return rv span.status = "error" if status_code >= 400 else "ok" - span.set_attribute(SPANDATA.HTTP_STATUS_CODE, status_code) + span.set_attribute(ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE, status_code) # getresponse doesn't include actually reading the response body. This # is done in read(). So if the metadata/headers suggest there's a body to @@ -520,8 +522,8 @@ def sentry_patched_popen_init( with sentry_sdk.start_span( name=description, attributes={ - "sentry.op": OP.SUBPROCESS, - "sentry.origin": "auto.subprocess.stdlib.subprocess", + ATTRIBUTE_NAMES.SENTRY_OP: OP.SUBPROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.subprocess.stdlib.subprocess", }, ) as span: for k, v in sentry_sdk.get_current_scope().iter_trace_propagation_headers( @@ -539,7 +541,7 @@ def sentry_patched_popen_init( rv = old_popen_init(self, *a, **kw) - span.set_attribute(SPANDATA.PROCESS_PID, self.pid) + span.set_attribute(ATTRIBUTE_NAMES.PROCESS_PID, self.pid) return rv @@ -556,11 +558,11 @@ def sentry_patched_popen_wait( with sentry_sdk.start_span( name=OP.SUBPROCESS_WAIT, attributes={ - "sentry.op": OP.SUBPROCESS_WAIT, - "sentry.origin": "auto.subprocess.stdlib.subprocess", + ATTRIBUTE_NAMES.SENTRY_OP: OP.SUBPROCESS_WAIT, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.subprocess.stdlib.subprocess", }, ) as span: - span.set_attribute(SPANDATA.PROCESS_PID, self.pid) + span.set_attribute(ATTRIBUTE_NAMES.PROCESS_PID, self.pid) return old_popen_wait(self, *a, **kw) subprocess.Popen.wait = sentry_patched_popen_wait # type: ignore @@ -576,11 +578,11 @@ def sentry_patched_popen_communicate( with sentry_sdk.start_span( name=OP.SUBPROCESS_COMMUNICATE, attributes={ - "sentry.op": OP.SUBPROCESS_COMMUNICATE, - "sentry.origin": "auto.subprocess.stdlib.subprocess", + ATTRIBUTE_NAMES.SENTRY_OP: OP.SUBPROCESS_COMMUNICATE, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.subprocess.stdlib.subprocess", }, ) as span: - span.set_attribute(SPANDATA.PROCESS_PID, self.pid) + span.set_attribute(ATTRIBUTE_NAMES.PROCESS_PID, self.pid) return old_popen_communicate(self, *a, **kw) subprocess.Popen.communicate = sentry_patched_popen_communicate # type: ignore From dce4d0a900e57341c202667c8e760b3f755a7909 Mon Sep 17 00:00:00 2001 From: srothh Date: Thu, 26 Nov 2026 11:25:51 +0100 Subject: [PATCH 2/2] test(attributes): Enforce sentry-conventions in network tests Enforce sentry-conventions attribute names in network integration tests (grpc, httpx, httpx2, pyreqwest, socket, stdlib). GH-8323 PY-3090 Co-Authored-By: Claude Opus 5.5 --- tests/integrations/grpc/test_grpc.py | 126 ++++---- tests/integrations/grpc/test_grpc_aio.py | 84 +++--- tests/integrations/httpx/test_httpx.py | 269 ++++++++++-------- tests/integrations/httpx2/test_httpx2.py | 269 ++++++++++-------- .../integrations/pyreqwest/test_pyreqwest.py | 155 +++++----- tests/integrations/socket/test_socket.py | 33 ++- tests/integrations/stdlib/test_httplib.py | 131 +++++---- tests/integrations/stdlib/test_subprocess.py | 31 +- 8 files changed, 602 insertions(+), 496 deletions(-) diff --git a/tests/integrations/grpc/test_grpc.py b/tests/integrations/grpc/test_grpc.py index 949d7a92a5..1e29520d6b 100644 --- a/tests/integrations/grpc/test_grpc.py +++ b/tests/integrations/grpc/test_grpc.py @@ -5,6 +5,7 @@ import grpc import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -77,9 +78,11 @@ def test_grpc_server_starts_transaction( spans = [item["payload"] for item in items if item["type"] == "span"] span = spans[0] - assert spans[1]["attributes"]["sentry.segment.name.source"] == "custom" - assert spans[1]["attributes"]["sentry.op"] == OP.GRPC_SERVER - assert span["attributes"]["sentry.op"] == "test" + assert ( + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" + ) + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "test" @pytest.mark.forked @@ -117,9 +120,11 @@ def test_grpc_server_other_interceptors( spans = [item["payload"] for item in items if item["type"] == "span"] span = spans[0] - assert spans[1]["attributes"]["sentry.segment.name.source"] == "custom" - assert spans[1]["attributes"]["sentry.op"] == OP.GRPC_SERVER - assert span["attributes"]["sentry.op"] == "test" + assert ( + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" + ) + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "test" @pytest.mark.forked @@ -167,10 +172,12 @@ def test_grpc_server_continues_transaction( spans = [item["payload"] for item in items if item["type"] == "span"] span = spans[0] - assert spans[1]["attributes"]["sentry.segment.name.source"] == "custom" - assert spans[1]["attributes"]["sentry.op"] == OP.GRPC_SERVER + assert ( + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" + ) + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_SERVER assert spans[1]["trace_id"] == segment_span.trace_id - assert span["attributes"]["sentry.op"] == "test" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "test" @pytest.mark.forked @@ -201,26 +208,26 @@ def test_grpc_client_starts_span( span = spans[2] assert len(spans) == 4 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary unary call to /grpc_test_server.gRPCTestService/TestServe" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestServe", - "sentry.environment": mock.ANY, - "sentry.op": "grpc.client", - "sentry.origin": "auto.grpc.grpc", - "sentry.release": mock.ANY, - "sentry.sdk.name": "sentry.python", - "sentry.sdk.version": mock.ANY, - "sentry.segment.id": mock.ANY, - "sentry.segment.name": "custom parent", - "server.address": mock.ANY, - "thread.id": mock.ANY, - "thread.name": mock.ANY, - "rpc.response.status_code": "OK", + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestServe", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_OP: "grpc.client", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc", + ATTRIBUTE_NAMES.SENTRY_RELEASE: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: "sentry.python", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: "custom parent", + ATTRIBUTE_NAMES.SERVER_ADDRESS: mock.ANY, + ATTRIBUTE_NAMES.THREAD_ID: mock.ANY, + ATTRIBUTE_NAMES.THREAD_NAME: mock.ANY, + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE: "OK", } ) @@ -253,25 +260,25 @@ def test_grpc_client_unary_stream_starts_span( span = spans[0] assert len(spans) == 2 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary stream call to /grpc_test_server.gRPCTestService/TestUnaryStream" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestUnaryStream", - "sentry.environment": mock.ANY, - "sentry.op": "grpc.client", - "sentry.origin": "auto.grpc.grpc", - "sentry.release": mock.ANY, - "sentry.sdk.name": "sentry.python", - "sentry.sdk.version": mock.ANY, - "sentry.segment.id": mock.ANY, - "sentry.segment.name": "custom parent", - "server.address": mock.ANY, - "thread.id": mock.ANY, - "thread.name": mock.ANY, + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestUnaryStream", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_OP: "grpc.client", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc", + ATTRIBUTE_NAMES.SENTRY_RELEASE: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: "sentry.python", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: "custom parent", + ATTRIBUTE_NAMES.SERVER_ADDRESS: mock.ANY, + ATTRIBUTE_NAMES.THREAD_ID: mock.ANY, + ATTRIBUTE_NAMES.THREAD_NAME: mock.ANY, } ) @@ -318,26 +325,26 @@ def test_grpc_client_other_interceptor( span = spans[2] assert len(spans) == 4 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary unary call to /grpc_test_server.gRPCTestService/TestServe" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestServe", - "sentry.environment": mock.ANY, - "sentry.op": "grpc.client", - "sentry.origin": "auto.grpc.grpc", - "sentry.release": mock.ANY, - "sentry.sdk.name": "sentry.python", - "sentry.sdk.version": mock.ANY, - "sentry.segment.id": mock.ANY, - "sentry.segment.name": "custom parent", - "server.address": mock.ANY, - "thread.id": mock.ANY, - "thread.name": mock.ANY, - "rpc.response.status_code": "OK", + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestServe", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_OP: "grpc.client", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc", + ATTRIBUTE_NAMES.SENTRY_RELEASE: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: "sentry.python", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: "custom parent", + ATTRIBUTE_NAMES.SERVER_ADDRESS: mock.ANY, + ATTRIBUTE_NAMES.THREAD_ID: mock.ANY, + ATTRIBUTE_NAMES.THREAD_NAME: mock.ANY, + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE: "OK", } ) @@ -371,15 +378,15 @@ def test_prevent_dual_client_interceptor( span = spans[2] assert len(spans) == 4 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary unary call to /grpc_test_server.gRPCTestService/TestServe" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestServe", - "rpc.response.status_code": "OK", + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestServe", + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE: "OK", } ) @@ -479,13 +486,14 @@ def test_span_origin( spans = [item["payload"] for item in items if item["type"] == "span"] - assert spans[1]["attributes"]["sentry.origin"] == "auto.grpc.grpc" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.grpc.grpc" assert ( - spans[0]["attributes"]["sentry.origin"] == "auto.grpc.grpc.TestService" + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.grpc.grpc.TestService" ) # manually created in TestService, not the instrumentation - assert spans[3]["attributes"]["sentry.origin"] == "manual" - assert spans[2]["attributes"]["sentry.origin"] == "auto.grpc.grpc" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.grpc.grpc" class TestService(gRPCTestServiceServicer): @@ -496,8 +504,8 @@ def TestServe(request, context): with sentry_sdk.start_span( name="test", attributes={ - "sentry.op": "test", - "sentry.origin": "auto.grpc.grpc.TestService", + ATTRIBUTE_NAMES.SENTRY_OP: "test", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc.TestService", }, ): pass diff --git a/tests/integrations/grpc/test_grpc_aio.py b/tests/integrations/grpc/test_grpc_aio.py index ac136cadc8..c25d3c6bb8 100644 --- a/tests/integrations/grpc/test_grpc_aio.py +++ b/tests/integrations/grpc/test_grpc_aio.py @@ -5,6 +5,7 @@ import grpc import pytest import pytest_asyncio +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -109,9 +110,11 @@ async def test_grpc_server_starts_transaction( spans = [item.payload for item in items] span = spans[0] - assert spans[1]["attributes"]["sentry.segment.name.source"] == "custom" - assert spans[1]["attributes"]["sentry.op"] == OP.GRPC_SERVER - assert span["attributes"]["sentry.op"] == "test" + assert ( + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" + ) + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "test" @pytest.mark.asyncio @@ -150,10 +153,12 @@ async def test_grpc_server_continues_transaction( spans = [item.payload for item in items] span = spans[0] - assert spans[1]["attributes"]["sentry.segment.name.source"] == "custom" - assert spans[1]["attributes"]["sentry.op"] == OP.GRPC_SERVER + assert ( + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "custom" + ) + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_SERVER assert spans[1]["trace_id"] == segment_span.trace_id - assert span["attributes"]["sentry.op"] == "test" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "test" @pytest.mark.asyncio @@ -221,26 +226,26 @@ async def test_grpc_client_starts_span( span = spans[2] assert len(spans) == 4 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary unary call to /grpc_test_server.gRPCTestService/TestServe" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestServe", - "sentry.environment": mock.ANY, - "sentry.op": "grpc.client", - "sentry.origin": "auto.grpc.grpc", - "sentry.release": mock.ANY, - "sentry.sdk.name": "sentry.python", - "sentry.sdk.version": mock.ANY, - "sentry.segment.id": mock.ANY, - "sentry.segment.name": "custom parent", - "server.address": mock.ANY, - "thread.id": mock.ANY, - "thread.name": mock.ANY, - "rpc.response.status_code": "OK", + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestServe", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_OP: "grpc.client", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc", + ATTRIBUTE_NAMES.SENTRY_RELEASE: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: "sentry.python", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: "custom parent", + ATTRIBUTE_NAMES.SERVER_ADDRESS: mock.ANY, + ATTRIBUTE_NAMES.THREAD_ID: mock.ANY, + ATTRIBUTE_NAMES.THREAD_NAME: mock.ANY, + ATTRIBUTE_NAMES.RPC_RESPONSE_STATUS_CODE: "OK", } ) @@ -267,25 +272,25 @@ async def test_grpc_client_unary_stream_starts_span( span = spans[0] assert len(spans) == 2 - assert span["attributes"]["sentry.op"] == OP.GRPC_CLIENT + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.GRPC_CLIENT assert ( span["name"] == "unary stream call to /grpc_test_server.gRPCTestService/TestUnaryStream" ) assert span["attributes"] == ApproxDict( { - "rpc.method": "/grpc_test_server.gRPCTestService/TestUnaryStream", - "sentry.environment": mock.ANY, - "sentry.op": "grpc.client", - "sentry.origin": "auto.grpc.grpc", - "sentry.release": mock.ANY, - "sentry.sdk.name": "sentry.python", - "sentry.sdk.version": mock.ANY, - "sentry.segment.id": mock.ANY, - "sentry.segment.name": "custom parent", - "server.address": mock.ANY, - "thread.id": mock.ANY, - "thread.name": mock.ANY, + ATTRIBUTE_NAMES.RPC_METHOD: "/grpc_test_server.gRPCTestService/TestUnaryStream", + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_OP: "grpc.client", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc", + ATTRIBUTE_NAMES.SENTRY_RELEASE: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: "sentry.python", + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: mock.ANY, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: "custom parent", + ATTRIBUTE_NAMES.SERVER_ADDRESS: mock.ANY, + ATTRIBUTE_NAMES.THREAD_ID: mock.ANY, + ATTRIBUTE_NAMES.THREAD_NAME: mock.ANY, } ) @@ -338,13 +343,14 @@ async def test_span_origin( spans = [item["payload"] for item in items if item["type"] == "span"] - assert spans[1]["attributes"]["sentry.origin"] == "auto.grpc.grpc" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.grpc.grpc" assert ( - spans[0]["attributes"]["sentry.origin"] == "auto.grpc.grpc.TestService.aio" + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.grpc.grpc.TestService.aio" ) # manually created in TestService, not the instrumentation - assert spans[3]["attributes"]["sentry.origin"] == "manual" - assert spans[2]["attributes"]["sentry.origin"] == "auto.grpc.grpc" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.grpc.grpc" class TestService(gRPCTestServiceServicer): @@ -359,8 +365,8 @@ async def TestServe(cls, request, context): with sentry_sdk.start_span( name="test", attributes={ - "sentry.op": "test", - "sentry.origin": "auto.grpc.grpc.TestService.aio", + ATTRIBUTE_NAMES.SENTRY_OP: "test", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.grpc.grpc.TestService.aio", }, ): pass diff --git a/tests/integrations/httpx/test_httpx.py b/tests/integrations/httpx/test_httpx.py index cd73f1bc34..8269cd9fb2 100644 --- a/tests/integrations/httpx/test_httpx.py +++ b/tests/integrations/httpx/test_httpx.py @@ -3,6 +3,7 @@ import httpx import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -15,7 +16,8 @@ def _get_http_client_span(items): return next( item.payload for item in items - if item.payload.get("attributes", {}).get("sentry.op") == OP.HTTP_CLIENT + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.HTTP_CLIENT ) @@ -632,10 +634,10 @@ def test_request_source_disabled_sync(sentry_init, capture_items, httpx_mock): http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.asyncio @@ -660,10 +662,10 @@ async def test_request_source_disabled_async(sentry_init, capture_items, httpx_m http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.parametrize("enable_http_request_source", [None, True]) @@ -697,10 +699,10 @@ def test_request_source_enabled_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] @pytest.mark.asyncio @@ -732,10 +734,10 @@ async def test_request_source_enabled_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] def test_request_source_sync(sentry_init, capture_items, httpx_mock): @@ -759,25 +761,30 @@ def test_request_source_sync(sentry_init, capture_items, httpx_mock): http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx.test_httpx" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx/test_httpx.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path - assert http_span["attributes"][SPANDATA.CODE_FUNCTION] == "test_request_source_sync" + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "test_request_source_sync" + ) @pytest.mark.asyncio @@ -802,26 +809,29 @@ async def test_request_source_async(sentry_init, capture_items, httpx_mock): http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx.test_httpx" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx/test_httpx.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] == "test_request_source_async" + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "test_request_source_async" ) @@ -853,20 +863,31 @@ def test_request_source_with_module_in_search_path_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 - assert http_span["attributes"][SPANDATA.CODE_NAMESPACE] == "httpx_helpers.helpers" - assert http_span["attributes"]["code.file.path"] == "httpx_helpers/helpers.py" + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] + == "httpx_helpers.helpers" + ) + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH] + == "httpx_helpers/helpers.py" + ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path - assert http_span["attributes"][SPANDATA.CODE_FUNCTION] == "get_request_with_client" + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "get_request_with_client" + ) @pytest.mark.asyncio @@ -898,21 +919,29 @@ async def test_request_source_with_module_in_search_path_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 - assert http_span["attributes"][SPANDATA.CODE_NAMESPACE] == "httpx_helpers.helpers" - assert http_span["attributes"]["code.file.path"] == "httpx_helpers/helpers.py" + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] + == "httpx_helpers.helpers" + ) + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH] + == "httpx_helpers/helpers.py" + ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "async_get_request_with_client" ) @@ -941,10 +970,10 @@ def test_no_request_source_if_duration_too_short_sync( http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.asyncio @@ -972,10 +1001,10 @@ async def test_no_request_source_if_duration_too_short_async( http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] def test_request_source_if_duration_over_threshold_sync( @@ -1002,26 +1031,28 @@ def test_request_source_if_duration_over_threshold_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx.test_httpx" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx/test_httpx.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "test_request_source_if_duration_over_threshold_sync" ) @@ -1051,26 +1082,28 @@ async def test_request_source_if_duration_over_threshold_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx.test_httpx" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx/test_httpx.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "test_request_source_if_duration_over_threshold_async" ) @@ -1094,7 +1127,7 @@ def test_span_origin_sync(sentry_init, capture_items, httpx_mock): http_span = _get_http_client_span(items) - assert http_span["attributes"]["sentry.origin"] == "auto.http.httpx" + assert http_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.httpx" @pytest.mark.asyncio @@ -1117,7 +1150,7 @@ async def test_span_origin_async(sentry_init, capture_items, httpx_mock): http_span = _get_http_client_span(items) - assert http_span["attributes"]["sentry.origin"] == "auto.http.httpx" + assert http_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.httpx" @pytest.mark.parametrize( @@ -1164,15 +1197,15 @@ def test_http_url_attributes_sync( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["http.response.status_code"] == 200 - assert http_span["attributes"]["url.full"] == expected_url_full - assert http_span["attributes"]["url.fragment"] == "frag" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" if expected_query is not None: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query else: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] @pytest.mark.asyncio @@ -1220,15 +1253,15 @@ async def test_http_url_attributes_async( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["http.response.status_code"] == 200 - assert http_span["attributes"]["url.full"] == expected_url_full - assert http_span["attributes"]["url.fragment"] == "frag" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" if expected_query is not None: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query else: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] def test_http_url_attributes_no_query_or_fragment_sync( @@ -1252,11 +1285,11 @@ def test_http_url_attributes_no_query_or_fragment_sync( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["http.response.status_code"] == 200 - assert http_span["attributes"]["url.full"] == "http://example.com/" - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/" + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] @pytest.mark.asyncio @@ -1281,11 +1314,11 @@ async def test_http_url_attributes_no_query_or_fragment_async( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["http.response.status_code"] == 200 - assert http_span["attributes"]["url.full"] == "http://example.com/" - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/" + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] @pytest.mark.parametrize( @@ -1350,12 +1383,12 @@ def test_url_query_data_collection_sync( http_span = _get_http_client_span(items) - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full if expected_query is None: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] else: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.asyncio @@ -1421,12 +1454,12 @@ async def test_url_query_data_collection_async( http_span = _get_http_client_span(items) - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full if expected_query is None: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] else: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.parametrize( @@ -1580,9 +1613,9 @@ def test_omit_url_data_if_parsing_fails( http_span = _get_http_client_span(items) - assert "url.full" not in http_span["attributes"] - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FULL not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] crumb = event["breadcrumbs"]["values"][0] diff --git a/tests/integrations/httpx2/test_httpx2.py b/tests/integrations/httpx2/test_httpx2.py index 2c9b130a84..d3763c0f97 100644 --- a/tests/integrations/httpx2/test_httpx2.py +++ b/tests/integrations/httpx2/test_httpx2.py @@ -3,6 +3,7 @@ import httpx2 import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -15,7 +16,8 @@ def _get_http_client_span(items): return next( item.payload for item in items - if item.payload.get("attributes", {}).get("sentry.op") == OP.HTTP_CLIENT + if item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.HTTP_CLIENT ) @@ -645,10 +647,10 @@ def test_request_source_disabled_sync(sentry_init, capture_items, httpx2_mock): http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.asyncio @@ -673,10 +675,10 @@ async def test_request_source_disabled_async(sentry_init, capture_items, httpx2_ http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.parametrize("enable_http_request_source", [None, True]) @@ -709,10 +711,10 @@ def test_request_source_enabled_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] @pytest.mark.asyncio @@ -746,10 +748,10 @@ async def test_request_source_enabled_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] def test_request_source_sync(sentry_init, capture_items, httpx2_mock): @@ -773,25 +775,30 @@ def test_request_source_sync(sentry_init, capture_items, httpx2_mock): http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx2.test_httpx2" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx2/test_httpx2.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path - assert http_span["attributes"][SPANDATA.CODE_FUNCTION] == "test_request_source_sync" + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "test_request_source_sync" + ) @pytest.mark.asyncio @@ -816,26 +823,29 @@ async def test_request_source_async(sentry_init, capture_items, httpx2_mock): http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx2.test_httpx2" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx2/test_httpx2.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] == "test_request_source_async" + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "test_request_source_async" ) @@ -867,20 +877,31 @@ def test_request_source_with_module_in_search_path_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 - assert http_span["attributes"][SPANDATA.CODE_NAMESPACE] == "httpx2_helpers.helpers" - assert http_span["attributes"]["code.file.path"] == "httpx2_helpers/helpers.py" + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] + == "httpx2_helpers.helpers" + ) + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH] + == "httpx2_helpers/helpers.py" + ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path - assert http_span["attributes"][SPANDATA.CODE_FUNCTION] == "get_request_with_client" + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] + == "get_request_with_client" + ) @pytest.mark.asyncio @@ -912,21 +933,29 @@ async def test_request_source_with_module_in_search_path_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 - assert http_span["attributes"][SPANDATA.CODE_NAMESPACE] == "httpx2_helpers.helpers" - assert http_span["attributes"]["code.file.path"] == "httpx2_helpers/helpers.py" + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] + == "httpx2_helpers.helpers" + ) + assert ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH] + == "httpx2_helpers/helpers.py" + ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "async_get_request_with_client" ) @@ -955,10 +984,10 @@ def test_no_request_source_if_duration_too_short_sync( http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] @pytest.mark.asyncio @@ -986,10 +1015,10 @@ async def test_no_request_source_if_duration_too_short_async( http_span = _get_http_client_span(items) - assert "code.line.number" not in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE not in http_span["attributes"] - assert "code.file.path" not in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in http_span["attributes"] def test_request_source_if_duration_over_threshold_sync( @@ -1016,26 +1045,28 @@ def test_request_source_if_duration_over_threshold_sync( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx2.test_httpx2" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx2/test_httpx2.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "test_request_source_if_duration_over_threshold_sync" ) @@ -1065,26 +1096,28 @@ async def test_request_source_if_duration_over_threshold_async( http_span = _get_http_client_span(items) - assert "code.line.number" in http_span["attributes"] - assert SPANDATA.CODE_NAMESPACE in http_span["attributes"] - assert "code.file.path" in http_span["attributes"] - assert SPANDATA.CODE_FUNCTION in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in http_span["attributes"] + assert ATTRIBUTE_NAMES.CODE_FUNCTION in http_span["attributes"] - assert type(http_span["attributes"]["code.line.number"]) == int - assert http_span["attributes"]["code.line.number"] > 0 + assert type(http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER]) == int + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_LINE_NUMBER] > 0 assert ( - http_span["attributes"][SPANDATA.CODE_NAMESPACE] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_NAMESPACE] == "tests.integrations.httpx2.test_httpx2" ) - assert http_span["attributes"]["code.file.path"].endswith( + assert http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH].endswith( "tests/integrations/httpx2/test_httpx2.py" ) - is_relative_path = http_span["attributes"]["code.file.path"][0] != os.sep + is_relative_path = ( + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FILE_PATH][0] != os.sep + ) assert is_relative_path assert ( - http_span["attributes"][SPANDATA.CODE_FUNCTION] + http_span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION] == "test_request_source_if_duration_over_threshold_async" ) @@ -1108,7 +1141,7 @@ def test_span_origin_sync(sentry_init, capture_items, httpx2_mock): http_span = _get_http_client_span(items) - assert http_span["attributes"]["sentry.origin"] == "auto.http.httpx2" + assert http_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.httpx2" @pytest.mark.asyncio @@ -1131,7 +1164,7 @@ async def test_span_origin_async(sentry_init, capture_items, httpx2_mock): http_span = _get_http_client_span(items) - assert http_span["attributes"]["sentry.origin"] == "auto.http.httpx2" + assert http_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.httpx2" @pytest.mark.parametrize( @@ -1178,14 +1211,14 @@ def test_http_url_attributes_sync( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full if expected_query is not None: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query else: - assert "url.query" not in http_span["attributes"] - assert http_span["attributes"]["url.fragment"] == "frag" - assert http_span["attributes"]["http.response.status_code"] == 200 + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 @pytest.mark.asyncio @@ -1233,14 +1266,14 @@ async def test_http_url_attributes_async( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full if expected_query is not None: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query else: - assert "url.query" not in http_span["attributes"] - assert http_span["attributes"]["url.fragment"] == "frag" - assert http_span["attributes"]["http.response.status_code"] == 200 + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 def test_http_url_attributes_no_query_or_fragment_sync( @@ -1264,11 +1297,11 @@ def test_http_url_attributes_no_query_or_fragment_sync( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["url.full"] == "http://example.com/" - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] - assert http_span["attributes"]["http.response.status_code"] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/" + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 @pytest.mark.asyncio @@ -1293,11 +1326,11 @@ async def test_http_url_attributes_no_query_or_fragment_async( http_span = _get_http_client_span(items) - assert http_span["attributes"]["http.request.method"] == "GET" - assert http_span["attributes"]["url.full"] == "http://example.com/" - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] - assert http_span["attributes"]["http.response.status_code"] == 200 + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == "http://example.com/" + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] + assert http_span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 @pytest.mark.parametrize( @@ -1363,11 +1396,11 @@ def test_url_query_data_collection_sync( http_span = _get_http_client_span(items) if expected_query is None: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] else: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full @pytest.mark.asyncio @@ -1434,11 +1467,11 @@ async def test_url_query_data_collection_async( http_span = _get_http_client_span(items) if expected_query is None: - assert "url.query" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] else: - assert http_span["attributes"]["url.query"] == expected_query + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query - assert http_span["attributes"]["url.full"] == expected_url_full + assert http_span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == expected_url_full @pytest.mark.parametrize( @@ -1592,9 +1625,9 @@ def test_omit_url_data_if_parsing_fails( http_span = _get_http_client_span(items) - assert "url.full" not in http_span["attributes"] - assert "url.query" not in http_span["attributes"] - assert "url.fragment" not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FULL not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in http_span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in http_span["attributes"] crumb = event["breadcrumbs"]["values"][0] diff --git a/tests/integrations/pyreqwest/test_pyreqwest.py b/tests/integrations/pyreqwest/test_pyreqwest.py index 8c2fefed69..1604441e19 100644 --- a/tests/integrations/pyreqwest/test_pyreqwest.py +++ b/tests/integrations/pyreqwest/test_pyreqwest.py @@ -10,6 +10,7 @@ from pyreqwest.client import ClientBuilder, SyncClientBuilder from pyreqwest.simple.request import pyreqwest_get as async_pyreqwest_get from pyreqwest.simple.sync_request import pyreqwest_get as sync_pyreqwest_get +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -78,18 +79,18 @@ def test_sync_client_spans(sentry_init, capture_items, server_port): spans = [item.payload for item in items] assert len(spans) == 2 span = spans[0] - assert span["attributes"]["sentry.op"] == "http.client" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span["name"] == f"GET http://localhost:{server_port}/hello" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 200 - assert span["attributes"]["sentry.origin"] == "auto.http.pyreqwest" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyreqwest" assert ( - span["attributes"]["url.full"] + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == f"http://localhost:{server_port}/hello?q=test#frag" ) - assert span["attributes"][SPANDATA.URL_QUERY] == "q=test" - assert span["attributes"][SPANDATA.URL_FRAGMENT] == "frag" + assert span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "q=test" + assert span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" @pytest.mark.asyncio @@ -111,13 +112,13 @@ async def test_async_client_spans(sentry_init, capture_items, server_port): spans = [item.payload for item in items] assert len(spans) == 2 span = spans[0] - assert span["attributes"]["sentry.op"] == "http.client" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span["name"] == f"GET {url}" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 200 - assert span["attributes"]["sentry.origin"] == "auto.http.pyreqwest" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyreqwest" - assert span["attributes"]["url.full"] == url + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == url def test_sync_simple_request_spans(sentry_init, capture_items, server_port): @@ -137,12 +138,12 @@ def test_sync_simple_request_spans(sentry_init, capture_items, server_port): spans = [item.payload for item in items] assert len(spans) == 2 span = spans[0] - assert span["attributes"]["sentry.op"] == "http.client" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span["name"] == f"GET {url}" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 200 - assert span["attributes"]["sentry.origin"] == "auto.http.pyreqwest" - assert span["attributes"]["url.full"] == url + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyreqwest" + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == url @pytest.mark.asyncio @@ -163,13 +164,13 @@ async def test_async_simple_request_spans(sentry_init, capture_items, server_por spans = [item.payload for item in items] assert len(spans) == 2 span = spans[0] - assert span["attributes"]["sentry.op"] == "http.client" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span["name"] == f"GET {url}" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 200 - assert span["attributes"]["sentry.origin"] == "auto.http.pyreqwest" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyreqwest" - assert span["attributes"]["url.full"] == url + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == url def test_span_origin( @@ -191,7 +192,9 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.origin"] == "auto.http.pyreqwest" + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.http.pyreqwest" + ) def test_outgoing_trace_headers( @@ -221,7 +224,9 @@ def test_outgoing_trace_headers( sentry_sdk.flush() spans = [item.payload for item in items] http_span = next( - 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 "sentry-trace" in headers @@ -249,7 +254,7 @@ def test_outgoing_trace_headers_append_to_baggage( with sentry_sdk.start_span( name="/interactions/other-dogs/new-dog", attributes={ - "sentry.op": "greeting.sniff", + ATTRIBUTE_NAMES.SENTRY_OP: "greeting.sniff", }, ): client = SyncClientBuilder().build() @@ -261,7 +266,9 @@ def test_outgoing_trace_headers_append_to_baggage( sentry_sdk.flush() spans = [item.payload for item in items] http_span = next( - 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 "baggage" in headers @@ -339,11 +346,11 @@ def test_omit_url_data_if_parsing_fails( span = spans[0] assert span["name"] == "GET [Filtered]" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.HTTP_STATUS_CODE] == 200 - assert "url.full" not in span["attributes"] - assert SPANDATA.URL_QUERY not in span["attributes"] - assert SPANDATA.URL_FRAGMENT not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_RESPONSE_STATUS_CODE] == 200 + assert ATTRIBUTE_NAMES.URL_FULL not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in span["attributes"] def test_request_source_disabled( @@ -370,10 +377,10 @@ def test_request_source_disabled( span = spans[0] data = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER not in data - assert SPANDATA.CODE_NAMESPACE not in data - assert SPANDATA.CODE_FILE_PATH not in data - assert SPANDATA.CODE_FUNCTION not in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in data @pytest.mark.parametrize("enable_http_request_source", [None, True]) @@ -406,10 +413,10 @@ def test_request_source_enabled( span = spans[0] data = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in data - assert SPANDATA.CODE_NAMESPACE in data - assert SPANDATA.CODE_FILE_PATH in data - assert SPANDATA.CODE_FUNCTION in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION in data def test_request_source( @@ -436,20 +443,20 @@ def test_request_source( span = spans[0] data = span.get("attributes", {}) - assert type(data.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert data.get(SPANDATA.CODE_LINE_NUMBER) > 0 + assert type(data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 assert ( - data.get(SPANDATA.CODE_NAMESPACE) + data.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "tests.integrations.pyreqwest.test_pyreqwest" ) - assert data.get(SPANDATA.CODE_FILE_PATH).endswith( + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/pyreqwest/test_pyreqwest.py" ) - is_relative_path = data.get(SPANDATA.CODE_FILE_PATH)[0] != os.sep + is_relative_path = data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep assert is_relative_path - assert data.get(SPANDATA.CODE_FUNCTION) == "test_request_source" + assert data.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "test_request_source" def test_request_source_with_module_in_search_path( @@ -478,15 +485,15 @@ def test_request_source_with_module_in_search_path( span = spans[0] data = span.get("attributes", {}) - assert type(data.get(SPANDATA.CODE_LINE_NUMBER)) == int - assert data.get(SPANDATA.CODE_LINE_NUMBER) > 0 - assert data.get(SPANDATA.CODE_NAMESPACE) == "pyreqwest_helpers.helpers" - assert data.get(SPANDATA.CODE_FILE_PATH) == "pyreqwest_helpers/helpers.py" + assert type(data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 + assert data.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "pyreqwest_helpers.helpers" + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH) == "pyreqwest_helpers/helpers.py" - is_relative_path = data.get(SPANDATA.CODE_FILE_PATH)[0] != os.sep + is_relative_path = data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep assert is_relative_path - assert data.get(SPANDATA.CODE_FUNCTION) == "get_request_with_client" + assert data.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "get_request_with_client" def test_no_request_source_if_duration_too_short( @@ -529,10 +536,10 @@ def fake_start_span(*args, **kwargs): span = spans[0] data = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER not in data - assert SPANDATA.CODE_NAMESPACE not in data - assert SPANDATA.CODE_FILE_PATH not in data - assert SPANDATA.CODE_FUNCTION not in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER not in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH not in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in data def test_request_source_if_duration_over_threshold( @@ -575,10 +582,10 @@ def fake_start_span(*args, **kwargs): span = spans[0] data = span.get("attributes", {}) - assert SPANDATA.CODE_LINE_NUMBER in data - assert SPANDATA.CODE_NAMESPACE in data - assert SPANDATA.CODE_FILE_PATH in data - assert SPANDATA.CODE_FUNCTION in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION in data def test_crumb_capture( @@ -769,9 +776,9 @@ def test_url_query_data_collection_sync( span = [item.payload for item in items][0] if expected_query is None: - assert SPANDATA.URL_QUERY not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] else: - assert span["attributes"][SPANDATA.URL_QUERY] == expected_query + assert span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.asyncio @@ -832,9 +839,9 @@ async def test_url_query_data_collection_async( span = [item.payload for item in items][0] if expected_query is None: - assert SPANDATA.URL_QUERY not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] else: - assert span["attributes"][SPANDATA.URL_QUERY] == expected_query + assert span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.parametrize( @@ -879,7 +886,7 @@ def test_url_full_reassembly_sync( span = [item.payload for item in items][0] - assert span["attributes"][SPANDATA.URL_FULL] == base_url + expected_suffix + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == base_url + expected_suffix @pytest.mark.asyncio @@ -925,7 +932,7 @@ async def test_url_full_reassembly_async( span = [item.payload for item in items][0] - assert span["attributes"][SPANDATA.URL_FULL] == base_url + expected_suffix + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == base_url + expected_suffix def test_url_query_params_off_keeps_bare_url_sync( @@ -951,9 +958,9 @@ def test_url_query_params_off_keeps_bare_url_sync( span = [item.payload for item in items][0] - assert SPANDATA.URL_QUERY not in span["attributes"] - assert span["attributes"][SPANDATA.URL_FULL] == base_url + "#frag" - assert span["attributes"][SPANDATA.URL_FRAGMENT] == "frag" + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == base_url + "#frag" + assert span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" @pytest.mark.asyncio @@ -980,9 +987,9 @@ async def test_url_query_params_off_keeps_bare_url_async( span = [item.payload for item in items][0] - assert SPANDATA.URL_QUERY not in span["attributes"] - assert span["attributes"][SPANDATA.URL_FULL] == base_url + "#frag" - assert span["attributes"][SPANDATA.URL_FRAGMENT] == "frag" + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == base_url + "#frag" + assert span["attributes"][ATTRIBUTE_NAMES.URL_FRAGMENT] == "frag" @pytest.mark.parametrize( @@ -1137,9 +1144,9 @@ def test_omit_url_data_if_parsing_fails_data_collection( span = [item.payload for item in items][0] - assert SPANDATA.URL_FULL not in span["attributes"] - assert SPANDATA.URL_QUERY not in span["attributes"] - assert SPANDATA.URL_FRAGMENT not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_FULL not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_FRAGMENT not in span["attributes"] crumb = event["breadcrumbs"]["values"][0] diff --git a/tests/integrations/socket/test_socket.py b/tests/integrations/socket/test_socket.py index e3dc659fb4..67ab1a1760 100644 --- a/tests/integrations/socket/test_socket.py +++ b/tests/integrations/socket/test_socket.py @@ -1,5 +1,7 @@ import socket +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.integrations.socket import SocketIntegration from tests.conftest import create_mock_http_server @@ -23,11 +25,11 @@ def test_getaddrinfo_trace(sentry_init, capture_items): spans = [item.payload for item in items] dns_span, _root = spans - assert dns_span["attributes"]["sentry.op"] == "socket.dns" - assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "socket.dns" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.socket.socket" assert dns_span["name"] == f"localhost:{PORT}" - assert dns_span["attributes"]["server.address"] == "localhost" - assert dns_span["attributes"]["server.port"] == PORT + assert dns_span["attributes"][ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SERVER_PORT] == PORT def test_create_connection_trace(sentry_init, capture_items): @@ -50,15 +52,15 @@ def test_create_connection_trace(sentry_init, capture_items): # spans finish in order: dns (inner) ends first, connect ends, then root dns_span, connect_span, _root = spans - assert connect_span["attributes"]["sentry.op"] == "socket.connection" + assert connect_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "socket.connection" assert connect_span["name"] == f"localhost:{PORT}" - assert connect_span["attributes"]["server.address"] == "localhost" - assert connect_span["attributes"]["server.port"] == PORT + assert connect_span["attributes"][ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert connect_span["attributes"][ATTRIBUTE_NAMES.SERVER_PORT] == PORT - assert dns_span["attributes"]["sentry.op"] == "socket.dns" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "socket.dns" assert dns_span["name"] == f"localhost:{PORT}" - assert dns_span["attributes"]["server.address"] == "localhost" - assert dns_span["attributes"]["server.port"] == PORT + assert dns_span["attributes"][ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SERVER_PORT] == PORT def test_span_origin(sentry_init, capture_items): @@ -77,8 +79,11 @@ def test_span_origin(sentry_init, capture_items): spans = [item.payload for item in items] dns_span, connect_span, _root = spans - assert connect_span["attributes"]["sentry.op"] == "socket.connection" - assert connect_span["attributes"]["sentry.origin"] == "auto.socket.socket" + assert connect_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "socket.connection" + assert ( + connect_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.socket.socket" + ) - assert dns_span["attributes"]["sentry.op"] == "socket.dns" - assert dns_span["attributes"]["sentry.origin"] == "auto.socket.socket" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "socket.dns" + assert dns_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.socket.socket" diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index 9c6ef717c0..7f9aaca311 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -11,6 +11,7 @@ from urllib.request import urlopen import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -277,7 +278,7 @@ def getresponse(self, *args, **kwargs): with sentry_sdk.start_span( name="/interactions/other-dogs/new-dog", attributes={ - "sentry.op": "greeting.sniff", + ATTRIBUTE_NAMES.SENTRY_OP: "greeting.sniff", }, ): connection = HTTPSConnectionRecordingRequestHeaders("localhost", port=PORT) @@ -531,7 +532,7 @@ def getresponse(self, *args, **kwargs): with sentry_sdk.start_span( name="/interactions/other-dogs/new-dog", attributes={ - "sentry.op": "greeting.sniff", + ATTRIBUTE_NAMES.SENTRY_OP: "greeting.sniff", }, ): connection = HTTPSConnectionRecordingRequestHeaders(host) @@ -568,10 +569,10 @@ def test_request_source_disabled(sentry_init, capture_items): attributes = span["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 @pytest.mark.parametrize("enable_http_request_source", [None, True]) @@ -599,10 +600,10 @@ def test_request_source_enabled(sentry_init, capture_items, enable_http_request_ attributes = span["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 def test_request_source(sentry_init, capture_items): @@ -626,25 +627,25 @@ def test_request_source(sentry_init, capture_items): attributes = span["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.stdlib.test_httplib" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/stdlib/test_httplib.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) == "test_request_source" + assert attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "test_request_source" def test_request_source_with_module_in_search_path(sentry_init, capture_items): @@ -672,20 +673,24 @@ def test_request_source_with_module_in_search_path(sentry_init, capture_items): attributes = span["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) == "httplib_helpers.helpers" - assert attributes.get(SPANDATA.CODE_FILE_PATH) == "httplib_helpers/helpers.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) == "httplib_helpers.helpers" + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH) == "httplib_helpers/helpers.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) == "get_request_with_connection" + assert ( + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "get_request_with_connection" + ) def test_no_request_source_if_duration_too_short(sentry_init, capture_items): @@ -720,10 +725,10 @@ def add_http_request_source_with_pinned_timestamps(span): attributes = span["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 def test_request_source_if_duration_over_threshold(sentry_init, capture_items): @@ -760,26 +765,26 @@ def add_http_request_source_with_pinned_timestamps(span): attributes = span["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.stdlib.test_httplib" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/stdlib/test_httplib.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) + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "add_http_request_source_with_pinned_timestamps" ) @@ -798,10 +803,13 @@ def test_span_origin(sentry_init, capture_items): sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[1]["attributes"]["sentry.origin"] == "manual" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" - assert spans[0]["attributes"]["sentry.op"] == "http.client" - assert spans[0]["attributes"]["sentry.origin"] == "auto.http.stdlib.httplib" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.http.stdlib.httplib" + ) def test_http_timeout(monkeypatch, sentry_init, capture_items): @@ -817,7 +825,7 @@ def test_http_timeout(monkeypatch, sentry_init, capture_items): with sentry_sdk.start_span( name="name", attributes={ - "sentry.op": "op", + ATTRIBUTE_NAMES.SENTRY_OP: "op", }, ): conn = HTTPConnection("localhost", port=PORT) @@ -828,7 +836,7 @@ def test_http_timeout(monkeypatch, sentry_init, capture_items): spans = [item.payload for item in items] assert len(spans) == 2 span = spans[0] - assert span["attributes"]["sentry.op"] == "http.client" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "http.client" assert span["name"] == f"GET http://localhost:{PORT}/bla" @@ -850,20 +858,21 @@ def test_proxy_http_tunnel(sentry_init, capture_items, tunnel_port): (span,) = ( span for span in spans - if span["attributes"].get("sentry.origin") == "auto.http.stdlib.httplib" + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.stdlib.httplib" ) port_modifier = f":{tunnel_port}" if tunnel_port else "" assert span["name"] == f"GET http://api.example.com{port_modifier}/foo" - assert span["attributes"][SPANDATA.HTTP_REQUEST_METHOD] == "GET" - assert span["attributes"][SPANDATA.NETWORK_PEER_ADDRESS] == "localhost" - assert span["attributes"][SPANDATA.NETWORK_PEER_PORT] == PROXY_PORT + assert span["attributes"][ATTRIBUTE_NAMES.HTTP_REQUEST_METHOD] == "GET" + assert span["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == "localhost" + assert span["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == PROXY_PORT assert ( - span["attributes"][SPANDATA.URL_FULL] + span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == f"http://api.example.com{port_modifier}/foo?bar=1" ) - assert span["attributes"][SPANDATA.URL_QUERY] == "bar=1" + assert span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == "bar=1" def test_chunked_response_span_covers_body_read(sentry_init, capture_items): @@ -940,13 +949,14 @@ def test_url_query_data_collection( (span,) = ( item.payload for item in items - if item.payload["attributes"].get("sentry.origin") == "auto.http.stdlib.httplib" + if item.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.stdlib.httplib" ) if expected_query is None: - assert SPANDATA.URL_QUERY not in span["attributes"] + assert ATTRIBUTE_NAMES.URL_QUERY not in span["attributes"] else: - assert span["attributes"][SPANDATA.URL_QUERY] == expected_query + assert span["attributes"][ATTRIBUTE_NAMES.URL_QUERY] == expected_query @pytest.mark.parametrize( @@ -987,11 +997,12 @@ def test_url_full_reassembly( (span,) = ( item.payload for item in items - if item.payload["attributes"].get("sentry.origin") == "auto.http.stdlib.httplib" + if item.payload["attributes"].get(ATTRIBUTE_NAMES.SENTRY_ORIGIN) + == "auto.http.stdlib.httplib" ) base_url = "http://localhost:{}/some/random/url".format(PORT) - assert span["attributes"][SPANDATA.URL_FULL] == base_url + expected_suffix + assert span["attributes"][ATTRIBUTE_NAMES.URL_FULL] == base_url + expected_suffix @pytest.mark.parametrize( diff --git a/tests/integrations/stdlib/test_subprocess.py b/tests/integrations/stdlib/test_subprocess.py index c634af9b72..89f17e1ec4 100644 --- a/tests/integrations/stdlib/test_subprocess.py +++ b/tests/integrations/stdlib/test_subprocess.py @@ -5,10 +5,10 @@ from typing import Any, Mapping import pytest +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.integrations.stdlib import StdlibIntegration from tests.conftest import ApproxDict @@ -132,9 +132,9 @@ def test_subprocess_basic( ) = (item.payload for item in items if item.type == "span") assert ( - subprocess_init_span["attributes"]["sentry.op"], - subprocess_wait_span["attributes"]["sentry.op"], - subprocess_communicate_span["attributes"]["sentry.op"], + subprocess_init_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP], + subprocess_wait_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP], + subprocess_communicate_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP], ) == ("subprocess", "subprocess.wait", "subprocess.communicate") # span hierarchy @@ -149,9 +149,9 @@ def test_subprocess_basic( ) assert ( - subprocess_init_span["attributes"][SPANDATA.PROCESS_PID] - == subprocess_wait_span["attributes"][SPANDATA.PROCESS_PID] - == subprocess_communicate_span["attributes"][SPANDATA.PROCESS_PID] + subprocess_init_span["attributes"][ATTRIBUTE_NAMES.PROCESS_PID] + == subprocess_wait_span["attributes"][ATTRIBUTE_NAMES.PROCESS_PID] + == subprocess_communicate_span["attributes"][ATTRIBUTE_NAMES.PROCESS_PID] ) # data of init span @@ -221,19 +221,22 @@ def test_subprocess_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[3]["attributes"]["sentry.origin"] == "manual" + assert spans[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" - assert spans[0]["attributes"]["sentry.op"] == "subprocess" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "subprocess" assert ( - spans[0]["attributes"]["sentry.origin"] == "auto.subprocess.stdlib.subprocess" + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.subprocess.stdlib.subprocess" ) - assert spans[1]["attributes"]["sentry.op"] == "subprocess.wait" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "subprocess.wait" assert ( - spans[1]["attributes"]["sentry.origin"] == "auto.subprocess.stdlib.subprocess" + spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.subprocess.stdlib.subprocess" ) - assert spans[2]["attributes"]["sentry.op"] == "subprocess.communicate" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "subprocess.communicate" assert ( - spans[2]["attributes"]["sentry.origin"] == "auto.subprocess.stdlib.subprocess" + spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.subprocess.stdlib.subprocess" )