diff --git a/sentry_sdk/logger.py b/sentry_sdk/logger.py index d7f4425dfd..d1b7ad4be9 100644 --- a/sentry_sdk/logger.py +++ b/sentry_sdk/logger.py @@ -3,6 +3,8 @@ import time from typing import TYPE_CHECKING, Any +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.utils import capture_internal_exceptions, format_attribute @@ -46,7 +48,7 @@ def _capture_log( if kwargs: # only attach template if there are parameters - attributes["sentry.message.template"] = format_attribute(template) + attributes[ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] = format_attribute(template) with capture_internal_exceptions(): body = template.format_map(_dict_default_key(kwargs)) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index abeafdb22a..3faa8859de 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -11,13 +11,14 @@ from itertools import chain from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk._types import AnnotatedValue from sentry_sdk.attachments import Attachment from sentry_sdk.consts import ( DEFAULT_MAX_BREADCRUMBS, FALSE_VALUES, - SPANDATA, ) from sentry_sdk.feature_flags import DEFAULT_FLAG_CAPACITY, FlagBuffer from sentry_sdk.session import Session @@ -343,15 +344,15 @@ def get_global_scope(cls) -> "Scope": return _global_scope def set_global_attributes(self) -> None: - self.set_attribute(SPANDATA.SENTRY_SDK_NAME, SDK_INFO["name"]) - self.set_attribute(SPANDATA.SENTRY_SDK_VERSION, SDK_INFO["version"]) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SDK_NAME, SDK_INFO["name"]) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SDK_VERSION, SDK_INFO["version"]) self.set_attribute( - "process.runtime.name", + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME, platform.python_implementation(), ) self.set_attribute( - "process.runtime.version", + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION, f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", ) @@ -359,15 +360,15 @@ def set_global_attributes(self) -> None: server_name = options.get("server_name") if server_name: - self.set_attribute(SPANDATA.SERVER_ADDRESS, server_name) + self.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, server_name) environment = options.get("environment") if environment: - self.set_attribute(SPANDATA.SENTRY_ENVIRONMENT, environment) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT, environment) release = options.get("release") if release: - self.set_attribute(SPANDATA.SENTRY_RELEASE, release) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_RELEASE, release) @classmethod def last_event_id(cls) -> "Optional[str]": @@ -704,7 +705,8 @@ def set_transaction_name(self, name: str, source: "Optional[str]" = None) -> Non self._span._segment.name = name if source: self._span._segment.set_attribute( - "sentry.segment.name.source", getattr(source, "value", source) + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, + getattr(source, "value", source), ) if source: @@ -734,18 +736,18 @@ def span(self, span: "Optional[Span]") -> None: if type(span) is Span: self._transaction = span.name - if span._attributes.get("sentry.segment.name.source"): + if span._attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE): self._transaction_info["source"] = str( - span._attributes["sentry.segment.name.source"] + span._attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] ) return if type(span) is NoOpSpan: if span._name is not None: self._transaction = span.name - if span._attributes.get("sentry.segment.name.source"): + if span._attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE): self._transaction_info["source"] = str( - span._attributes["sentry.segment.name.source"] + span._attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] ) def set_tag(self, key: str, value: "Any") -> None: @@ -1439,10 +1441,10 @@ def _apply_user_attributes_to_telemetry( return for attribute_name, user_attribute in ( - ("user.id", "id"), - ("user.name", "username"), - ("user.email", "email"), - ("user.ip_address", "ip_address"), + (ATTRIBUTE_NAMES.USER_ID, "id"), + (ATTRIBUTE_NAMES.USER_NAME, "username"), + (ATTRIBUTE_NAMES.USER_EMAIL, "email"), + (ATTRIBUTE_NAMES.USER_IP_ADDRESS, "ip_address"), ): if ( user_attribute in self._user diff --git a/sentry_sdk/traces.py b/sentry_sdk/traces.py index 0d9d55e2af..97f68c3bf9 100644 --- a/sentry_sdk/traces.py +++ b/sentry_sdk/traces.py @@ -6,8 +6,9 @@ from enum import Enum from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import SPANDATA from sentry_sdk.profiler.continuous_profiler import ( get_profiler_id, try_autostart_continuous_profiler, @@ -211,8 +212,8 @@ def __init__( self._name: str = name self._active: bool = active self._attributes: "Attributes" = { - "sentry.origin": "manual", - "sentry.trace_lifecycle": "stream", + ATTRIBUTE_NAMES.SENTRY_ORIGIN: "manual", + ATTRIBUTE_NAMES.SENTRY_TRACE_LIFECYCLE: "stream", } if attributes: @@ -322,19 +323,23 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None # Set attributes from the segment. These are set on span end on purpose # so that we have the best chance to capture the segment's final name # (since it might change during its lifetime) - self.set_attribute("sentry.segment.id", self._segment.span_id) - self.set_attribute("sentry.segment.name", self._segment.name) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID, self._segment.span_id) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME, self._segment.name) # Set conversation ID if one is set and this is a correct kind of span conversation_id = self._scope.get_conversation_id() if conversation_id: - has_ai_operation_name = SPANDATA.GEN_AI_OPERATION_NAME in self._attributes - op = self._attributes.get("sentry.op") + has_ai_operation_name = ( + ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME in self._attributes + ) + op = self._attributes.get(ATTRIBUTE_NAMES.SENTRY_OP) has_gen_ai_op = isinstance(op, str) and ( op.startswith("ai.") or op.startswith("gen_ai.") ) if has_ai_operation_name or has_gen_ai_op: - self.set_attribute("gen_ai.conversation.id", conversation_id) + self.set_attribute( + ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conversation_id + ) # Set the end timestamp if end_timestamp is not None: @@ -457,10 +462,10 @@ def _update_active_thread(self) -> None: thread_id, thread_name = get_current_thread_meta() if thread_id is not None: - self.set_attribute(SPANDATA.THREAD_ID, str(thread_id)) + self.set_attribute(ATTRIBUTE_NAMES.THREAD_ID, str(thread_id)) if thread_name is not None: - self.set_attribute(SPANDATA.THREAD_NAME, thread_name) + self.set_attribute(ATTRIBUTE_NAMES.THREAD_NAME, thread_name) def _dynamic_sampling_context(self) -> "dict[str, str]": return self._segment._get_baggage().dynamic_sampling_context() @@ -516,16 +521,16 @@ def _get_trace_context(self) -> "dict[str, Any]": "dynamic_sampling_context": self._dynamic_sampling_context(), } - if "sentry.op" in self._attributes: - context["op"] = self._attributes["sentry.op"] - if "sentry.origin" in self._attributes: - context["origin"] = self._attributes["sentry.origin"] + if ATTRIBUTE_NAMES.SENTRY_OP in self._attributes: + context["op"] = self._attributes[ATTRIBUTE_NAMES.SENTRY_OP] + if ATTRIBUTE_NAMES.SENTRY_ORIGIN in self._attributes: + context["origin"] = self._attributes[ATTRIBUTE_NAMES.SENTRY_ORIGIN] return context def _set_profile_id(self, profiler_id: "Optional[str]") -> None: if profiler_id is not None: - self.set_attribute("sentry.profiler_id", profiler_id) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_PROFILER_ID, profiler_id) def _start_profile(self) -> None: if not self._is_segment(): @@ -541,19 +546,23 @@ def _set_segment_attributes(self) -> None: client = sentry_sdk.get_client() - self.set_attribute(SPANDATA.SENTRY_PLATFORM, "python") - self.set_attribute(SPANDATA.PROCESS_COMMAND_ARGS, sys.argv) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_PLATFORM, "python") + self.set_attribute(ATTRIBUTE_NAMES.PROCESS_COMMAND_ARGS, sys.argv) self.set_attribute( - SPANDATA.SENTRY_SDK_INTEGRATIONS, sorted(client.integrations.keys()) + ATTRIBUTE_NAMES.SENTRY_SDK_INTEGRATIONS, sorted(client.integrations.keys()) ) - if not self.get_attributes().get("sentry.segment.name.source"): + if not self.get_attributes().get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE): self.set_attribute( - "sentry.segment.name.source", SegmentNameSource.CUSTOM.value + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, + SegmentNameSource.CUSTOM.value, ) - if client.options.get("dist") and SPANDATA.SENTRY_DIST not in self._attributes: + if ( + client.options.get("dist") + and ATTRIBUTE_NAMES.SENTRY_DIST not in self._attributes + ): self.set_attribute( - SPANDATA.SENTRY_DIST, str(client.options["dist"]).strip() + ATTRIBUTE_NAMES.SENTRY_DIST, str(client.options["dist"]).strip() ) def _to_json(self) -> "SpanJSON": @@ -604,9 +613,13 @@ def __init__( ) -> None: self._name = name # type: ignore[assignment] self._attributes = {} - if attributes is not None and "sentry.segment.name.source" in attributes: + if ( + attributes is not None + and ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE in attributes + ): self.set_attribute( - "sentry.segment.name.source", attributes["sentry.segment.name.source"] + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, + attributes[ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE], ) self._span_id: "Optional[str]" = None @@ -685,17 +698,17 @@ def get_attributes(self) -> "Attributes": return {} def set_attribute(self, key: str, value: "AttributeValue") -> None: - if key != "sentry.segment.name.source": + if key != ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: return - super().set_attribute("sentry.segment.name.source", value) + super().set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, value) def set_attributes(self, attributes: "Attributes") -> None: for key, value in attributes.items(): - if key != "sentry.segment.name.source": + if key != ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: continue - self.set_attribute("sentry.segment.name.source", value) + self.set_attribute(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE, value) def remove_attribute(self, key: str) -> None: pass diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index f168403ab8..a9d0e89f59 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -12,8 +12,10 @@ from typing import TYPE_CHECKING from urllib.parse import quote, unquote +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.data_collection import ( _apply_data_collection_filtering_to_query_string, ) @@ -138,13 +140,15 @@ def record_sql_queries( additional_attributes = {} if query is not None: - additional_attributes["db.query.text"] = query + additional_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = query with sentry_sdk.start_span( name="" if query is None else query, attributes={ - "sentry.origin": span_origin, - "sentry.op": span_op_override_value if span_op_override_value else OP.DB, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: span_origin, + ATTRIBUTE_NAMES.SENTRY_OP: span_op_override_value + if span_op_override_value + else OP.DB, **additional_attributes, }, ) as span: @@ -188,14 +192,14 @@ def get_url_attributes( url_full = parsed_url.url if query: - attributes[SPANDATA.URL_QUERY] = query + attributes[ATTRIBUTE_NAMES.URL_QUERY] = query url_full += "?" + query if parsed_url.fragment: - attributes[SPANDATA.URL_FRAGMENT] = parsed_url.fragment + attributes[ATTRIBUTE_NAMES.URL_FRAGMENT] = parsed_url.fragment url_full += "#" + parsed_url.fragment - attributes[SPANDATA.URL_FULL] = url_full + attributes[ATTRIBUTE_NAMES.URL_FULL] = url_full return attributes @@ -271,14 +275,14 @@ def add_source( except Exception: lineno = None if lineno is not None: - span.set_attribute("code.line.number", lineno) + span.set_attribute(ATTRIBUTE_NAMES.CODE_LINE_NUMBER, lineno) try: namespace = frame.f_globals.get("__name__") except Exception: namespace = None if namespace is not None: - span.set_attribute(SPANDATA.CODE_NAMESPACE, namespace) + span.set_attribute(ATTRIBUTE_NAMES.CODE_NAMESPACE, namespace) filepath = _get_frame_module_abs_path(frame) if filepath is not None: @@ -290,7 +294,7 @@ def add_source( in_app_path = filepath if in_app_path is not None: - span.set_attribute("code.file.path", in_app_path) + span.set_attribute(ATTRIBUTE_NAMES.CODE_FILE_PATH, in_app_path) try: code_function = frame.f_code.co_name @@ -298,7 +302,7 @@ def add_source( code_function = None if code_function is not None: - span.set_attribute(SPANDATA.CODE_FUNCTION, frame.f_code.co_name) + span.set_attribute(ATTRIBUTE_NAMES.CODE_FUNCTION, frame.f_code.co_name) def add_query_source( @@ -759,7 +763,7 @@ def populate_from_segment(cls, segment: "Span") -> "Baggage": sentry_items["org_id"] = client.parsed_dsn.org_id if ( - segment.get_attributes().get("sentry.segment.name.source") + segment.get_attributes().get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE) not in LOW_QUALITY_SEGMENT_SOURCES ) and segment.name: sentry_items["transaction"] = segment.name @@ -906,8 +910,8 @@ def span_decorator(f: "Any") -> "Any": Decorator to create a span for the given function. """ new_attributes = dict(attributes) if attributes else {} - if "sentry.op" not in new_attributes: - new_attributes["sentry.op"] = OP.FUNCTION + if ATTRIBUTE_NAMES.SENTRY_OP not in new_attributes: + new_attributes[ATTRIBUTE_NAMES.SENTRY_OP] = OP.FUNCTION @functools.wraps(f) async def async_wrapper(*args: "Any", **kwargs: "Any") -> "Any": @@ -1105,12 +1109,12 @@ def _make_sampling_decision( "trace_id": propagation_context.trace_id, "span_id": None, "parent_span_id": propagation_context.parent_span_id, - "op": attributes.get("sentry.op"), + "op": attributes.get(ATTRIBUTE_NAMES.SENTRY_OP), "name": name, "description": name, "start_timestamp": None, "timestamp": None, - "source": attributes.get("sentry.segment.name.source"), + "source": attributes.get(ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE), "sampled": None, "data": attributes, }, diff --git a/tests/conftest.py b/tests/conftest.py index 370432913e..c1b91b05d2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,6 +41,8 @@ except (ImportError, RuntimeError): TestClient = None # type: ignore[assignment, misc] +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk import sentry_sdk.utils from sentry_sdk.envelope import Envelope, parse_json @@ -553,7 +555,7 @@ def inner(spans): def render_span(span): yield "- sentry.op={}: name={}".format( - json.dumps(span["attributes"].get("sentry.op")), + json.dumps(span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP)), json.dumps(span["name"]), ) diff --git a/tests/profiler/test_continuous_profiler.py b/tests/profiler/test_continuous_profiler.py index b19e4e8a30..9de4463aa1 100644 --- a/tests/profiler/test_continuous_profiler.py +++ b/tests/profiler/test_continuous_profiler.py @@ -4,6 +4,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import VERSION @@ -145,8 +146,8 @@ def assert_single_transaction_with_profile_chunks( { "data": ApproxDict( { - "thread.id": str(thread.ident), - "thread.name": thread.name, + ATTRIBUTE_NAMES.THREAD_ID: str(thread.ident), + ATTRIBUTE_NAMES.THREAD_NAME: thread.name, } ), } @@ -163,8 +164,8 @@ def assert_single_transaction_with_profile_chunks( assert span["data"] == ApproxDict( { "profiler_id": profiler_id, - "thread.id": str(thread.ident), - "thread.name": thread.name, + ATTRIBUTE_NAMES.THREAD_ID: str(thread.ident), + ATTRIBUTE_NAMES.THREAD_NAME: thread.name, } ) @@ -214,16 +215,23 @@ def assert_single_segment_with_profile_chunks( segment = segments_list[0] - assert segment["attributes"]["thread.id"]["value"] == str(thread.ident) - assert segment["attributes"]["thread.name"]["value"] == thread.name + assert segment["attributes"][ATTRIBUTE_NAMES.THREAD_ID]["value"] == str( + thread.ident + ) + assert segment["attributes"][ATTRIBUTE_NAMES.THREAD_NAME]["value"] == thread.name - profiler_id = segment["attributes"]["sentry.profiler_id"]["value"] + profiler_id = segment["attributes"][ATTRIBUTE_NAMES.SENTRY_PROFILER_ID]["value"] assert len(child_span_list) > 0 for span in child_span_list: - assert span["attributes"]["sentry.profiler_id"]["value"] == profiler_id - assert span["attributes"]["thread.id"]["value"] == str(thread.ident) - assert span["attributes"]["thread.name"]["value"] == thread.name + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_PROFILER_ID]["value"] + == profiler_id + ) + assert span["attributes"][ATTRIBUTE_NAMES.THREAD_ID]["value"] == str( + thread.ident + ) + assert span["attributes"][ATTRIBUTE_NAMES.THREAD_NAME]["value"] == thread.name for profile_chunk_item in items["profile_chunk"]: profile_chunk = profile_chunk_item.payload.json @@ -270,7 +278,7 @@ def assert_single_segment_without_profile_chunks(envelopes): assert len(items["profile_chunk"]) == 0 segment = segments_list[0] - assert "sentry.profiler_id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_PROFILER_ID not in segment["attributes"] @pytest.mark.forked diff --git a/tests/test_attributes.py b/tests/test_attributes.py index 394d49a313..af124bd0dc 100644 --- a/tests/test_attributes.py +++ b/tests/test_attributes.py @@ -1,4 +1,5 @@ import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk @@ -213,15 +214,15 @@ def test_user_attributes(sentry_init, capture_items): metric, span = [item.payload for item in items] - assert metric["attributes"]["user.id"] == "123456" - assert metric["attributes"]["user.name"] == "username" - assert metric["attributes"]["user.email"] == "username@example.com" - assert metric["attributes"]["user.ip_address"] == "127.0.0.1" + assert metric["attributes"][ATTRIBUTE_NAMES.USER_ID] == "123456" + assert metric["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "username" + assert metric["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "username@example.com" + assert metric["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" - assert span["attributes"]["user.id"] == "123456" - assert span["attributes"]["user.name"] == "username" - assert span["attributes"]["user.email"] == "username@example.com" - assert span["attributes"]["user.ip_address"] == "127.0.0.1" + assert span["attributes"][ATTRIBUTE_NAMES.USER_ID] == "123456" + assert span["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "username" + assert span["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "username@example.com" + assert span["attributes"][ATTRIBUTE_NAMES.USER_IP_ADDRESS] == "127.0.0.1" @pytest.mark.parametrize( diff --git a/tests/test_logs.py b/tests/test_logs.py index f594ba21e6..f053199b12 100644 --- a/tests/test_logs.py +++ b/tests/test_logs.py @@ -4,11 +4,12 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk import sentry_sdk.logger from sentry_sdk import get_client -from sentry_sdk.consts import SPANDATA, VERSION +from sentry_sdk.consts import VERSION def test_logs_enabled_by_default(sentry_init, capture_envelopes): @@ -204,13 +205,15 @@ def test_logs_attributes(sentry_init, capture_items): for k, v in attrs.items(): assert logs[0]["attributes"][k] == v - assert logs[0]["attributes"]["sentry.environment"] == "production" + assert logs[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT] == "production" if sentry_sdk.get_client().options.get("release") is not None: - assert "sentry.release" in logs[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_RELEASE in logs[0]["attributes"] assert logs[0]["attributes"]["sentry.message.parameter.my_var"] == "some value" - assert logs[0]["attributes"][SPANDATA.SERVER_ADDRESS] == "test-server" - assert logs[0]["attributes"]["sentry.sdk.name"].startswith("sentry.python") - assert logs[0]["attributes"]["sentry.sdk.version"] == VERSION + assert logs[0]["attributes"][ATTRIBUTE_NAMES.SERVER_ADDRESS] == "test-server" + assert logs[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SDK_NAME].startswith( + "sentry.python" + ) + assert logs[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SDK_VERSION] == VERSION def test_logs_message_params(sentry_init, capture_items): @@ -237,21 +240,21 @@ def test_logs_message_params(sentry_init, capture_items): assert logs[0]["body"] == "The recorded value was '1'" assert logs[0]["attributes"]["sentry.message.parameter.int_var"] == 1 assert ( - logs[0]["attributes"]["sentry.message.template"] + logs[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "The recorded value was '{int_var}'" ) assert logs[1]["body"] == "The recorded value was '2.0'" assert logs[1]["attributes"]["sentry.message.parameter.float_var"] == 2.0 assert ( - logs[1]["attributes"]["sentry.message.template"] + logs[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "The recorded value was '{float_var}'" ) assert logs[2]["body"] == "The recorded value was 'False'" assert logs[2]["attributes"]["sentry.message.parameter.bool_var"] is False assert ( - logs[2]["attributes"]["sentry.message.template"] + logs[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "The recorded value was '{bool_var}'" ) @@ -261,7 +264,7 @@ def test_logs_message_params(sentry_init, capture_items): == "some string value" ) assert ( - logs[3]["attributes"]["sentry.message.template"] + logs[3]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "The recorded value was '{string_var}'" ) @@ -271,12 +274,12 @@ def test_logs_message_params(sentry_init, capture_items): == "Exception('some error')" ) assert ( - logs[4]["attributes"]["sentry.message.template"] + logs[4]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == "The recorded error was '{error}'" ) assert logs[5]["body"] == "The recorded value was hardcoded." - assert "sentry.message.template" not in logs[5]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE not in logs[5]["attributes"] def test_logs_tied_to_segments(sentry_init, capture_items): @@ -363,9 +366,9 @@ def test_log_user_attributes(sentry_init, capture_items): # Check that all expected user attributes are present. assert log["attributes"].items() >= { - ("user.id", "1"), - ("user.email", "test@example.com"), - ("user.name", "test"), + (ATTRIBUTE_NAMES.USER_ID, "1"), + (ATTRIBUTE_NAMES.USER_EMAIL, "test@example.com"), + (ATTRIBUTE_NAMES.USER_NAME, "test"), } @@ -383,9 +386,9 @@ def test_log_no_user_attributes_if_no_pii(sentry_init, capture_items): logs = [item.payload for item in items] (log,) = logs - assert "user.id" not in log["attributes"] - assert "user.email" not in log["attributes"] - assert "user.name" not in log["attributes"] + assert ATTRIBUTE_NAMES.USER_ID not in log["attributes"] + assert ATTRIBUTE_NAMES.USER_EMAIL not in log["attributes"] + assert ATTRIBUTE_NAMES.USER_NAME not in log["attributes"] def test_auto_flush_logs_after_5s(sentry_init, capture_envelopes): @@ -448,9 +451,9 @@ def test_logs_with_literal_braces( # Verify template is only stored when there are parameters if params: - assert logs[0]["attributes"]["sentry.message.template"] == message + assert logs[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE] == message else: - assert "sentry.message.template" not in logs[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE not in logs[0]["attributes"] def test_transport_format(sentry_init, capture_envelopes): @@ -481,19 +484,19 @@ def test_transport_format(sentry_init, capture_envelopes): "timestamp": mock.ANY, "trace_id": mock.ANY, "attributes": { - "sentry.environment": { + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: { "type": "string", "value": "production", }, - "sentry.release": { + ATTRIBUTE_NAMES.SENTRY_RELEASE: { "type": "string", "value": "1.0.0", }, - "sentry.sdk.name": { + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: { "type": "string", "value": mock.ANY, }, - "sentry.sdk.version": { + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: { "type": "string", "value": VERSION, }, @@ -501,11 +504,11 @@ def test_transport_format(sentry_init, capture_envelopes): "type": "integer", "value": 13, }, - "process.runtime.name": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: { "type": "string", "value": mock.ANY, }, - "process.runtime.version": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: { "type": "string", "value": mock.ANY, }, @@ -513,7 +516,7 @@ def test_transport_format(sentry_init, capture_envelopes): "type": "string", "value": "warn", }, - "server.address": { + ATTRIBUTE_NAMES.SERVER_ADDRESS: { "type": "string", "value": "test-server", }, @@ -566,19 +569,19 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1): "timestamp": mock.ANY, "trace_id": mock.ANY, "attributes": { - "sentry.environment": { + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: { "type": "string", "value": "production", }, - "sentry.release": { + ATTRIBUTE_NAMES.SENTRY_RELEASE: { "type": "string", "value": "1.0.0", }, - "sentry.sdk.name": { + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: { "type": "string", "value": mock.ANY, }, - "sentry.sdk.version": { + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: { "type": "string", "value": VERSION, }, @@ -586,11 +589,11 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1): "type": "integer", "value": 9, }, - "process.runtime.name": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: { "type": "string", "value": mock.ANY, }, - "process.runtime.version": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: { "type": "string", "value": mock.ANY, }, @@ -598,7 +601,7 @@ def record_lost_event(reason, data_category=None, item=None, *, quantity=1): "type": "string", "value": "info", }, - "server.address": { + ATTRIBUTE_NAMES.SERVER_ADDRESS: { "type": "string", "value": "test-server", }, diff --git a/tests/test_metrics.py b/tests/test_metrics.py index efc458c32a..8ed9244985 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -3,10 +3,11 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import get_client -from sentry_sdk.consts import SPANDATA, VERSION +from sentry_sdk.consts import VERSION def test_metrics_basics(sentry_init, capture_items): @@ -26,8 +27,8 @@ def test_metrics_basics(sentry_init, capture_items): assert metrics[0]["type"] == "counter" assert metrics[0]["value"] == 1.0 assert "unit" not in metrics[0] - assert "sentry.sdk.name" in metrics[0]["attributes"] - assert "sentry.sdk.version" in metrics[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_SDK_NAME in metrics[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_SDK_VERSION in metrics[0]["attributes"] assert metrics[1]["name"] == "test.gauge" assert metrics[1]["type"] == "gauge" @@ -55,12 +56,14 @@ def test_metrics_with_attributes(sentry_init, capture_items): assert metrics[0]["attributes"]["endpoint"] == "/api/test" assert metrics[0]["attributes"]["status"] == "success" - assert metrics[0]["attributes"]["sentry.release"] == "1.0.0" - assert metrics[0]["attributes"]["sentry.environment"] == "test" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_RELEASE] == "1.0.0" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT] == "test" - assert metrics[0]["attributes"][SPANDATA.SERVER_ADDRESS] == "test-server" - assert metrics[0]["attributes"]["sentry.sdk.name"].startswith("sentry.python") - assert metrics[0]["attributes"]["sentry.sdk.version"] == VERSION + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.SERVER_ADDRESS] == "test-server" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SDK_NAME].startswith( + "sentry.python" + ) + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_SDK_VERSION] == VERSION def test_metrics_with_user(sentry_init, capture_items): @@ -77,9 +80,9 @@ def test_metrics_with_user(sentry_init, capture_items): metrics = [item.payload for item in items] assert len(metrics) == 1 - assert metrics[0]["attributes"]["user.id"] == "user-123" - assert metrics[0]["attributes"]["user.email"] == "test@example.com" - assert metrics[0]["attributes"]["user.name"] == "testuser" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.USER_ID] == "user-123" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "test@example.com" + assert metrics[0]["attributes"][ATTRIBUTE_NAMES.USER_NAME] == "testuser" def test_metrics_no_user_if_user_info_off(sentry_init, capture_items): @@ -96,9 +99,9 @@ def test_metrics_no_user_if_user_info_off(sentry_init, capture_items): metrics = [item.payload for item in items] assert len(metrics) == 1 - assert "user.id" not in metrics[0]["attributes"] - assert "user.email" not in metrics[0]["attributes"] - assert "user.name" not in metrics[0]["attributes"] + assert ATTRIBUTE_NAMES.USER_ID not in metrics[0]["attributes"] + assert ATTRIBUTE_NAMES.USER_EMAIL not in metrics[0]["attributes"] + assert ATTRIBUTE_NAMES.USER_NAME not in metrics[0]["attributes"] def test_metrics_with_span(sentry_init, capture_items): @@ -282,31 +285,31 @@ def test_transport_format(sentry_init, capture_envelopes): "timestamp": mock.ANY, "trace_id": mock.ANY, "attributes": { - "sentry.environment": { + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: { "type": "string", "value": "production", }, - "sentry.release": { + ATTRIBUTE_NAMES.SENTRY_RELEASE: { "type": "string", "value": "1.0.0", }, - "process.runtime.name": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: { "type": "string", "value": mock.ANY, }, - "process.runtime.version": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: { "type": "string", "value": mock.ANY, }, - "sentry.sdk.name": { + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: { "type": "string", "value": mock.ANY, }, - "sentry.sdk.version": { + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: { "type": "string", "value": VERSION, }, - "server.address": { + ATTRIBUTE_NAMES.SERVER_ADDRESS: { "type": "string", "value": "test-server", }, diff --git a/tests/test_scope.py b/tests/test_scope.py index 1643206736..2cc549c4c9 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -3,6 +3,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import ( @@ -99,8 +100,8 @@ def test_set_user_none_values_are_dropped_when_copying_to_attributes( segment = items[0].payload - assert "user.name" not in segment["attributes"] - assert segment["attributes"]["user.email"] == "ada@beans.com" + assert ATTRIBUTE_NAMES.USER_NAME not in segment["attributes"] + assert segment["attributes"][ATTRIBUTE_NAMES.USER_EMAIL] == "ada@beans.com" def test_merging(sentry_init, capture_events): diff --git a/tests/test_transport.py b/tests/test_transport.py index e6d18fbbe6..63f684d3f0 100644 --- a/tests/test_transport.py +++ b/tests/test_transport.py @@ -25,6 +25,8 @@ running_under_gevent, reason="Async tests not compatible with gevent" ) +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk import ( Client, @@ -711,19 +713,19 @@ def test_complex_limits_without_data_category( ).timestamp(), "trace_id": "00000000-0000-0000-0000-000000000000", "attributes": { - "sentry.environment": { + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: { "value": "production", "type": "string", }, - "sentry.release": { + ATTRIBUTE_NAMES.SENTRY_RELEASE: { "value": "1.0.0", "type": "string", }, - "sentry.sdk.name": { + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: { "value": "sentry.python", "type": "string", }, - "sentry.sdk.version": { + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: { "value": "2.45.0", "type": "string", }, @@ -735,7 +737,7 @@ def test_complex_limits_without_data_category( "value": "info", "type": "string", }, - "server.address": { + ATTRIBUTE_NAMES.SERVER_ADDRESS: { "value": "test-server", "type": "string", }, @@ -753,27 +755,27 @@ def test_complex_limits_without_data_category( "value": 2.0, "type": "double", }, - "sentry.message.template": { + ATTRIBUTE_NAMES.SENTRY_MESSAGE_TEMPLATE: { "value": "The recorded value was '{float_var}'", "type": "string", }, - "sentry.sdk.name": { + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: { "value": "sentry.python", "type": "string", }, - "sentry.sdk.version": { + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: { "value": "2.45.0", "type": "string", }, - "server.address": { + ATTRIBUTE_NAMES.SERVER_ADDRESS: { "value": "test-server", "type": "string", }, - "sentry.environment": { + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: { "value": "production", "type": "string", }, - "sentry.release": { + ATTRIBUTE_NAMES.SENTRY_RELEASE: { "value": "1.0.0", "type": "string", }, diff --git a/tests/tracing/test_conversation_id.py b/tests/tracing/test_conversation_id.py index 72cc553e08..150f672c34 100644 --- a/tests/tracing/test_conversation_id.py +++ b/tests/tracing/test_conversation_id.py @@ -1,3 +1,5 @@ +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk @@ -13,9 +15,9 @@ def test_conversation_id_propagates_to_span_with_gen_ai_operation_name( with sentry_sdk.start_span(name="test-sg"): with sentry_sdk.start_span( - name="client", attributes={"sentry.op": "http.client"} + name="client", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "http.client"} ) as span: - span.set_attribute("gen_ai.operation.name", "chat") + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "chat") sentry_sdk.flush() @@ -23,8 +25,11 @@ def test_conversation_id_propagates_to_span_with_gen_ai_operation_name( span, segment, ) = [item.payload for item in items] - assert span["attributes"].get("gen_ai.conversation.id") == "conv-op-name-test" - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ( + span["attributes"].get(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID) + == "conv-op-name-test" + ) + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_propagates_to_span_with_ai_op(sentry_init, capture_items): @@ -37,7 +42,8 @@ def test_conversation_id_propagates_to_span_with_ai_op(sentry_init, capture_item with sentry_sdk.start_span(name="test-tx"): with sentry_sdk.start_span( - name="completion", attributes={"sentry.op": "ai.chat.completions"} + name="completion", + attributes={ATTRIBUTE_NAMES.SENTRY_OP: "ai.chat.completions"}, ): pass @@ -47,8 +53,11 @@ def test_conversation_id_propagates_to_span_with_ai_op(sentry_init, capture_item span, segment, ) = [item.payload for item in items] - assert span["attributes"].get("gen_ai.conversation.id") == "conv-ai-op-test" - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ( + span["attributes"].get(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID) + == "conv-ai-op-test" + ) + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_propagates_to_span_with_gen_ai_op( @@ -67,7 +76,7 @@ def test_conversation_id_propagates_to_span_with_gen_ai_op( with sentry_sdk.start_span(name="test-tx"): with sentry_sdk.start_span( - name="invoke", attributes={"sentry.op": "gen_ai.invoke_agent"} + name="invoke", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "gen_ai.invoke_agent"} ): pass @@ -75,8 +84,11 @@ def test_conversation_id_propagates_to_span_with_gen_ai_op( span, segment = [item.payload for item in items] - assert span["attributes"].get("gen_ai.conversation.id") == "conv-gen-ai-op-test" - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ( + span["attributes"].get(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID) + == "conv-gen-ai-op-test" + ) + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_not_propagated_to_non_ai_span(sentry_init, capture_items): @@ -89,7 +101,7 @@ def test_conversation_id_not_propagated_to_non_ai_span(sentry_init, capture_item with sentry_sdk.start_span(name="test-sg"): with sentry_sdk.start_span( - name="client", attributes={"sentry.op": "http.client"} + name="client", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "http.client"} ) as span: span.set_attribute("some.other.data", "value") @@ -99,8 +111,8 @@ def test_conversation_id_not_propagated_to_non_ai_span(sentry_init, capture_item span, segment, ) = [item.payload for item in items] - assert "gen_ai.conversation.id" not in span["attributes"] - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_not_propagated_when_not_set(sentry_init, capture_items): @@ -113,7 +125,8 @@ def test_conversation_id_not_propagated_when_not_set(sentry_init, capture_items) with sentry_sdk.start_span(name="test-sg"): with sentry_sdk.start_span( - name="completion", attributes={"sentry.op": "ai.chat.completions"} + name="completion", + attributes={ATTRIBUTE_NAMES.SENTRY_OP: "ai.chat.completions"}, ): pass @@ -123,8 +136,8 @@ def test_conversation_id_not_propagated_when_not_set(sentry_init, capture_items) span, segment, ) = [item.payload for item in items] - assert "gen_ai.conversation.id" not in span["attributes"] - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_not_propagated_to_span_without_op(sentry_init, capture_items): @@ -145,8 +158,8 @@ def test_conversation_id_not_propagated_to_span_without_op(sentry_init, capture_ span, segment, ) = [item.payload for item in items] - assert "gen_ai.conversation.id" not in span["attributes"] - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_propagates_with_gen_ai_operation_name_no_op( @@ -161,7 +174,7 @@ def test_conversation_id_propagates_with_gen_ai_operation_name_no_op( with sentry_sdk.start_span(name="test-sg"): with sentry_sdk.start_span(name="unnamed-span") as span: - span.set_attribute("gen_ai.operation.name", "embedding") + span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME, "embedding") sentry_sdk.flush() @@ -170,9 +183,10 @@ def test_conversation_id_propagates_with_gen_ai_operation_name_no_op( segment, ) = [item.payload for item in items] assert ( - span["attributes"].get("gen_ai.conversation.id") == "conv-no-op-but-data-test" + span["attributes"].get(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID) + == "conv-no-op-but-data-test" ) - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] def test_conversation_id_propagates_to_segment_with_ai_op(sentry_init, capture_items): @@ -184,14 +198,17 @@ def test_conversation_id_propagates_to_segment_with_ai_op(sentry_init, capture_i scope.set_conversation_id("conv-tx-ai-op-test") with sentry_sdk.start_span( - name="AI Workflow", attributes={"sentry.op": "ai.workflow"} + name="AI Workflow", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "ai.workflow"} ): pass sentry_sdk.flush() (segment,) = [item.payload for item in items] - assert segment["attributes"].get("gen_ai.conversation.id") == "conv-tx-ai-op-test" + assert ( + segment["attributes"].get(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID) + == "conv-tx-ai-op-test" + ) def test_conversation_id_not_propagated_to_non_ai_segment(sentry_init, capture_items): @@ -203,11 +220,11 @@ def test_conversation_id_not_propagated_to_non_ai_segment(sentry_init, capture_i scope.set_conversation_id("conv-tx-should-not-appear") with sentry_sdk.start_span( - name="HTTP Request", attributes={"sentry.op": "http.server"} + name="HTTP Request", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "http.server"} ): pass sentry_sdk.flush() (segment,) = [item.payload for item in items] - assert "gen_ai.conversation.id" not in segment["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID not in segment["attributes"] diff --git a/tests/tracing/test_decorator.py b/tests/tracing/test_decorator.py index 63635cefd3..fda5c11627 100644 --- a/tests/tracing/test_decorator.py +++ b/tests/tracing/test_decorator.py @@ -1,6 +1,7 @@ import inspect import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk @@ -36,7 +37,7 @@ def traced_function(): assert ( span["name"] == "test_decorator.test_trace_decorator..traced_function" ) - assert span["attributes"]["sentry.op"] == "function" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert span["status"] == "ok" @@ -62,7 +63,7 @@ def traced_function(): assert span["name"] == "traced" assert span["attributes"]["traced.attribute"] == 123 - assert span["attributes"]["sentry.op"] == "function" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert span["status"] == "ok" @@ -118,7 +119,7 @@ async def traced_function(): span["name"] == "test_decorator.test_trace_decorator_async..traced_function" ) - assert span["attributes"]["sentry.op"] == "function" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert span["status"] == "ok" @@ -145,7 +146,7 @@ async def traced_function(): assert span["name"] == "traced" assert span["attributes"]["traced.attribute"] == 123 - assert span["attributes"]["sentry.op"] == "function" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == "function" assert span["status"] == "ok" diff --git a/tests/tracing/test_integration_tests.py b/tests/tracing/test_integration_tests.py index 626bf5498a..72a0062bc6 100644 --- a/tests/tracing/test_integration_tests.py +++ b/tests/tracing/test_integration_tests.py @@ -5,6 +5,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import capture_message @@ -32,17 +33,26 @@ def test_basic(sentry_init, capture_items, sample_rate): span1, span2, parent_span = [item.payload for item in items] assert parent_span["name"] == "hi" - assert parent_span["attributes"]["sentry.segment.name.source"] == "custom" + assert ( + parent_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "custom" + ) assert span1["status"] == "error" assert span1["name"] == "foo" - assert span1["attributes"]["sentry.segment.id"] == parent_span["span_id"] - assert span1["attributes"]["sentry.segment.name"] == "hi" + assert ( + span1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID] + == parent_span["span_id"] + ) + assert span1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "hi" assert span2["status"] == "ok" assert span2["name"] == "bar" - assert span2["attributes"]["sentry.segment.id"] == parent_span["span_id"] - assert span2["attributes"]["sentry.segment.name"] == "hi" + assert ( + span2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID] + == parent_span["span_id"] + ) + assert span2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "hi" else: assert not items @@ -53,7 +63,9 @@ def test_error_event_linked_without_performance(sentry_init, capture_items): with sentry_sdk.start_span( name="no-op span", - attributes={"sentry.segment.name.source": "custom segment source"}, + attributes={ + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: "custom segment source" + }, ): sentry_sdk.capture_message("hi") @@ -159,7 +171,10 @@ def test_continue_trace(sentry_init, capture_items, parent_sampled, sample_rate) child_span_payload = spans[0].payload message_payload = messages[0].payload - assert child_span_payload["attributes"]["sentry.segment.name"] == "ho" + assert ( + child_span_payload["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] + == "ho" + ) assert ( child_span_payload["trace_id"] diff --git a/tests/tracing/test_misc.py b/tests/tracing/test_misc.py index 7c38828627..eb8ad49860 100644 --- a/tests/tracing/test_misc.py +++ b/tests/tracing/test_misc.py @@ -1,6 +1,7 @@ from unittest.mock import MagicMock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import MATCH_ALL @@ -121,7 +122,7 @@ def test_start_span_segment_updates_scope_name(sentry_init): scope = sentry_sdk.get_current_scope() with sentry_sdk.start_span( - name="foobar", attributes={"sentry.segment.name.source": "test"} + name="foobar", attributes={ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: "test"} ): assert scope._transaction == "foobar" assert scope._transaction_info == {"source": "test"} diff --git a/tests/tracing/test_span_origin.py b/tests/tracing/test_span_origin.py index 758de8b066..5752262220 100644 --- a/tests/tracing/test_span_origin.py +++ b/tests/tracing/test_span_origin.py @@ -1,3 +1,5 @@ +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk @@ -13,7 +15,7 @@ def test_span_origin_manual(sentry_init, capture_items): (span,) = [item.payload for item in items] assert len(items) == 1 - assert span["attributes"]["sentry.origin"] == "manual" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" def test_span_origin_custom(sentry_init, capture_items): @@ -21,12 +23,12 @@ def test_span_origin_custom(sentry_init, capture_items): items = capture_items("span") with sentry_sdk.start_span( - name="hi", attributes={"sentry.origin": "foo.foo2.foo3"} + name="hi", attributes={ATTRIBUTE_NAMES.SENTRY_ORIGIN: "foo.foo2.foo3"} ): pass with sentry_sdk.start_span( - name="ho", attributes={"sentry.origin": "baz.baz2.baz3"} + name="ho", attributes={ATTRIBUTE_NAMES.SENTRY_ORIGIN: "baz.baz2.baz3"} ): pass @@ -35,5 +37,5 @@ def test_span_origin_custom(sentry_init, capture_items): (span1, span2) = [item.payload for item in items] assert len(items) == 2 - assert span1["attributes"]["sentry.origin"] == "foo.foo2.foo3" - assert span2["attributes"]["sentry.origin"] == "baz.baz2.baz3" + assert span1["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "foo.foo2.foo3" + assert span2["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "baz.baz2.baz3" diff --git a/tests/tracing/test_span_streaming.py b/tests/tracing/test_span_streaming.py index 5e2ae35c8a..1fa9987d08 100644 --- a/tests/tracing/test_span_streaming.py +++ b/tests/tracing/test_span_streaming.py @@ -4,6 +4,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.profiler.continuous_profiler import get_profiler_id @@ -38,9 +39,9 @@ def test_start_span(sentry_init, capture_items): child, segment = spans assert segment["name"] == "segment" - assert segment["attributes"]["sentry.segment.name"] == "segment" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child["name"] == "child" - assert child["attributes"]["sentry.segment.name"] == "segment" + assert child["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert segment["is_segment"] is True assert "parent_span_id" not in segment @@ -77,9 +78,9 @@ def test_start_span_no_context_manager(sentry_init, capture_items): child, segment = spans assert segment["name"] == "segment" - assert segment["attributes"]["sentry.segment.name"] == "segment" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child["name"] == "child" - assert child["attributes"]["sentry.segment.name"] == "segment" + assert child["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert segment["is_segment"] is True assert child["is_segment"] is False @@ -449,7 +450,7 @@ def test_span_links(sentry_init, capture_envelopes): "trace_id": "627a2885119dcc8184fae7eef09438cb", "span_id": "6c71fc6b09b8b716", "sampled": True, - "attributes": {"sentry.link.type": "previous_trace"}, + "attributes": {ATTRIBUTE_NAMES.SENTRY_LINK_TYPE: "previous_trace"}, } with sentry_sdk.start_span(name="parent", links=[previous_trace]) as span: span.add_link({"trace_id": "a" * 32, "span_id": "b" * 16}) @@ -459,14 +460,17 @@ def test_span_links(sentry_init, capture_envelopes): "trace_id": "c" * 32, "span_id": "d" * 16, "sampled": False, - "attributes": {"messaging.message.id": "1", "retries": 2}, + "attributes": { + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID: "1", + "retries": 2, + }, } ] ) with sentry_sdk.start_span(name="child"): ... # span keeps its own copy. - previous_trace["attributes"]["sentry.link.type"] = "changed" + previous_trace["attributes"][ATTRIBUTE_NAMES.SENTRY_LINK_TYPE] = "changed" sentry_sdk.flush() child, span = envelopes[0].items[0].payload.json["items"] @@ -479,7 +483,10 @@ def test_span_links(sentry_init, capture_envelopes): "span_id": "6c71fc6b09b8b716", "sampled": True, "attributes": { - "sentry.link.type": {"type": "string", "value": "previous_trace"} + ATTRIBUTE_NAMES.SENTRY_LINK_TYPE: { + "type": "string", + "value": "previous_trace", + } }, }, {"trace_id": "a" * 32, "span_id": "b" * 16}, @@ -488,7 +495,7 @@ def test_span_links(sentry_init, capture_envelopes): "span_id": "d" * 16, "sampled": False, "attributes": { - "messaging.message.id": {"type": "string", "value": "1"}, + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID: {"type": "string", "value": "1"}, "retries": {"type": "integer", "value": 2}, }, }, @@ -611,17 +618,17 @@ def test_start_inactive_span(sentry_init, capture_items): assert segment["is_segment"] is True assert segment["name"] == "segment" - assert segment["attributes"]["sentry.segment.name"] == "segment" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child1["is_segment"] is False assert child1["name"] == "child1" - assert child1["attributes"]["sentry.segment.name"] == "segment" + assert child1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child1["parent_span_id"] == segment["span_id"] assert child1["trace_id"] == segment["trace_id"] assert child2["is_segment"] is False assert child2["name"] == "child2" - assert child2["attributes"]["sentry.segment.name"] == "segment" + assert child2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child2["parent_span_id"] == segment["span_id"] assert child2["trace_id"] == segment["trace_id"] @@ -645,13 +652,13 @@ def test_start_span_override_parent(sentry_init, capture_items): child2, child1, segment = spans assert segment["name"] == "segment" - assert segment["attributes"]["sentry.segment.name"] == "segment" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child1["name"] == "child1" - assert child1["attributes"]["sentry.segment.name"] == "segment" + assert child1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert child2["name"] == "child2" - assert child2["attributes"]["sentry.segment.name"] == "segment" + assert child2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment" assert segment["is_segment"] is True @@ -684,12 +691,12 @@ def test_sibling_segments(sentry_init, capture_items): segment1, segment2 = spans assert segment1["name"] == "segment1" - assert segment1["attributes"]["sentry.segment.name"] == "segment1" + assert segment1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment1" assert segment1["is_segment"] is True assert "parent_span_id" not in segment1 assert segment2["name"] == "segment2" - assert segment2["attributes"]["sentry.segment.name"] == "segment2" + assert segment2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment2" assert segment2["is_segment"] is True assert "parent_span_id" not in segment2 @@ -717,12 +724,12 @@ def test_sibling_segments_new_trace(sentry_init, capture_items): assert len(spans) == 2 (segment1,) = (span for span in spans if span["name"] == "segment1") - assert segment1["attributes"]["sentry.segment.name"] == "segment1" + assert segment1["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment1" assert segment1["is_segment"] is True assert "parent_span_id" not in segment1 (segment2,) = (span for span in spans if span["name"] == "segment2") - assert segment2["attributes"]["sentry.segment.name"] == "segment2" + assert segment2["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "segment2" assert segment2["is_segment"] is True assert "parent_span_id" not in segment2 @@ -1671,7 +1678,7 @@ def test_segment_span_has_profiler_id( spans = [item.payload for item in items] assert len(spans) == 1 - assert "sentry.profiler_id" in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_PROFILER_ID in spans[0]["attributes"] profile_chunks = [ item @@ -1705,7 +1712,7 @@ def test_segment_span_no_profiler_id_when_unsampled( spans = [item.payload for item in items] assert len(spans) == 1 - assert "sentry.profiler_id" not in spans[0]["attributes"] + assert ATTRIBUTE_NAMES.SENTRY_PROFILER_ID not in spans[0]["attributes"] profile_chunks = [ item @@ -1768,27 +1775,30 @@ def test_default_attributes(sentry_init, capture_envelopes): "content_type": "application/vnd.sentry.items.span.v2+json", } assert item.payload.json["items"][0]["attributes"] == { - "thread.id": {"value": mock.ANY, "type": "string"}, - "thread.name": {"value": "MainThread", "type": "string"}, - "process.command_args": {"value": mock.ANY, "type": "array"}, - "sentry.segment.id": {"value": mock.ANY, "type": "string"}, - "sentry.segment.name": {"value": "test", "type": "string"}, - "sentry.sdk.name": {"value": "sentry.python", "type": "string"}, - "sentry.sdk.version": {"value": mock.ANY, "type": "string"}, - "server.address": {"value": "test-server", "type": "string"}, - "sentry.environment": {"value": "production", "type": "string"}, - "sentry.platform": {"value": "python", "type": "string"}, - "sentry.release": {"value": "1.0.0", "type": "string"}, - "sentry.dist": {"value": "1.0", "type": "string"}, - "sentry.origin": {"value": "manual", "type": "string"}, - "sentry.sdk.integrations": {"value": mock.ANY, "type": "array"}, - "sentry.trace_lifecycle": {"value": "stream", "type": "string"}, - "sentry.segment.name.source": {"value": "custom", "type": "string"}, - "process.runtime.name": { + ATTRIBUTE_NAMES.THREAD_ID: {"value": mock.ANY, "type": "string"}, + ATTRIBUTE_NAMES.THREAD_NAME: {"value": "MainThread", "type": "string"}, + ATTRIBUTE_NAMES.PROCESS_COMMAND_ARGS: {"value": mock.ANY, "type": "array"}, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_ID: {"value": mock.ANY, "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME: {"value": "test", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_SDK_NAME: {"value": "sentry.python", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_SDK_VERSION: {"value": mock.ANY, "type": "string"}, + ATTRIBUTE_NAMES.SERVER_ADDRESS: {"value": "test-server", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_ENVIRONMENT: {"value": "production", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_PLATFORM: {"value": "python", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_RELEASE: {"value": "1.0.0", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_DIST: {"value": "1.0", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: {"value": "manual", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_SDK_INTEGRATIONS: {"value": mock.ANY, "type": "array"}, + ATTRIBUTE_NAMES.SENTRY_TRACE_LIFECYCLE: {"value": "stream", "type": "string"}, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: { + "value": "custom", + "type": "string", + }, + ATTRIBUTE_NAMES.PROCESS_RUNTIME_NAME: { "type": "string", "value": mock.ANY, }, - "process.runtime.version": { + ATTRIBUTE_NAMES.PROCESS_RUNTIME_VERSION: { "type": "string", "value": mock.ANY, },