diff --git a/sentry_sdk/integrations/aiomysql.py b/sentry_sdk/integrations/aiomysql.py index 4973a0488b..c159bfb718 100644 --- a/sentry_sdk/integrations/aiomysql.py +++ b/sentry_sdk/integrations/aiomysql.py @@ -2,6 +2,8 @@ from typing import Any, Awaitable, Callable, TypeVar +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 @@ -181,8 +183,8 @@ async def _inner(self: "Connection") -> T: return await f(self) span_attributes: dict[str, Any] = { - "sentry.op": OP.DB, - "sentry.origin": AioMySQLIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioMySQLIntegration.origin, } | breadcrumb_data with sentry_sdk.start_span(name="connect", attributes=span_attributes): @@ -193,24 +195,24 @@ async def _inner(self: "Connection") -> T: def _get_connect_data(conn: Any, *, use_streaming_keys: bool = False) -> dict[str, Any]: if use_streaming_keys: - db_system = SPANDATA.DB_SYSTEM_NAME - db_name = SPANDATA.DB_NAMESPACE + db_system = ATTRIBUTE_NAMES.DB_SYSTEM_NAME + db_name = ATTRIBUTE_NAMES.DB_NAMESPACE else: db_system = SPANDATA.DB_SYSTEM db_name = SPANDATA.DB_NAME data: dict[str, Any] = { db_system: "mysql", - SPANDATA.DB_DRIVER_NAME: "aiomysql", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "aiomysql", } host = getattr(conn, "host", None) if host is not None: - data[SPANDATA.SERVER_ADDRESS] = host + data[ATTRIBUTE_NAMES.SERVER_ADDRESS] = host port = getattr(conn, "port", None) if port is not None: - data[SPANDATA.SERVER_PORT] = port + data[ATTRIBUTE_NAMES.SERVER_PORT] = port database = getattr(conn, "db", None) if database is not None: @@ -218,28 +220,28 @@ def _get_connect_data(conn: Any, *, use_streaming_keys: bool = False) -> dict[st user = getattr(conn, "user", None) if user is not None: - data[SPANDATA.DB_USER] = user + data[ATTRIBUTE_NAMES.DB_USER] = user return data def _set_db_data(span: Any, conn: Any) -> None: """Set database-related span data from connection object.""" - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "mysql") - span.set_attribute(SPANDATA.DB_DRIVER_NAME, "aiomysql") + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "mysql") + span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "aiomysql") host = getattr(conn, "host", None) if host is not None: - span.set_attribute(SPANDATA.SERVER_ADDRESS, host) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, host) port = getattr(conn, "port", None) if port is not None: - span.set_attribute(SPANDATA.SERVER_PORT, port) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, port) database = getattr(conn, "db", None) if database is not None: - span.set_attribute(SPANDATA.DB_NAMESPACE, database) + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, database) user = getattr(conn, "user", None) if user is not None: - span.set_attribute(SPANDATA.DB_USER, user) + span.set_attribute(ATTRIBUTE_NAMES.DB_USER, user) diff --git a/sentry_sdk/integrations/asyncpg.py b/sentry_sdk/integrations/asyncpg.py index f54b22816b..a75f394ebd 100644 --- a/sentry_sdk/integrations/asyncpg.py +++ b/sentry_sdk/integrations/asyncpg.py @@ -4,8 +4,10 @@ import re from typing import Any, Awaitable, Callable, Iterator, TypeVar +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, Integration, _check_minimum_version from sentry_sdk.traces import Span from sentry_sdk.tracing_utils import ( @@ -202,17 +204,17 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T": addr = kwargs.get("addr") span_attributes = { - "sentry.op": OP.DB, - "sentry.origin": AsyncPGIntegration.origin, - SPANDATA.DB_SYSTEM_NAME: "postgresql", - SPANDATA.DB_USER: user, - SPANDATA.DB_NAMESPACE: database, - SPANDATA.DB_DRIVER_NAME: "asyncpg", + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: AsyncPGIntegration.origin, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "postgresql", + ATTRIBUTE_NAMES.DB_USER: user, + ATTRIBUTE_NAMES.DB_NAMESPACE: database, + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "asyncpg", } if addr: try: - span_attributes[SPANDATA.SERVER_ADDRESS] = addr[0] - span_attributes[SPANDATA.SERVER_PORT] = addr[1] + span_attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] = addr[0] + span_attributes[ATTRIBUTE_NAMES.SERVER_PORT] = addr[1] except IndexError: pass @@ -235,17 +237,17 @@ def _set_db_data(span: "Span", conn: "Any") -> None: database = conn._params.database user = conn._params.user - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "postgresql") - span.set_attribute(SPANDATA.DB_DRIVER_NAME, "asyncpg") + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "postgresql") + span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "asyncpg") if addr: try: - span.set_attribute(SPANDATA.SERVER_ADDRESS, addr[0]) - span.set_attribute(SPANDATA.SERVER_PORT, addr[1]) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, addr[0]) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, addr[1]) except IndexError: pass if database: - span.set_attribute(SPANDATA.DB_NAMESPACE, database) + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, database) if user: - span.set_attribute(SPANDATA.DB_USER, user) + span.set_attribute(ATTRIBUTE_NAMES.DB_USER, user) diff --git a/sentry_sdk/integrations/clickhouse_driver.py b/sentry_sdk/integrations/clickhouse_driver.py index 457ec3d626..5ee324288d 100644 --- a/sentry_sdk/integrations/clickhouse_driver.py +++ b/sentry_sdk/integrations/clickhouse_driver.py @@ -1,6 +1,8 @@ import functools from typing import TYPE_CHECKING, TypeVar +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 @@ -82,9 +84,9 @@ def _inner(*args: "P.args", **kwargs: "P.kwargs") -> "T": span = sentry_sdk.start_span( name=query, attributes={ - "sentry.op": OP.DB, - "sentry.origin": ClickhouseDriverIntegration.origin, - SPANDATA.DB_QUERY_TEXT: str(query), + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ClickhouseDriverIntegration.origin, + ATTRIBUTE_NAMES.DB_QUERY_TEXT: str(query), }, ) @@ -164,9 +166,9 @@ def _set_db_data(span: "Optional[Span]", connection: "Connection") -> None: if span is None: return - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "clickhouse") - span.set_attribute(SPANDATA.DB_NAMESPACE, connection.database) - span.set_attribute(SPANDATA.DB_DRIVER_NAME, "clickhouse-driver") - span.set_attribute(SPANDATA.SERVER_ADDRESS, connection.host) - span.set_attribute(SPANDATA.SERVER_PORT, connection.port) - span.set_attribute(SPANDATA.DB_USER, connection.user) + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "clickhouse") + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, connection.database) + span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "clickhouse-driver") + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, connection.host) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, connection.port) + span.set_attribute(ATTRIBUTE_NAMES.DB_USER, connection.user) diff --git a/sentry_sdk/integrations/pymongo.py b/sentry_sdk/integrations/pymongo.py index 1047e9caa3..bd44d100b5 100644 --- a/sentry_sdk/integrations/pymongo.py +++ b/sentry_sdk/integrations/pymongo.py @@ -1,8 +1,10 @@ import copy import json +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, Integration, _check_minimum_version from sentry_sdk.traces import SpanStatus from sentry_sdk.utils import ( @@ -92,21 +94,21 @@ def _strip_pii(command: "Dict[str, Any]") -> "Dict[str, Any]": def _get_db_data(event: "Any") -> "Dict[str, Any]": data = {} - data[SPANDATA.DB_DRIVER_NAME] = "pymongo" + data[ATTRIBUTE_NAMES.DB_DRIVER_NAME] = "pymongo" db_name = event.database_name server_address = event.connection_id[0] if server_address is not None: - data[SPANDATA.SERVER_ADDRESS] = server_address + data[ATTRIBUTE_NAMES.SERVER_ADDRESS] = server_address server_port = event.connection_id[1] if server_port is not None: - data[SPANDATA.SERVER_PORT] = server_port + data[ATTRIBUTE_NAMES.SERVER_PORT] = server_port - data["db.system.name"] = "mongodb" + data[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] = "mongodb" if db_name is not None: - data["db.namespace"] = db_name + data[ATTRIBUTE_NAMES.DB_NAMESPACE] = db_name return data @@ -145,11 +147,11 @@ def started(self, event: "CommandStartedEvent") -> None: query = json.dumps(command, default=str) data = { - "db.operation.name": operation_name, - "db.collection.name": collection_name, - SPANDATA.DB_QUERY_TEXT: query, - "sentry.op": OP.DB, - "sentry.origin": PyMongoIntegration.origin, + ATTRIBUTE_NAMES.DB_OPERATION_NAME: operation_name, + ATTRIBUTE_NAMES.DB_COLLECTION_NAME: collection_name, + ATTRIBUTE_NAMES.DB_QUERY_TEXT: query, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: PyMongoIntegration.origin, **db_data, } diff --git a/sentry_sdk/integrations/redis/_async_common.py b/sentry_sdk/integrations/redis/_async_common.py index 03e82cd61b..dccf9f9bcd 100644 --- a/sentry_sdk/integrations/redis/_async_common.py +++ b/sentry_sdk/integrations/redis/_async_common.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.redis.consts import ( SPAN_ORIGIN, ) @@ -58,8 +60,8 @@ async def _sentry_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": span = sentry_sdk.start_span( name="redis.pipeline.execute", attributes={ - "sentry.origin": SPAN_ORIGIN, - "sentry.op": OP.DB_QUERY, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB_QUERY, }, ) @@ -119,7 +121,7 @@ async def _sentry_execute_command( additional_cache_span_attributes = {} with capture_internal_exceptions(): - additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = ( + additional_cache_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = ( _get_safe_command(name, args) ) @@ -128,23 +130,23 @@ async def _sentry_execute_command( cache_span = sentry_sdk.start_span( name=cache_properties["description"], attributes={ - "sentry.op": cache_properties["op"], - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: cache_properties["op"], + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, **additional_cache_span_attributes, }, ) additional_db_span_attributes = {} with capture_internal_exceptions(): - additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command( - name, args + additional_db_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = ( + _get_safe_command(name, args) ) db_span = sentry_sdk.start_span( name=db_properties["description"], attributes={ - "sentry.op": db_properties["op"], - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: db_properties["op"], + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, **additional_db_span_attributes, }, ) diff --git a/sentry_sdk/integrations/redis/_sync_common.py b/sentry_sdk/integrations/redis/_sync_common.py index b5e5ae9463..9bdf05cf85 100644 --- a/sentry_sdk/integrations/redis/_sync_common.py +++ b/sentry_sdk/integrations/redis/_sync_common.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.redis.consts import ( SPAN_ORIGIN, ) @@ -55,8 +57,8 @@ def sentry_patched_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any": span = sentry_sdk.start_span( name="redis.pipeline.execute", attributes={ - "sentry.origin": SPAN_ORIGIN, - "sentry.op": OP.DB_QUERY, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: OP.DB_QUERY, }, ) @@ -120,7 +122,7 @@ def sentry_patched_execute_command( additional_cache_span_attributes = {} with capture_internal_exceptions(): - additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = ( + additional_cache_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = ( _get_safe_command(name, args) ) @@ -129,23 +131,23 @@ def sentry_patched_execute_command( cache_span = sentry_sdk.start_span( name=cache_properties["description"], attributes={ - "sentry.op": cache_properties["op"], - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: cache_properties["op"], + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, **additional_cache_span_attributes, }, ) additional_db_span_attributes = {} with capture_internal_exceptions(): - additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command( - name, args + additional_db_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = ( + _get_safe_command(name, args) ) db_span = sentry_sdk.start_span( name=db_properties["description"], attributes={ - "sentry.op": db_properties["op"], - "sentry.origin": SPAN_ORIGIN, + ATTRIBUTE_NAMES.SENTRY_OP: db_properties["op"], + ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN, **additional_db_span_attributes, }, ) diff --git a/sentry_sdk/integrations/redis/modules/caches.py b/sentry_sdk/integrations/redis/modules/caches.py index 134c9983f9..80ddc26f1d 100644 --- a/sentry_sdk/integrations/redis/modules/caches.py +++ b/sentry_sdk/integrations/redis/modules/caches.py @@ -2,7 +2,9 @@ Code used for the Caches module in Sentry """ -from sentry_sdk.consts import OP, SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis.utils import _get_safe_key, _key_as_string from sentry_sdk.utils import capture_internal_exceptions @@ -83,19 +85,19 @@ def _set_cache_data( return_value: "Optional[Any]", ) -> None: with capture_internal_exceptions(): - span.set_attribute(SPANDATA.CACHE_KEY, properties["key"]) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_KEY, properties["key"]) if properties["redis_command"] in GET_COMMANDS: if return_value is not None: - span.set_attribute(SPANDATA.CACHE_HIT, True) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, True) size = ( len(str(return_value).encode("utf-8")) if not isinstance(return_value, bytes) else len(return_value) ) - span.set_attribute(SPANDATA.CACHE_ITEM_SIZE, size) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_ITEM_SIZE, size) else: - span.set_attribute(SPANDATA.CACHE_HIT, False) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_HIT, False) elif properties["redis_command"] in SET_COMMANDS: if properties["value"] is not None: @@ -104,7 +106,7 @@ def _set_cache_data( if not isinstance(properties["value"], bytes) else len(properties["value"]) ) - span.set_attribute(SPANDATA.CACHE_ITEM_SIZE, size) + span.set_attribute(ATTRIBUTE_NAMES.CACHE_ITEM_SIZE, size) try: connection_params = redis_client.connection_pool.connection_kwargs @@ -119,8 +121,8 @@ def _set_cache_data( host = connection_params.get("host") if host is not None: - span.set_attribute(SPANDATA.NETWORK_PEER_ADDRESS, host) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS, host) port = connection_params.get("port") if port is not None: - span.set_attribute(SPANDATA.NETWORK_PEER_PORT, port) + span.set_attribute(ATTRIBUTE_NAMES.NETWORK_PEER_PORT, port) diff --git a/sentry_sdk/integrations/redis/modules/queries.py b/sentry_sdk/integrations/redis/modules/queries.py index 910c510b2c..af9aa39205 100644 --- a/sentry_sdk/integrations/redis/modules/queries.py +++ b/sentry_sdk/integrations/redis/modules/queries.py @@ -4,7 +4,9 @@ from typing import TYPE_CHECKING -from sentry_sdk.consts import OP, SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis.utils import _get_safe_command from sentry_sdk.utils import capture_internal_exceptions @@ -46,17 +48,17 @@ def _set_db_data_on_span(span: "Span", connection_params: "dict[str, Any]") -> N host = connection_params.get("host") port = connection_params.get("port") - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "redis") - span.set_attribute(SPANDATA.DB_DRIVER_NAME, "redis-py") + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "redis") + span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "redis-py") if db is not None: - span.set_attribute(SPANDATA.DB_NAMESPACE, str(db)) + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, str(db)) if host is not None: - span.set_attribute(SPANDATA.SERVER_ADDRESS, host) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, host) if port is not None: - span.set_attribute(SPANDATA.SERVER_PORT, port) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, port) def _set_db_data(span: "Span", redis_instance: "Redis[Any]") -> None: diff --git a/sentry_sdk/integrations/redis/utils.py b/sentry_sdk/integrations/redis/utils.py index a2be8a53e8..e733ee91cd 100644 --- a/sentry_sdk/integrations/redis/utils.py +++ b/sentry_sdk/integrations/redis/utils.py @@ -1,7 +1,8 @@ from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations.redis.consts import ( _COMMANDS_INCLUDING_SENSITIVE_DATA, _MAX_NUM_ARGS, @@ -105,11 +106,11 @@ def _parse_rediscluster_command(command: "Any") -> "Sequence[Any]": def _set_client_data(span: "Span", is_cluster: bool, name: str, *args: "Any") -> None: if name: - span.set_attribute(SPANDATA.DB_OPERATION_NAME, name) + span.set_attribute(ATTRIBUTE_NAMES.DB_OPERATION_NAME, name) key = _extract_key(name, args) if key is not None: - span.set_attribute("db.redis.key", key) + span.set_attribute(ATTRIBUTE_NAMES.DB_REDIS_KEY, key) def _extract_key(name: str, args: "Any") -> "Optional[str]": diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 2e13302f99..52314b5465 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -1,4 +1,5 @@ -from sentry_sdk.consts import SPANDATA +from sentry_conventions.attributes import ATTRIBUTE_NAMES + from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.traces import Span, SpanStatus from sentry_sdk.tracing_utils import ( @@ -148,12 +149,12 @@ def _set_db_data(span: "Span", conn: "Any") -> None: db_system = _get_db_system(conn.engine.name) if db_system is not None: - span.set_attribute(SPANDATA.DB_SYSTEM_NAME, db_system) + span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, db_system) try: driver = conn.dialect.driver if driver: - span.set_attribute(SPANDATA.DB_DRIVER_NAME, driver) + span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, driver) except Exception: pass @@ -162,12 +163,12 @@ def _set_db_data(span: "Span", conn: "Any") -> None: db_name = conn.engine.url.database if db_name is not None: - span.set_attribute(SPANDATA.DB_NAMESPACE, db_name) + span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, db_name) server_address = conn.engine.url.host if server_address is not None: - span.set_attribute(SPANDATA.SERVER_ADDRESS, server_address) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, server_address) server_port = conn.engine.url.port if server_port is not None: - span.set_attribute(SPANDATA.SERVER_PORT, server_port) + span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, server_port) diff --git a/tests/integrations/aiomysql/test_aiomysql.py b/tests/integrations/aiomysql/test_aiomysql.py index e3ffe60329..e31ce7803f 100644 --- a/tests/integrations/aiomysql/test_aiomysql.py +++ b/tests/integrations/aiomysql/test_aiomysql.py @@ -18,10 +18,10 @@ import aiomysql import pytest import pytest_asyncio +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.aiomysql import AioMySQLIntegration from sentry_sdk.tracing_utils import record_sql_queries from tests.conftest import ApproxDict @@ -528,10 +528,10 @@ async def test_query_source_disabled(sentry_init, capture_items): data = insert_span.get("attributes", {}) - assert "code.line.number" not in data - assert "code.namespace" not in data - assert "code.file.path" not in data - assert "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.asyncio @@ -575,10 +575,10 @@ async def test_query_source_enabled(sentry_init, capture_items, enable_db_query_ data = insert_span.get("attributes", {}) - assert "code.line.number" in data - assert "code.namespace" in data - assert "code.file.path" in data - assert "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 @pytest.mark.asyncio @@ -618,22 +618,25 @@ async def test_query_source(sentry_init, capture_items): data = insert_span.get("attributes", {}) - assert "code.line.number" in data - assert "code.namespace" in data - assert "code.file.path" in data - assert "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 - assert type(data.get("code.line.number")) == int - assert data.get("code.line.number") > 0 - assert data.get("code.namespace") == "tests.integrations.aiomysql.test_aiomysql" - assert data.get("code.file.path").endswith( + 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) + == "tests.integrations.aiomysql.test_aiomysql" + ) + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/aiomysql/test_aiomysql.py" ) - is_relative_path = data.get("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("code.function") == "test_query_source" + assert data.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "test_query_source" @pytest.mark.asyncio @@ -684,10 +687,10 @@ def fake_record_sql_queries_streaming(*args, **kwargs): assert connect_span["name"] == "connect" data = insert_span.get("attributes", {}) - assert "code.line.number" not in data - assert "code.namespace" not in data - assert "code.file.path" not in data - assert "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.asyncio @@ -739,22 +742,28 @@ def fake_record_sql_queries_streaming(*args, **kwargs): data = insert_span.get("attributes", {}) - assert "code.line.number" in data - assert "code.namespace" in data - assert "code.file.path" in data - assert "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 - assert type(data.get("code.line.number")) == int - assert data.get("code.line.number") > 0 - assert data.get("code.namespace") == "tests.integrations.aiomysql.test_aiomysql" - assert data.get("code.file.path").endswith( + 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) + == "tests.integrations.aiomysql.test_aiomysql" + ) + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/aiomysql/test_aiomysql.py" ) - is_relative_path = data.get("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("code.function") == "test_query_source_if_duration_over_threshold" + assert ( + data.get(ATTRIBUTE_NAMES.CODE_FUNCTION) + == "test_query_source_if_duration_over_threshold" + ) @pytest.mark.asyncio @@ -783,11 +792,11 @@ async def test_span_origin(sentry_init, capture_items): segment_spans = [s for s in spans if s.get("is_segment")] assert len(segment_spans) == 1 - assert segment_spans[0]["attributes"]["sentry.origin"] == "manual" + assert segment_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" assert len(non_segment_spans) == 3 for span in non_segment_spans: - assert span["attributes"]["sentry.origin"] == "auto.db.aiomysql" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.aiomysql" @pytest.mark.asyncio @@ -854,8 +863,8 @@ async def test_db_data_on_spans(sentry_init, capture_items): s for s in spans if not s.get("is_segment") and "SELECT" in s.get("name", "") ][0] data = query_span.get("attributes", {}) - assert data.get(SPANDATA.DB_SYSTEM_NAME) == "mysql" - assert data.get(SPANDATA.SERVER_ADDRESS) == MYSQL_HOST - assert data.get(SPANDATA.SERVER_PORT) == MYSQL_PORT - assert data.get(SPANDATA.DB_NAMESPACE) == MYSQL_DB - assert data.get(SPANDATA.DB_USER) == MYSQL_USER + assert data.get(ATTRIBUTE_NAMES.DB_SYSTEM_NAME) == "mysql" + assert data.get(ATTRIBUTE_NAMES.SERVER_ADDRESS) == MYSQL_HOST + assert data.get(ATTRIBUTE_NAMES.SERVER_PORT) == MYSQL_PORT + assert data.get(ATTRIBUTE_NAMES.DB_NAMESPACE) == MYSQL_DB + assert data.get(ATTRIBUTE_NAMES.DB_USER) == MYSQL_USER diff --git a/tests/integrations/asyncpg/test_asyncpg.py b/tests/integrations/asyncpg/test_asyncpg.py index 2ef7993795..8eaae95e91 100644 --- a/tests/integrations/asyncpg/test_asyncpg.py +++ b/tests/integrations/asyncpg/test_asyncpg.py @@ -19,6 +19,7 @@ import pytest import pytest_asyncio from asyncpg import Connection, connect +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -536,9 +537,9 @@ async def test_query_source_disabled( data = insert_span.get("attributes", {}) assert SPANDATA.CODE_LINENO not in data - assert SPANDATA.CODE_NAMESPACE not in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in data assert SPANDATA.CODE_FILEPATH not in data - assert SPANDATA.CODE_FUNCTION not in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in data @pytest.mark.asyncio @@ -627,20 +628,20 @@ async def test_query_source_with_module_in_search_path( assert connect_span["name"] == "connect" data = insert_span.get("attributes", {}) - assert "code.line.number" in data - assert "code.file.path" in data - assert SPANDATA.CODE_NAMESPACE in data - assert SPANDATA.CODE_FUNCTION in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION in data - assert type(data.get("code.line.number")) == int - assert data.get("code.line.number") > 0 - assert data.get("code.file.path") == "asyncpg_helpers/helpers.py" - assert data.get(SPANDATA.CODE_NAMESPACE) == "asyncpg_helpers.helpers" + 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_FILE_PATH) == "asyncpg_helpers/helpers.py" + assert data.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) == "asyncpg_helpers.helpers" - is_relative_path = data.get("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) == "execute_query_in_connection" + assert data.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "execute_query_in_connection" @pytest.mark.asyncio @@ -682,9 +683,9 @@ async def test_no_query_source_if_duration_too_short( data = insert_span.get("attributes", {}) assert SPANDATA.CODE_LINENO not in data - assert SPANDATA.CODE_NAMESPACE not in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE not in data assert SPANDATA.CODE_FILEPATH not in data - assert SPANDATA.CODE_FUNCTION not in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION not in data @pytest.mark.asyncio @@ -766,10 +767,16 @@ async def test_span_origin(sentry_init, capture_items): assert select1_span["name"] == "SELECT 1" assert select2_span["name"] == "SELECT 2" - assert segment["attributes"]["sentry.origin"] == "manual" - assert connect_span["attributes"]["sentry.origin"] == "auto.db.asyncpg" - assert select1_span["attributes"]["sentry.origin"] == "auto.db.asyncpg" - assert select2_span["attributes"]["sentry.origin"] == "auto.db.asyncpg" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert ( + connect_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.asyncpg" + ) + assert ( + select1_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.asyncpg" + ) + assert ( + select2_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.asyncpg" + ) @pytest.mark.asyncio @@ -816,19 +823,22 @@ async def test_multiline_query_description_normalized( def _assert_query_source(span, expected_function): data = span.get("attributes", {}) - assert "code.line.number" in data - assert "code.file.path" in data - assert SPANDATA.CODE_NAMESPACE in data - assert SPANDATA.CODE_FUNCTION in data + assert ATTRIBUTE_NAMES.CODE_LINE_NUMBER in data + assert ATTRIBUTE_NAMES.CODE_FILE_PATH in data + assert ATTRIBUTE_NAMES.CODE_NAMESPACE in data + assert ATTRIBUTE_NAMES.CODE_FUNCTION in data - assert type(data.get("code.line.number")) == int - assert data.get("code.line.number") > 0 - assert data[SPANDATA.CODE_NAMESPACE] == "tests.integrations.asyncpg.test_asyncpg" - assert data.get("code.file.path").endswith( + assert type(data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER)) == int + assert data.get(ATTRIBUTE_NAMES.CODE_LINE_NUMBER) > 0 + assert ( + data[ATTRIBUTE_NAMES.CODE_NAMESPACE] + == "tests.integrations.asyncpg.test_asyncpg" + ) + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/asyncpg/test_asyncpg.py" ) - assert data.get("code.file.path")[0] != os.sep - assert data[SPANDATA.CODE_FUNCTION] == expected_function + assert data.get(ATTRIBUTE_NAMES.CODE_FILE_PATH)[0] != os.sep + assert data[ATTRIBUTE_NAMES.CODE_FUNCTION] == expected_function @pytest.mark.asyncio @@ -936,7 +946,7 @@ async def test_query_source_prepare( assert segment["name"] == "test_segment" assert ( - query_span["attributes"][SPANDATA.DB_QUERY_TEXT] + query_span["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SELECT * FROM users WHERE name = $1" ) @@ -991,8 +1001,10 @@ async def test_cursor_iteration_creates_db_cursor_iter_spans( assert len(cursor_iter_spans) == 5 for span in cursor_iter_spans: - assert span["attributes"]["sentry.op"] == OP.DB_CURSOR_ITERATOR - assert span["attributes"][SPANDATA.DB_QUERY_TEXT] == "SELECT * FROM users" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_CURSOR_ITERATOR + assert ( + span["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SELECT * FROM users" + ) @pytest.mark.asyncio @@ -1055,19 +1067,19 @@ async def test_cursor_fetch_methods_create_spans( assert commit_span["name"] == "COMMIT;" assert ( - fetchrow_span_1["attributes"][SPANDATA.DB_QUERY_TEXT] + fetchrow_span_1["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SELECT * FROM users WHERE dob > $1" ) assert ( - fetchrow_span_2["attributes"][SPANDATA.DB_QUERY_TEXT] + fetchrow_span_2["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SELECT * FROM users WHERE dob > $1" ) for span in (fetchrow_span_1, fetchrow_span_2): - assert span["attributes"][SPANDATA.DB_SYSTEM_NAME] == "postgresql" - assert span["attributes"][SPANDATA.DB_DRIVER_NAME] == "asyncpg" - assert span["attributes"]["sentry.op"] == OP.DB_CURSOR_FETCH - assert span["attributes"]["sentry.origin"] == "auto.db.asyncpg" + assert span["attributes"][ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "postgresql" + assert span["attributes"][ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "asyncpg" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_CURSOR_FETCH + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.asyncpg" _assert_query_source( span, diff --git a/tests/integrations/clickhouse_driver/test_clickhouse_driver.py b/tests/integrations/clickhouse_driver/test_clickhouse_driver.py index 1960819004..a17b946747 100644 --- a/tests/integrations/clickhouse_driver/test_clickhouse_driver.py +++ b/tests/integrations/clickhouse_driver/test_clickhouse_driver.py @@ -9,6 +9,7 @@ import clickhouse_driver from clickhouse_driver import Client, connect +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -447,15 +448,15 @@ def test_clickhouse_client_spans( { "name": "DROP TABLE IF EXISTS test", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "sentry.op": "db", - "db.query.text": "DROP TABLE IF EXISTS test", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "DROP TABLE IF EXISTS test", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -463,15 +464,15 @@ def test_clickhouse_client_spans( { "name": "CREATE TABLE test (x Int32) ENGINE = Memory", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "sentry.op": "db", - "db.query.text": "CREATE TABLE test (x Int32) ENGINE = Memory", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "CREATE TABLE test (x Int32) ENGINE = Memory", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -479,15 +480,15 @@ def test_clickhouse_client_spans( { "name": "INSERT INTO test (x) VALUES", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "sentry.op": "db", - "db.query.text": "INSERT INTO test (x) VALUES", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "INSERT INTO test (x) VALUES", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -495,15 +496,15 @@ def test_clickhouse_client_spans( { "name": "INSERT INTO test (x) VALUES", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "sentry.op": "db", - "db.query.text": "INSERT INTO test (x) VALUES", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "INSERT INTO test (x) VALUES", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -511,15 +512,15 @@ def test_clickhouse_client_spans( { "name": "SELECT sum(x) FROM test WHERE x > 150", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "sentry.op": "db", - "db.query.text": "SELECT sum(x) FROM test WHERE x > 150", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "SELECT sum(x) FROM test WHERE x > 150", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -674,15 +675,15 @@ def test_clickhouse_dbapi_spans( { "name": "DROP TABLE IF EXISTS test", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "db.query.text": "DROP TABLE IF EXISTS test", - "sentry.op": "db", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "DROP TABLE IF EXISTS test", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -690,15 +691,15 @@ def test_clickhouse_dbapi_spans( { "name": "CREATE TABLE test (x Int32) ENGINE = Memory", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "db.query.text": "CREATE TABLE test (x Int32) ENGINE = Memory", - "sentry.op": "db", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "CREATE TABLE test (x Int32) ENGINE = Memory", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -706,15 +707,15 @@ def test_clickhouse_dbapi_spans( { "name": "INSERT INTO test (x) VALUES", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "db.query.text": "INSERT INTO test (x) VALUES", - "sentry.op": "db", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "INSERT INTO test (x) VALUES", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -722,15 +723,15 @@ def test_clickhouse_dbapi_spans( { "name": "INSERT INTO test (x) VALUES", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "db.query.text": "INSERT INTO test (x) VALUES", - "sentry.op": "db", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "INSERT INTO test (x) VALUES", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -738,15 +739,15 @@ def test_clickhouse_dbapi_spans( { "name": "SELECT sum(x) FROM test WHERE x > 150", "attributes": { - "db.system.name": "clickhouse", - "db.driver.name": "clickhouse-driver", - "db.namespace": "", - "db.user": "default", - "db.query.text": "SELECT sum(x) FROM test WHERE x > 150", - "sentry.op": "db", - "sentry.origin": "auto.db.clickhouse_driver", - "server.address": "localhost", - "server.port": 9000, + ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "clickhouse", + ATTRIBUTE_NAMES.DB_DRIVER_NAME: "clickhouse-driver", + ATTRIBUTE_NAMES.DB_NAMESPACE: "", + ATTRIBUTE_NAMES.DB_USER: "default", + ATTRIBUTE_NAMES.DB_QUERY_TEXT: "SELECT sum(x) FROM test WHERE x > 150", + ATTRIBUTE_NAMES.SENTRY_OP: "db", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "auto.db.clickhouse_driver", + ATTRIBUTE_NAMES.SERVER_ADDRESS: "localhost", + ATTRIBUTE_NAMES.SERVER_PORT: 9000, }, "trace_id": trace_id, "parent_span_id": span_id, @@ -786,5 +787,8 @@ 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[0]["attributes"]["sentry.origin"] == "auto.db.clickhouse_driver" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert ( + spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.db.clickhouse_driver" + ) diff --git a/tests/integrations/pymongo/test_pymongo.py b/tests/integrations/pymongo/test_pymongo.py index c0139e7a6f..a368c43a1a 100644 --- a/tests/integrations/pymongo/test_pymongo.py +++ b/tests/integrations/pymongo/test_pymongo.py @@ -1,6 +1,7 @@ import pytest from mockupdb import MockupDB, OpQuery from pymongo import MongoClient +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -80,28 +81,31 @@ def test_segment( for span in find, insert_success, insert_fail: attrs = span["attributes"] - assert attrs["db.system.name"] == "mongodb" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "pymongo" - assert attrs["db.namespace"] == "test_db" - assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost" - assert attrs[SPANDATA.SERVER_PORT] == mongo_server.port - assert attrs["sentry.op"] == "db" - assert attrs["sentry.origin"] == "auto.db.pymongo" - - assert find["attributes"]["db.operation.name"] == "find" - assert insert_success["attributes"]["db.operation.name"] == "insert" - assert insert_fail["attributes"]["db.operation.name"] == "insert" + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "mongodb" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "pymongo" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "test_db" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == mongo_server.port + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == "db" + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.pymongo" + + assert find["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "find" + assert insert_success["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "insert" + assert insert_fail["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "insert" assert find["name"].startswith('{"find') assert insert_success["name"].startswith('{"insert') assert insert_fail["name"].startswith('{"insert') - assert find["attributes"]["db.collection.name"] == "test_collection" - assert insert_success["attributes"]["db.collection.name"] == "test_collection" - assert insert_fail["attributes"]["db.collection.name"] == "erroneous" + assert find["attributes"][ATTRIBUTE_NAMES.DB_COLLECTION_NAME] == "test_collection" + assert ( + insert_success["attributes"][ATTRIBUTE_NAMES.DB_COLLECTION_NAME] + == "test_collection" + ) + assert insert_fail["attributes"][ATTRIBUTE_NAMES.DB_COLLECTION_NAME] == "erroneous" for span in find, insert_success, insert_fail: - assert span["attributes"][SPANDATA.DB_QUERY_TEXT] == span["name"] + assert span["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == span["name"] if expect_query_values: assert "1" in find["name"] @@ -476,7 +480,7 @@ def test_span_origin(sentry_init, capture_items, mongo_server): assert len(spans) == 2 (db_span, segment) = spans assert segment["name"] == "test_segment" - assert db_span["attributes"]["sentry.origin"] == "auto.db.pymongo" + assert db_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.pymongo" def test_status_on_success(sentry_init, capture_items, mongo_server): diff --git a/tests/integrations/redis/asyncio/test_redis_asyncio.py b/tests/integrations/redis/asyncio/test_redis_asyncio.py index 8d37667a7b..7edd091991 100644 --- a/tests/integrations/redis/asyncio/test_redis_asyncio.py +++ b/tests/integrations/redis/asyncio/test_redis_asyncio.py @@ -1,9 +1,10 @@ import pytest from fakeredis.aioredis import FakeRedis +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import RedisIntegration @@ -60,14 +61,14 @@ async def test_async_redis_pipeline(sentry_init, capture_items): assert parent_span["name"] == "custom parent" assert pipeline_span["name"] == "redis.pipeline.execute" attrs = pipeline_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" - assert attrs[SPANDATA.DB_NAMESPACE] == "0" - assert attrs[SPANDATA.SERVER_ADDRESS] == ( + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "0" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == ( connection.connection_pool.connection_kwargs.get("host") ) - assert attrs[SPANDATA.SERVER_PORT] == 6379 + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 6379 @pytest.mark.asyncio @@ -95,6 +96,6 @@ async def test_async_span_origin(sentry_init, capture_items): set_span, pipeline_span, parent_span = [item.payload for item in items] assert parent_span["name"] == "custom parent" - assert parent_span["attributes"]["sentry.origin"] == "manual" - assert set_span["attributes"]["sentry.origin"] == "auto.db.redis" - assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis" + assert parent_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert set_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert pipeline_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" diff --git a/tests/integrations/redis/cluster/test_redis_cluster.py b/tests/integrations/redis/cluster/test_redis_cluster.py index 6bcb5c569b..66aac2de60 100644 --- a/tests/integrations/redis/cluster/test_redis_cluster.py +++ b/tests/integrations/redis/cluster/test_redis_cluster.py @@ -2,10 +2,11 @@ import pytest import redis +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import RedisIntegration @@ -94,14 +95,14 @@ def test_rediscluster_basic( span = redis_spans[-1] assert span["name"] == description attrs = span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" # ClusterNode converts localhost to 127.0.0.1 - assert attrs[SPANDATA.SERVER_ADDRESS] == "127.0.0.1" - assert attrs[SPANDATA.SERVER_PORT] == 6379 - assert attrs[SPANDATA.DB_OPERATION_NAME] == "SET" - assert attrs["db.redis.key"] == "bar" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "127.0.0.1" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 6379 + assert attrs[ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "SET" + assert attrs[ATTRIBUTE_NAMES.DB_REDIS_KEY] == "bar" def test_rediscluster_pipeline(sentry_init, capture_items): @@ -132,12 +133,12 @@ def test_rediscluster_pipeline(sentry_init, capture_items): pipeline_span = redis_spans[-1] assert pipeline_span["name"] == "redis.pipeline.execute" attrs = pipeline_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" # ClusterNode converts localhost to 127.0.0.1 - assert attrs[SPANDATA.SERVER_ADDRESS] == "127.0.0.1" - assert attrs[SPANDATA.SERVER_PORT] == 6379 + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "127.0.0.1" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 6379 def test_rediscluster_span_origin( @@ -170,8 +171,8 @@ def test_rediscluster_span_origin( redis_spans = payloads[:-1] assert parent_span["name"] == "custom parent" - assert parent_span["attributes"]["sentry.origin"] == "manual" + assert parent_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" assert len(redis_spans) >= 2 for span in redis_spans: - assert span["attributes"]["sentry.origin"] == "auto.db.redis" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" diff --git a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py index 604ad5463d..f5c83fff3b 100644 --- a/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py +++ b/tests/integrations/redis/cluster_asyncio/test_redis_cluster_asyncio.py @@ -1,9 +1,10 @@ import pytest from redis.asyncio import cluster +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import RedisIntegration from tests.conftest import ApproxDict @@ -96,13 +97,13 @@ async def test_async_basic( assert parent_span["name"] == "custom parent" assert redis_span["name"] == description attrs = redis_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" - assert attrs[SPANDATA.SERVER_ADDRESS] == "127.0.0.1" - assert attrs[SPANDATA.SERVER_PORT] == 6379 - assert attrs[SPANDATA.DB_OPERATION_NAME] == "SET" - assert attrs["db.redis.key"] == "bar" + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "127.0.0.1" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 6379 + assert attrs[ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "SET" + assert attrs[ATTRIBUTE_NAMES.DB_REDIS_KEY] == "bar" @pytest.mark.asyncio @@ -131,11 +132,11 @@ async def test_async_redis_pipeline(sentry_init, capture_items): assert parent_span["name"] == "custom parent" assert pipeline_span["name"] == "redis.pipeline.execute" attrs = pipeline_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" - assert attrs[SPANDATA.SERVER_ADDRESS] == "127.0.0.1" - assert attrs[SPANDATA.SERVER_PORT] == 6379 + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "127.0.0.1" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 6379 @pytest.mark.asyncio @@ -165,6 +166,6 @@ async def test_async_span_origin(sentry_init, capture_items): set_span, pipeline_span, parent_span = [item.payload for item in items] assert parent_span["name"] == "custom parent" - assert parent_span["attributes"]["sentry.origin"] == "manual" - assert set_span["attributes"]["sentry.origin"] == "auto.db.redis" - assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis" + assert parent_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert set_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert pipeline_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" diff --git a/tests/integrations/redis/test_redis.py b/tests/integrations/redis/test_redis.py index 1f8be1c7dc..ee8a3546c8 100644 --- a/tests/integrations/redis/test_redis.py +++ b/tests/integrations/redis/test_redis.py @@ -2,10 +2,11 @@ import pytest from fakeredis import FakeRedis +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import RedisIntegration MOCK_CONNECTION_POOL = mock.MagicMock() @@ -69,10 +70,14 @@ def test_pii_data_redacted( assert parent["name"] == "custom parent" assert set1["name"] == "SET 'somekey1' [Filtered]" - assert set1["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey1' [Filtered]" - assert set1["attributes"]["sentry.op"] == OP.DB_QUERY + assert ( + set1["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SET 'somekey1' [Filtered]" + ) + assert set1["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert set2["name"] == "SET 'somekey2' [Filtered]" - assert set2["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey2' [Filtered]" + assert ( + set2["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SET 'somekey2' [Filtered]" + ) assert get["name"] == "GET 'somekey2'" assert delete["name"] == "DEL 'somekey1' [Filtered]" @@ -116,8 +121,8 @@ def test_data_collection_database_query_data( assert parent["name"] == "custom parent" assert set_span["name"] == expected_description - assert set_span["attributes"][SPANDATA.DB_QUERY_TEXT] == expected_description - assert set_span["attributes"]["sentry.op"] == OP.DB_QUERY + assert set_span["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == expected_description + assert set_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY def test_no_data_truncation_by_default(sentry_init, capture_items): @@ -145,12 +150,14 @@ def test_no_data_truncation_by_default(sentry_init, capture_items): assert parent["name"] == "custom parent" assert set1["name"] == f"SET 'somekey1' '{long_string}'" assert ( - set1["attributes"][SPANDATA.DB_QUERY_TEXT] == f"SET 'somekey1' '{long_string}'" + set1["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] + == f"SET 'somekey1' '{long_string}'" ) - assert set1["attributes"]["sentry.op"] == OP.DB_QUERY + assert set1["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert set2["name"] == f"SET 'somekey2' '{short_string}'" assert ( - set2["attributes"][SPANDATA.DB_QUERY_TEXT] == f"SET 'somekey2' '{short_string}'" + set2["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] + == f"SET 'somekey2' '{short_string}'" ) @@ -219,13 +226,13 @@ def test_db_connection_attributes_client(sentry_init, capture_items): assert parent_span["name"] == "custom parent" assert redis_span["name"] == "GET 'foobar'" attrs = redis_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_QUERY_TEXT] == "GET 'foobar'" - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" - assert attrs[SPANDATA.DB_NAMESPACE] == "1" - assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost" - assert attrs[SPANDATA.SERVER_PORT] == 63791 + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "GET 'foobar'" + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "1" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 63791 def test_db_connection_attributes_pipeline(sentry_init, capture_items): @@ -252,12 +259,12 @@ def test_db_connection_attributes_pipeline(sentry_init, capture_items): assert parent_span["name"] == "custom parent" assert pipeline_span["name"] == "redis.pipeline.execute" attrs = pipeline_span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_DRIVER_NAME] == "redis-py" - assert attrs[SPANDATA.DB_NAMESPACE] == "1" - assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost" - assert attrs[SPANDATA.SERVER_PORT] == 63791 + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "redis-py" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "1" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 63791 def test_span_origin(sentry_init, capture_items): @@ -286,7 +293,10 @@ def test_span_origin(sentry_init, capture_items): set_span, pipeline_span, parent_span = [item.payload for item in items] assert parent_span["name"] == "custom parent" - assert parent_span["attributes"]["sentry.origin"] == "manual" - assert set_span["attributes"]["sentry.origin"] == "auto.db.redis" - assert set_span["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'somekey' 'somevalue'" - assert pipeline_span["attributes"]["sentry.origin"] == "auto.db.redis" + assert parent_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert set_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert ( + set_span["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] + == "SET 'somekey' 'somevalue'" + ) + assert pipeline_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" diff --git a/tests/integrations/redis/test_redis_cache_module.py b/tests/integrations/redis/test_redis_cache_module.py index 2c7c321097..fb312f575d 100644 --- a/tests/integrations/redis/test_redis_cache_module.py +++ b/tests/integrations/redis/test_redis_cache_module.py @@ -3,9 +3,10 @@ import fakeredis import pytest from fakeredis import FakeRedis +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.redis import RedisIntegration from sentry_sdk.integrations.redis.utils import _get_safe_key, _key_as_string from sentry_sdk.utils import parse_version @@ -33,7 +34,7 @@ def test_no_cache_basic(sentry_init, capture_items): assert len(items) == 2 db_span, parent_span = items[0].payload, items[1].payload assert parent_span["name"] == "custom parent" - assert db_span["attributes"]["sentry.op"] == OP.DB_QUERY + assert db_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY def test_cache_basic(sentry_init, capture_items): @@ -65,38 +66,41 @@ def test_cache_basic(sentry_init, capture_items): payloads = [item.payload for item in items] # hget: db only (HGET is not a cache command) - assert payloads[0]["attributes"]["sentry.op"] == OP.DB_QUERY - assert payloads[0]["attributes"][SPANDATA.DB_OPERATION_NAME] == "HGET" + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "HGET" # get: db then cache.get - assert payloads[1]["attributes"]["sentry.op"] == OP.DB_QUERY - assert payloads[1]["attributes"][SPANDATA.DB_OPERATION_NAME] == "GET" - assert payloads[2]["attributes"]["sentry.op"] == "cache.get" - assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'mycachekey'" + assert payloads[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert payloads[1]["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "GET" + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert ( + payloads[2]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "GET 'mycachekey'" + ) # set: db then cache.put - assert payloads[3]["attributes"]["sentry.op"] == OP.DB_QUERY - assert payloads[3]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SET" - assert payloads[4]["attributes"]["sentry.op"] == "cache.put" + assert payloads[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert payloads[3]["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "SET" + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert ( - payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT] == "SET 'mycachekey1' 'bla'" + payloads[4]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] + == "SET 'mycachekey1' 'bla'" ) # setex: db then cache.put - assert payloads[5]["attributes"]["sentry.op"] == OP.DB_QUERY - assert payloads[5]["attributes"][SPANDATA.DB_OPERATION_NAME] == "SETEX" - assert payloads[6]["attributes"]["sentry.op"] == "cache.put" + assert payloads[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert payloads[5]["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "SETEX" + assert payloads[6]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert ( - payloads[6]["attributes"][SPANDATA.DB_QUERY_TEXT] + payloads[6]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "SETEX 'mycachekey2' 10 'blub'" ) # mget: db then cache.get - assert payloads[7]["attributes"]["sentry.op"] == OP.DB_QUERY - assert payloads[7]["attributes"][SPANDATA.DB_OPERATION_NAME] == "MGET" - assert payloads[8]["attributes"]["sentry.op"] == "cache.get" + assert payloads[7]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert payloads[7]["attributes"][ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "MGET" + assert payloads[8]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert ( - payloads[8]["attributes"][SPANDATA.DB_QUERY_TEXT] + payloads[8]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "MGET 'mycachekey1' 'mycachekey2'" ) @@ -129,25 +133,27 @@ def test_cache_keys(sentry_init, capture_items): payloads = [item.payload for item in items] # somethingelse: db only - assert payloads[0]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[0]["name"] == "GET 'somethingelse'" # blub: db then cache.get - assert payloads[1]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[1]["name"] == "GET 'blub'" - assert payloads[2]["attributes"]["sentry.op"] == "cache.get" + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert payloads[2]["name"] == "blub" - assert payloads[2]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blub'" + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "GET 'blub'" # blubkeything: db then cache.get - assert payloads[3]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[3]["name"] == "GET 'blubkeything'" - assert payloads[4]["attributes"]["sentry.op"] == "cache.get" + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert payloads[4]["name"] == "blubkeything" - assert payloads[4]["attributes"][SPANDATA.DB_QUERY_TEXT] == "GET 'blubkeything'" + assert ( + payloads[4]["attributes"][ATTRIBUTE_NAMES.DB_QUERY_TEXT] == "GET 'blubkeything'" + ) # bl: db only (no prefix match) - assert payloads[5]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[5]["name"] == "GET 'bl'" assert payloads[6]["name"] == "custom parent" @@ -182,54 +188,56 @@ def test_cache_data(sentry_init, capture_items): payloads = [item.payload for item in items] # First get (miss) - assert payloads[0]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_get_miss = payloads[1] - assert cache_get_miss["attributes"]["sentry.op"] == "cache.get" + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert cache_get_miss["name"] == "mycachekey" - assert cache_get_miss["attributes"]["cache.key"] == ["mycachekey"] - assert cache_get_miss["attributes"]["cache.hit"] is False - assert "cache.item_size" not in cache_get_miss["attributes"] + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["mycachekey"] + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] is False + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in cache_get_miss["attributes"] if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_get_miss["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_get_miss["attributes"] else: - assert cache_get_miss["attributes"]["network.peer.port"] == 6378 + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_get_miss["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_get_miss["attributes"] else: - assert cache_get_miss["attributes"]["network.peer.address"] == host + assert ( + cache_get_miss["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host + ) # Set - assert payloads[2]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_put = payloads[3] - assert cache_put["attributes"]["sentry.op"] == "cache.put" + assert cache_put["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert cache_put["name"] == "mycachekey" - assert cache_put["attributes"]["cache.key"] == ["mycachekey"] - assert "cache.hit" not in cache_put["attributes"] - assert cache_put["attributes"]["cache.item_size"] == 18 + assert cache_put["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["mycachekey"] + assert ATTRIBUTE_NAMES.CACHE_HIT not in cache_put["attributes"] + assert cache_put["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 18 if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_put["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_put["attributes"] else: - assert cache_put["attributes"]["network.peer.port"] == 6378 + assert cache_put["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_put["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_put["attributes"] else: - assert cache_put["attributes"]["network.peer.address"] == host + assert cache_put["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host # Second get (hit) - assert payloads[4]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_get_hit = payloads[5] - assert cache_get_hit["attributes"]["sentry.op"] == "cache.get" - assert cache_get_hit["attributes"]["cache.key"] == ["mycachekey"] - assert cache_get_hit["attributes"]["cache.hit"] is True - assert cache_get_hit["attributes"]["cache.item_size"] == 18 + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["mycachekey"] + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] is True + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 18 if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_get_hit["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_get_hit["attributes"] else: - assert cache_get_hit["attributes"]["network.peer.port"] == 6378 + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_get_hit["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_get_hit["attributes"] else: - assert cache_get_hit["attributes"]["network.peer.address"] == host + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host assert payloads[6]["name"] == "custom parent" @@ -266,7 +274,9 @@ def test_cache_prefixes(sentry_init, capture_items): assert payloads[-1]["name"] == "custom parent" cache_spans = [ - p for p in payloads if p["attributes"].get("sentry.op") == "cache.get" + p + for p in payloads + if p["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "cache.get" ] assert len(cache_spans) == 5 diff --git a/tests/integrations/redis/test_redis_cache_module_async.py b/tests/integrations/redis/test_redis_cache_module_async.py index f7990df627..7303915e59 100644 --- a/tests/integrations/redis/test_redis_cache_module_async.py +++ b/tests/integrations/redis/test_redis_cache_module_async.py @@ -9,9 +9,10 @@ import fakeredis from fakeredis.aioredis import FakeRedis as FakeRedisAsync +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.redis import RedisIntegration from sentry_sdk.utils import parse_version @@ -37,7 +38,7 @@ async def test_no_cache_basic(sentry_init, capture_items): assert len(items) == 2 db_span, parent_span = items[0].payload, items[1].payload assert parent_span["name"] == "custom parent" - assert db_span["attributes"]["sentry.op"] == OP.DB_QUERY + assert db_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY @pytest.mark.asyncio @@ -61,9 +62,9 @@ async def test_cache_basic(sentry_init, capture_items): assert len(items) == 3 db_span, cache_span, parent_span = [item.payload for item in items] assert parent_span["name"] == "custom parent" - assert db_span["attributes"]["sentry.op"] == OP.DB_QUERY - assert cache_span["attributes"]["sentry.op"] == "cache.get" - assert cache_span["attributes"][SPANDATA.CACHE_KEY] == ["myasynccachekey"] + assert db_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert cache_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert cache_span["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["myasynccachekey"] @pytest.mark.asyncio @@ -91,25 +92,25 @@ async def test_cache_keys(sentry_init, capture_items): payloads = [item.payload for item in items] # asomethingelse: db only - assert payloads[0]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[0]["name"] == "GET 'asomethingelse'" # ablub: db then cache.get - assert payloads[1]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[1]["name"] == "GET 'ablub'" - assert payloads[2]["attributes"]["sentry.op"] == "cache.get" + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert payloads[2]["name"] == "ablub" - assert payloads[2]["attributes"][SPANDATA.CACHE_KEY] == ["ablub"] + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["ablub"] # ablubkeything: db then cache.get - assert payloads[3]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[3]["name"] == "GET 'ablubkeything'" - assert payloads[4]["attributes"]["sentry.op"] == "cache.get" + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert payloads[4]["name"] == "ablubkeything" - assert payloads[4]["attributes"][SPANDATA.CACHE_KEY] == ["ablubkeything"] + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["ablubkeything"] # abl: db only (no prefix match) - assert payloads[5]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY assert payloads[5]["name"] == "GET 'abl'" assert payloads[6]["name"] == "custom parent" @@ -142,53 +143,57 @@ async def test_cache_data(sentry_init, capture_items): payloads = [item.payload for item in items] # First get (miss) - assert payloads[0]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_get_miss = payloads[1] - assert cache_get_miss["attributes"]["sentry.op"] == "cache.get" + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" assert cache_get_miss["name"] == "myasynccachekey" - assert cache_get_miss["attributes"]["cache.key"] == ["myasynccachekey"] - assert cache_get_miss["attributes"]["cache.hit"] is False - assert "cache.item_size" not in cache_get_miss["attributes"] + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == [ + "myasynccachekey" + ] + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] is False + assert ATTRIBUTE_NAMES.CACHE_ITEM_SIZE not in cache_get_miss["attributes"] if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_get_miss["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_get_miss["attributes"] else: - assert cache_get_miss["attributes"]["network.peer.port"] == 6378 + assert cache_get_miss["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_get_miss["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_get_miss["attributes"] else: - assert cache_get_miss["attributes"]["network.peer.address"] == host + assert ( + cache_get_miss["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host + ) # Set - assert payloads[2]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_put = payloads[3] - assert cache_put["attributes"]["sentry.op"] == "cache.put" + assert cache_put["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.put" assert cache_put["name"] == "myasynccachekey" - assert cache_put["attributes"]["cache.key"] == ["myasynccachekey"] - assert "cache.hit" not in cache_put["attributes"] - assert cache_put["attributes"]["cache.item_size"] == 18 + assert cache_put["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["myasynccachekey"] + assert ATTRIBUTE_NAMES.CACHE_HIT not in cache_put["attributes"] + assert cache_put["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 18 if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_put["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_put["attributes"] else: - assert cache_put["attributes"]["network.peer.port"] == 6378 + assert cache_put["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_put["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_put["attributes"] else: - assert cache_put["attributes"]["network.peer.address"] == host + assert cache_put["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host # Second get (hit) - assert payloads[4]["attributes"]["sentry.op"] == OP.DB_QUERY + assert payloads[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY cache_get_hit = payloads[5] - assert cache_get_hit["attributes"]["sentry.op"] == "cache.get" - assert cache_get_hit["attributes"]["cache.key"] == ["myasynccachekey"] - assert cache_get_hit["attributes"]["cache.hit"] is True - assert cache_get_hit["attributes"]["cache.item_size"] == 18 + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "cache.get" + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_KEY] == ["myasynccachekey"] + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_HIT] is True + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.CACHE_ITEM_SIZE] == 18 if FAKEREDIS_VERSION <= (2, 7, 1): - assert "network.peer.port" not in cache_get_hit["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_PORT not in cache_get_hit["attributes"] else: - assert cache_get_hit["attributes"]["network.peer.port"] == 6378 + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_PORT] == 6378 if FAKEREDIS_VERSION <= (1, 7, 1): - assert "network.peer.address" not in cache_get_hit["attributes"] + assert ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS not in cache_get_hit["attributes"] else: - assert cache_get_hit["attributes"]["network.peer.address"] == host + assert cache_get_hit["attributes"][ATTRIBUTE_NAMES.NETWORK_PEER_ADDRESS] == host assert payloads[6]["name"] == "custom parent" diff --git a/tests/integrations/redis_py_cluster_legacy/test_redis_py_cluster_legacy.py b/tests/integrations/redis_py_cluster_legacy/test_redis_py_cluster_legacy.py index 502bf10131..33d08eb684 100644 --- a/tests/integrations/redis_py_cluster_legacy/test_redis_py_cluster_legacy.py +++ b/tests/integrations/redis_py_cluster_legacy/test_redis_py_cluster_legacy.py @@ -2,10 +2,11 @@ import pytest import rediscluster +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.redis import RedisIntegration MOCK_CONNECTION_POOL = mock.MagicMock() @@ -76,14 +77,14 @@ def test_rediscluster_basic(sentry_init, capture_items, data_collection, descrip assert span["name"] == description attrs = span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs["sentry.origin"] == "auto.db.redis" - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_NAMESPACE] == "1" - assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost" - assert attrs[SPANDATA.SERVER_PORT] == 63791 - assert attrs[SPANDATA.DB_OPERATION_NAME] == "SET" - assert attrs["db.redis.key"] == "bar" + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "1" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 63791 + assert attrs[ATTRIBUTE_NAMES.DB_OPERATION_NAME] == "SET" + assert attrs[ATTRIBUTE_NAMES.DB_REDIS_KEY] == "bar" def test_rediscluster_pipeline(sentry_init, capture_items): @@ -107,9 +108,9 @@ def test_rediscluster_pipeline(sentry_init, capture_items): assert span["name"] == "redis.pipeline.execute" attrs = span["attributes"] - assert attrs["sentry.op"] == OP.DB_QUERY - assert attrs["sentry.origin"] == "auto.db.redis" - assert attrs[SPANDATA.DB_SYSTEM_NAME] == "redis" - assert attrs[SPANDATA.DB_NAMESPACE] == "1" - assert attrs[SPANDATA.SERVER_ADDRESS] == "localhost" - assert attrs[SPANDATA.SERVER_PORT] == 63791 + assert attrs[ATTRIBUTE_NAMES.SENTRY_OP] == OP.DB_QUERY + assert attrs[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert attrs[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "redis" + assert attrs[ATTRIBUTE_NAMES.DB_NAMESPACE] == "1" + assert attrs[ATTRIBUTE_NAMES.SERVER_ADDRESS] == "localhost" + assert attrs[ATTRIBUTE_NAMES.SERVER_PORT] == 63791 diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index d6d31996cb..290958a49c 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -3,6 +3,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from sqlalchemy import Column, ForeignKey, Integer, String, create_engine, text from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.declarative import declarative_base @@ -132,13 +133,13 @@ class Address(Base): sqlalchemy_spans = [ span for span in spans - if span["attributes"]["sentry.origin"] == "auto.db.sqlalchemy" + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.sqlalchemy" ] for span in sqlalchemy_spans: - assert span["attributes"][SPANDATA.DB_SYSTEM_NAME] == "sqlite" - assert span["attributes"][SPANDATA.DB_DRIVER_NAME] == "pysqlite" - assert span["attributes"][SPANDATA.DB_NAMESPACE] == ":memory:" - assert SPANDATA.SERVER_PORT not in span["attributes"] + assert span["attributes"][ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "sqlite" + assert span["attributes"][ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "pysqlite" + assert span["attributes"][ATTRIBUTE_NAMES.DB_NAMESPACE] == ":memory:" + assert ATTRIBUTE_NAMES.SERVER_PORT not in span["attributes"] assert ( render_span_tree(spans) @@ -214,13 +215,13 @@ class Address(Base): sqlalchemy_spans = [ span for span in spans - if span["attributes"]["sentry.origin"] == "auto.db.sqlalchemy" + if span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.sqlalchemy" ] for span in sqlalchemy_spans: - assert span["attributes"][SPANDATA.DB_SYSTEM_NAME] == "sqlite" - assert span["attributes"][SPANDATA.DB_DRIVER_NAME] == "pysqlite" + assert span["attributes"][ATTRIBUTE_NAMES.DB_SYSTEM_NAME] == "sqlite" + assert span["attributes"][ATTRIBUTE_NAMES.DB_DRIVER_NAME] == "pysqlite" assert SPANDATA.DB_NAME not in span["attributes"] - assert SPANDATA.SERVER_PORT not in span["attributes"] + assert ATTRIBUTE_NAMES.SERVER_PORT not in span["attributes"] def test_long_sql_query_preserved( @@ -302,15 +303,15 @@ class Person(Base): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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 break else: raise AssertionError("No db span found") @@ -359,15 +360,15 @@ class Person(Base): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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 break else: raise AssertionError("No db span found") @@ -409,30 +410,32 @@ class Person(Base): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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.sqlalchemy.test_sqlalchemy" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/sqlalchemy/test_sqlalchemy.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_query_source" + assert attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "test_query_source" break else: raise AssertionError("No db span found") @@ -483,31 +486,34 @@ class Person(Base): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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) == "sqlalchemy_helpers.helpers" + attributes.get(ATTRIBUTE_NAMES.CODE_NAMESPACE) + == "sqlalchemy_helpers.helpers" ) assert ( - attributes.get(SPANDATA.CODE_FILE_PATH) + attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH) == "sqlalchemy_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) + attributes.get(ATTRIBUTE_NAMES.CODE_FUNCTION) == "query_first_model_from_session" ) break @@ -569,15 +575,15 @@ def __exit__(self, type, value, traceback): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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 break else: raise AssertionError("No db span found") @@ -638,31 +644,33 @@ def __exit__(self, type, value, traceback): sentry_sdk.flush() spans = [item.payload for item in items] for span in spans: - if span["attributes"].get("sentry.op") == "db" and span["name"].startswith( - "SELECT person" - ): + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == "db" and span[ + "name" + ].startswith("SELECT person"): 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.sqlalchemy.test_sqlalchemy" ) - assert attributes.get(SPANDATA.CODE_FILE_PATH).endswith( + assert attributes.get(ATTRIBUTE_NAMES.CODE_FILE_PATH).endswith( "tests/integrations/sqlalchemy/test_sqlalchemy.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) == "test_query_source_if_duration_over_threshold" ) break @@ -690,5 +698,5 @@ def test_span_origin( sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[0]["attributes"]["sentry.origin"] == "auto.db.sqlalchemy" - assert spans[1]["attributes"]["sentry.origin"] == "manual" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.sqlalchemy" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual"