diff --git a/sentry_sdk/integrations/arq.py b/sentry_sdk/integrations/arq.py index 6901e9136c..399b35caf3 100644 --- a/sentry_sdk/integrations/arq.py +++ b/sentry_sdk/integrations/arq.py @@ -2,8 +2,10 @@ import time from functools import wraps +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.crons import monitor from sentry_sdk.crons.utils import _get_timezone_name from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -95,8 +97,8 @@ async def _sentry_enqueue_job( with sentry_sdk.start_span( name=function, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": ArqIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ArqIntegration.origin, }, ): return await old_enqueue_job(self, function, *args, **kwargs) @@ -120,16 +122,16 @@ async def _sentry_run_job(self: "Worker", job_id: str, score: int) -> None: with sentry_sdk.start_span( name="unknown arq task", attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": ArqIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK, - SPANDATA.MESSAGING_MESSAGE_ID: job_id, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: ArqIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK, + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID: job_id, }, parent_span=None, ) as span: if self.queue_name is not None: span.set_attribute( - SPANDATA.MESSAGING_DESTINATION_NAME, self.queue_name + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME, self.queue_name ) return await old_run_job(self, job_id, score) diff --git a/sentry_sdk/integrations/celery/__init__.py b/sentry_sdk/integrations/celery/__init__.py index 2ccf40bd86..577470bc49 100644 --- a/sentry_sdk/integrations/celery/__init__.py +++ b/sentry_sdk/integrations/celery/__init__.py @@ -4,9 +4,11 @@ from functools import wraps from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk import isolation_scope -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.integrations.celery.beat import ( _patch_beat_apply_entry, @@ -271,8 +273,8 @@ def apply_async(*args: "Any", **kwargs: "Any") -> "Any": span = sentry_sdk.start_span( name=task_name, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": CeleryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CeleryIntegration.origin, }, ) @@ -331,9 +333,9 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any": name=task_name, parent_span=None, # make this a segment attributes={ - "sentry.origin": CeleryIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK.value, - "sentry.op": OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CeleryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK.value, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, }, ) @@ -352,7 +354,9 @@ def _set_messaging_destination_name(task: "Any", span: "Span") -> None: if delivery_info.get("exchange") == "" and routing_key is not None: # Empty exchange indicates the default exchange, meaning the tasks # are sent to the queue with the same name as the routing key. - span.set_attribute(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) + span.set_attribute( + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME, routing_key + ) def _wrap_task_call(task: "Any", f: "F") -> "F": @@ -372,8 +376,8 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=task.name, attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": CeleryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CeleryIntegration.origin, }, ) as span: _set_messaging_destination_name(task, span) @@ -391,22 +395,24 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any": if latency is not None: latency *= 1000 # milliseconds span.set_attribute( - SPANDATA.MESSAGING_MESSAGE_RECEIVE_LATENCY, latency + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RECEIVE_LATENCY, latency ) with capture_internal_exceptions(): - span.set_attribute(SPANDATA.MESSAGING_MESSAGE_ID, task.request.id) + span.set_attribute( + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID, task.request.id + ) with capture_internal_exceptions(): span.set_attribute( - SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT, task.request.retries, ) with capture_internal_exceptions(): with task.app.connection() as conn: span.set_attribute( - SPANDATA.MESSAGING_SYSTEM, + ATTRIBUTE_NAMES.MESSAGING_SYSTEM, conn.transport.driver_type, ) @@ -502,24 +508,29 @@ def sentry_publish(self: "Producer", *args: "Any", **kwargs: "Any") -> "Any": with sentry_sdk.start_span( name=task_name, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": CeleryIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: CeleryIntegration.origin, }, ) as span: if task_id is not None: - span.set_attribute(SPANDATA.MESSAGING_MESSAGE_ID, task_id) + span.set_attribute(ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID, task_id) if exchange == "" and routing_key is not None: # Empty exchange indicates the default exchange, meaning messages are # routed to the queue with the same name as the routing key. - span.set_attribute(SPANDATA.MESSAGING_DESTINATION_NAME, routing_key) + span.set_attribute( + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME, routing_key + ) if retries is not None: - span.set_attribute(SPANDATA.MESSAGING_MESSAGE_RETRY_COUNT, retries) + span.set_attribute( + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT, retries + ) with capture_internal_exceptions(): span.set_attribute( - SPANDATA.MESSAGING_SYSTEM, self.connection.transport.driver_type + ATTRIBUTE_NAMES.MESSAGING_SYSTEM, + self.connection.transport.driver_type, ) return original_publish(self, *args, **kwargs) diff --git a/sentry_sdk/integrations/dramatiq.py b/sentry_sdk/integrations/dramatiq.py index 9fa625f9c8..a1112725f1 100644 --- a/sentry_sdk/integrations/dramatiq.py +++ b/sentry_sdk/integrations/dramatiq.py @@ -1,9 +1,11 @@ import json from typing import TypeVar +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.api import get_baggage, get_traceparent -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.integrations._wsgi_common import request_body_within_bounds from sentry_sdk.traces import ( @@ -141,10 +143,10 @@ def before_process_message(self, broker: "Broker", message: "Message[R]") -> Non span = sentry_sdk.start_span( name=message.actor_name, attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": DramatiqIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK.value, - SPANDATA.MESSAGING_DESTINATION_NAME: message.queue_name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: DramatiqIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK.value, + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME: message.queue_name, }, parent_span=None, ) diff --git a/sentry_sdk/integrations/huey.py b/sentry_sdk/integrations/huey.py index 7367b4f1cf..dea940300d 100644 --- a/sentry_sdk/integrations/huey.py +++ b/sentry_sdk/integrations/huey.py @@ -3,9 +3,11 @@ from functools import wraps from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.api import get_baggage, get_traceparent -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.crons import monitor from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.traces import ( @@ -105,9 +107,9 @@ def _sentry_enqueue( with sentry_sdk.start_span( name=span_name, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": HueyIntegration.origin, - SPANDATA.MESSAGING_DESTINATION_NAME: self.name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: HueyIntegration.origin, + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME: self.name, }, ): if not isinstance(item, no_headers_types): @@ -203,13 +205,15 @@ def _sentry_execute( span_ctx = sentry_sdk.start_span( name=task.name, attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": HueyIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK, - SPANDATA.MESSAGING_DESTINATION_NAME: self.name, - "messaging.message.id": task.id, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: HueyIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK, + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME: self.name, + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID: task.id, "messaging.message.system": "huey", - "messaging.message.retry.count": (task.default_retries or 0) + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT: ( + task.default_retries or 0 + ) - task.retries, }, parent_span=None, diff --git a/sentry_sdk/integrations/mcp.py b/sentry_sdk/integrations/mcp.py index f847a73941..0cbdc52718 100644 --- a/sentry_sdk/integrations/mcp.py +++ b/sentry_sdk/integrations/mcp.py @@ -12,6 +12,8 @@ from functools import wraps from typing import TYPE_CHECKING +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -154,14 +156,14 @@ def _set_span_input_data( # Set handler identifier span.set_attribute(span_data_key, handler_name) - span.set_attribute(SPANDATA.MCP_METHOD_NAME, mcp_method_name) + span.set_attribute(ATTRIBUTE_NAMES.MCP_METHOD_NAME, mcp_method_name) # Set transport/MCP transport type span.set_attribute( - SPANDATA.NETWORK_TRANSPORT, + ATTRIBUTE_NAMES.NETWORK_TRANSPORT, "pipe" if mcp_transport == "stdio" else "tcp", ) - span.set_attribute(SPANDATA.MCP_TRANSPORT, mcp_transport) + span.set_attribute(ATTRIBUTE_NAMES.MCP_TRANSPORT, mcp_transport) # Set request_id if provided if request_id: @@ -169,7 +171,7 @@ def _set_span_input_data( # Set session_id if provided if session_id: - span.set_attribute(SPANDATA.MCP_SESSION_ID, session_id) + span.set_attribute(ATTRIBUTE_NAMES.MCP_SESSION_ID, session_id) # Set request arguments (excluding common request context objects) for k, v in arguments.items(): @@ -252,15 +254,15 @@ async def _instrument_tool_call( with _active_http_scopes(ctx=ctx), sentry_sdk.start_span( name=f"tools/call {handler_name}", attributes={ - "sentry.op": OP.MCP_SERVER, - "sentry.origin": MCPIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MCP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: MCPIntegration.origin, }, ) as span: # Set input span data _set_span_input_data( span, handler_name, - SPANDATA.GEN_AI_TOOL_NAME, + ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME, "tools/call", arguments, request_id, @@ -295,7 +297,7 @@ async def _instrument_tool_call( return result span.set_attribute( - SPANDATA.GEN_AI_TOOL_CALL_RESULT, + ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT, safe_serialize(result_content), ) @@ -328,15 +330,15 @@ async def _instrument_prompt_get( with _active_http_scopes(ctx=ctx), sentry_sdk.start_span( name=f"prompts/get {handler_name}", attributes={ - "sentry.op": OP.MCP_SERVER, - "sentry.origin": MCPIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MCP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: MCPIntegration.origin, }, ) as span: # Set input span data _set_span_input_data( span, handler_name, - SPANDATA.GEN_AI_PROMPT_NAME, + ATTRIBUTE_NAMES.GEN_AI_PROMPT_NAME, "prompts/get", arguments, request_id, @@ -370,7 +372,7 @@ async def _instrument_prompt_get( # Always set message count if we found messages if message_count > 0: span.set_attribute( - SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT, message_count + ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT, message_count ) if ( @@ -387,7 +389,7 @@ async def _instrument_prompt_get( role = first_message["role"] if role: - span.set_attribute(SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE, role) + span.set_attribute(ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE, role) content_text = None if "content" in first_message: @@ -397,7 +399,7 @@ async def _instrument_prompt_get( if content_text: span.set_attribute( - SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT, + ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT, content_text, ) except Exception: @@ -427,15 +429,15 @@ async def _instrument_resource_read( with _active_http_scopes(ctx=ctx), sentry_sdk.start_span( name=f"resources/read {handler_name}", attributes={ - "sentry.op": OP.MCP_SERVER, - "sentry.origin": MCPIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.MCP_SERVER, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: MCPIntegration.origin, }, ) as span: # Set input span data _set_span_input_data( span, handler_name, - SPANDATA.MCP_RESOURCE_URI, + ATTRIBUTE_NAMES.MCP_RESOURCE_URI, "resources/read", {}, request_id, @@ -447,7 +449,7 @@ async def _instrument_resource_read( if handler_name and "://" in handler_name: protocol = handler_name.split("://")[0] if protocol: - span.set_attribute(SPANDATA.MCP_RESOURCE_PROTOCOL, protocol) + span.set_attribute(ATTRIBUTE_NAMES.MCP_RESOURCE_PROTOCOL, protocol) try: result = await call_next(ctx) diff --git a/sentry_sdk/integrations/ray.py b/sentry_sdk/integrations/ray.py index ec7cb433b8..08e2f81fa9 100644 --- a/sentry_sdk/integrations/ray.py +++ b/sentry_sdk/integrations/ray.py @@ -2,6 +2,8 @@ import inspect import sys +from sentry_conventions.attributes import ATTRIBUTE_NAMES + import sentry_sdk from sentry_sdk.consts import OP from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -95,9 +97,9 @@ def new_func( with sentry_sdk.start_span( name="unknown Ray task" if function_name is None else function_name, attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": RayIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: RayIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK, }, parent_span=None, ): @@ -145,8 +147,8 @@ def _remote_method_with_header_propagation( with sentry_sdk.start_span( name="unknown Ray task" if function_name is None else function_name, attributes={ - "sentry.op": OP.QUEUE_PUBLISH, - "sentry.origin": RayIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PUBLISH, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: RayIntegration.origin, }, ): tracing = { diff --git a/sentry_sdk/integrations/rq.py b/sentry_sdk/integrations/rq.py index fd9de63135..2c9c145e93 100644 --- a/sentry_sdk/integrations/rq.py +++ b/sentry_sdk/integrations/rq.py @@ -3,8 +3,10 @@ import time import weakref +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.crons import MonitorStatus, capture_checkin from sentry_sdk.crons.utils import _get_interval_schedule from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version @@ -113,16 +115,18 @@ def sentry_patched_perform_job( with sentry_sdk.start_span( name="unknown RQ task" if func_name is None else func_name, attributes={ - "sentry.op": OP.QUEUE_PROCESS, - "sentry.origin": RqIntegration.origin, - "sentry.segment.name.source": SegmentNameSource.TASK, - SPANDATA.MESSAGING_MESSAGE_ID: job.id, - SPANDATA.MESSAGING_DESTINATION_NAME: queue.name, + ATTRIBUTE_NAMES.SENTRY_OP: OP.QUEUE_PROCESS, + ATTRIBUTE_NAMES.SENTRY_ORIGIN: RqIntegration.origin, + ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE: SegmentNameSource.TASK, + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID: job.id, + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME: queue.name, }, parent_span=None, ) as span: if func_name is not None: - span.set_attribute(SPANDATA.CODE_FUNCTION_NAME, func_name) + span.set_attribute( + ATTRIBUTE_NAMES.CODE_FUNCTION_NAME, func_name + ) rv = old_perform_job(self, job, queue, *args, **kwargs) diff --git a/tests/integrations/arq/test_arq.py b/tests/integrations/arq/test_arq.py index 8c0908746c..76fcc5f348 100644 --- a/tests/integrations/arq/test_arq.py +++ b/tests/integrations/arq/test_arq.py @@ -8,10 +8,11 @@ from arq.jobs import Job from arq.utils import timestamp_ms from fakeredis.aioredis import FakeRedis +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk import get_client -from sentry_sdk.consts import OP, SPANDATA +from sentry_sdk.consts import OP from sentry_sdk.integrations.arq import ArqIntegration from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES @@ -247,7 +248,7 @@ async def retry_job(ctx): # (queue.publish) span is created for it; only the consumer segment # is emitted. The consumer segment is preceded by the redis spans for # the re-enqueue, so it lands at index 2. - assert spans[2]["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert spans[2]["status"] == "ok" assert spans[2]["name"] == "retry_job" @@ -256,7 +257,7 @@ async def retry_job(ctx): sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[5]["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert spans[5]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert spans[5]["status"] == "ok" assert spans[5]["name"] == "retry_job" @@ -329,13 +330,16 @@ async def division(_, a, b=0): task_spans = [ span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PROCESS + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ] division_span = next(span for span in task_spans if span["name"] == "division") - assert division_span["attributes"]["sentry.segment.name.source"] == "task" assert ( - division_span["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] + division_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "task" + ) + assert ( + division_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] == worker.queue_name ) @@ -413,7 +417,7 @@ async def dummy_job(_): assert spans[2]["trace_id"] == span.trace_id assert spans[2]["span_id"] == span.span_id - assert spans[1]["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH assert spans[1]["name"] == "dummy_job" @@ -467,8 +471,8 @@ async def dummy_job(_): sentry_sdk.flush() spans = [item.payload for item in items] - assert spans[2]["attributes"]["sentry.origin"] == "manual" - assert spans[1]["attributes"]["sentry.origin"] == "auto.queue.arq" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.arq" @pytest.mark.asyncio @@ -500,10 +504,10 @@ async def job(ctx): # No producer (queue.publish) span is created for the re-enqueue # triggered by the retry, since it happens without an active span, so # the consumer segment lands at index 2. - assert spans[2]["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert spans[2]["attributes"]["sentry.origin"] == "auto.queue.arq" - assert spans[1]["attributes"]["sentry.origin"] == "auto.db.redis" - assert spans[0]["attributes"]["sentry.origin"] == "auto.db.redis" + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + assert spans[2]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.arq" + assert spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" + assert spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.db.redis" @pytest.mark.asyncio diff --git a/tests/integrations/celery/test_celery.py b/tests/integrations/celery/test_celery.py index 46904b9bea..23d92ad571 100644 --- a/tests/integrations/celery/test_celery.py +++ b/tests/integrations/celery/test_celery.py @@ -7,6 +7,7 @@ from celery import VERSION, Celery from celery.bin import worker from celery.exceptions import Ignore, Reject, Retry +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -289,7 +290,10 @@ def dummy_task(x, y): assert execution_span["name"] == "dummy_task" assert execution_span["is_segment"] is True - assert execution_span["attributes"]["sentry.segment.name.source"] == "task" + assert ( + execution_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] + == "task" + ) assert execution_span["trace_id"] == span.trace_id if task_fails: assert execution_span["status"] == "error" @@ -298,15 +302,17 @@ def dummy_task(x, y): assert process_span["name"] == "dummy_task" assert process_span["trace_id"] == span.trace_id - assert process_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert process_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert process_span["parent_span_id"] == execution_span["span_id"] assert submission_span["name"] == "submission" assert submission_span["is_segment"] is True assert submit_span["name"] == "dummy_task" - assert submit_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert submit_span["attributes"]["sentry.origin"] == "auto.queue.celery" + assert submit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + assert ( + submit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.celery" + ) assert submit_span["parent_span_id"] == submission_span["span_id"] == span.span_id assert submit_span["trace_id"] == span.trace_id @@ -435,7 +441,7 @@ def dummy_task(): assert not [item for item in items if item.type == "event"] process_span, execution_span = [item.payload for item in items] - assert process_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert process_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert process_span["status"] == "ok" assert execution_span["is_segment"] is True assert execution_span["status"] == "ok" @@ -672,7 +678,8 @@ def task(): ... process_span, _execution_span = items assert ( - process_span.payload["attributes"]["messaging.destination.name"] == routing_key + process_span.payload["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] + == routing_key ) @@ -701,7 +708,10 @@ def task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert "messaging.destination.name" not in process_span.payload["attributes"] + assert ( + ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME + not in process_span.payload["attributes"] + ) def test_messaging_id(init_celery, capture_items): @@ -719,7 +729,7 @@ def example_task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert "messaging.message.id" in process_span.payload["attributes"] + assert ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID in process_span.payload["attributes"] def test_retry_count_zero(init_celery, capture_items): @@ -737,7 +747,12 @@ def task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert process_span.payload["attributes"]["messaging.message.retry.count"] == 0 + assert ( + process_span.payload["attributes"][ + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT + ] + == 0 + ) @mock.patch("celery.app.task.Task.request") @@ -758,7 +773,12 @@ def task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert process_span.payload["attributes"]["messaging.message.retry.count"] == 3 + assert ( + process_span.payload["attributes"][ + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT + ] + == 3 + ) @pytest.mark.parametrize("system", ("redis", "amqp")) @@ -780,7 +800,9 @@ def task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert process_span.payload["attributes"]["messaging.system"] == system + assert ( + process_span.payload["attributes"][ATTRIBUTE_NAMES.MESSAGING_SYSTEM] == system + ) @pytest.mark.parametrize("system", ("amqp", "redis")) @@ -810,13 +832,20 @@ def task(): ... span_items = [item.payload for item in items] publish_span = next( - s for s in span_items if s["attributes"].get("sentry.op") == OP.QUEUE_PUBLISH + s + for s in span_items + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ) - assert publish_span["attributes"]["messaging.system"] == system - assert publish_span["attributes"]["messaging.destination.name"] == "celery" - assert "messaging.message.id" in publish_span["attributes"] - assert publish_span["attributes"]["messaging.message.retry.count"] == 0 + assert publish_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_SYSTEM] == system + assert ( + publish_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] + == "celery" + ) + assert ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID in publish_span["attributes"] + assert ( + publish_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT] == 0 + ) monkeypatch.setattr(kombu.messaging.Producer, "_publish", old_publish) @@ -836,8 +865,16 @@ def task(): ... sentry_sdk.flush() process_span, _execution_span = items - assert "messaging.message.receive.latency" in process_span.payload["attributes"] - assert process_span.payload["attributes"]["messaging.message.receive.latency"] > 0 + assert ( + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RECEIVE_LATENCY + in process_span.payload["attributes"] + ) + assert ( + process_span.payload["attributes"][ + ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RECEIVE_LATENCY + ] + > 0 + ) def tests_span_origin_consumer(init_celery, capture_items): @@ -856,8 +893,14 @@ def task(): ... sentry_sdk.flush() process_span, execution_span = items - assert execution_span.payload["attributes"]["sentry.origin"] == "auto.queue.celery" - assert process_span.payload["attributes"]["sentry.origin"] == "auto.queue.celery" + assert ( + execution_span.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.celery" + ) + assert ( + process_span.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.celery" + ) def tests_span_origin_producer(monkeypatch, sentry_init, capture_items): @@ -886,10 +929,13 @@ def task(): ... parent = items.pop(-1).payload assert parent["name"] == "custom parent" - assert parent["attributes"]["sentry.origin"] == "manual" + assert parent["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "manual" for item in items: - assert item.payload["attributes"]["sentry.origin"] == "auto.queue.celery" + assert ( + item.payload["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.celery" + ) monkeypatch.setattr(kombu.messaging.Producer, "_publish", old_publish) @@ -942,7 +988,7 @@ def test_send_task_wrapped( assert outer["is_segment"] is True assert submit_span["name"] == "very_creative_task_name" - assert submit_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH + assert submit_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH assert submit_span["trace_id"] == outer_span.trace_id assert submit_span["trace_id"] == kwargs["headers"]["sentry-trace"].split("-")[0] diff --git a/tests/integrations/dramatiq/test_dramatiq.py b/tests/integrations/dramatiq/test_dramatiq.py index ed644fe447..a30a71722c 100644 --- a/tests/integrations/dramatiq/test_dramatiq.py +++ b/tests/integrations/dramatiq/test_dramatiq.py @@ -4,9 +4,10 @@ import pytest from dramatiq.brokers.stub import StubBroker from dramatiq.middleware import Middleware, SkipMessage +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.dramatiq import DramatiqIntegration from sentry_sdk.integrations.logging import ignore_logger_for_events @@ -146,10 +147,10 @@ def dummy_actor(x, y): assert segment_item.type == "span" assert segment["name"] == "dummy_actor" assert segment["is_segment"] is True - assert segment["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert segment["attributes"]["sentry.segment.name.source"] == "task" + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + assert segment["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == "task" assert ( - segment["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] + segment["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] == dummy_actor.queue_name ) assert segment["status"] == ("error" if task_fails else "ok") @@ -181,7 +182,7 @@ def propagated_trace_task(): inner_segment, outer_segment = [i.payload for i in items] assert inner_segment["name"] == "propagated_trace_task" - assert inner_segment["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert inner_segment["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert inner_segment["trace_id"] == outer_span.trace_id assert outer_segment["name"] == "outer" diff --git a/tests/integrations/fastmcp/test_fastmcp.py b/tests/integrations/fastmcp/test_fastmcp.py index c5f8ffe1c1..33d4e6495b 100644 --- a/tests/integrations/fastmcp/test_fastmcp.py +++ b/tests/integrations/fastmcp/test_fastmcp.py @@ -20,6 +20,7 @@ from unittest import mock import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration @@ -161,15 +162,15 @@ def add_numbers(a: int, b: int) -> dict: # Verify span structure span = spans[0] - assert span["attributes"]["sentry.op"] == OP.MCP_SERVER - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "tools/call add_numbers" - assert span["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "add_numbers" - assert span["attributes"][SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert span["attributes"][SPANDATA.MCP_TRANSPORT] == "stdio" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "add_numbers" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" assert span["attributes"][SPANDATA.MCP_REQUEST_ID] == "req-123" - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT in span["attributes"] @pytest.mark.asyncio @@ -218,15 +219,15 @@ def add_numbers(a: int, b: int) -> dict: # Verify span structure span = spans[0] - assert span["attributes"]["sentry.op"] == OP.MCP_SERVER - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "tools/call add_numbers" - assert span["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "add_numbers" - assert span["attributes"][SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert span["attributes"][SPANDATA.MCP_TRANSPORT] == "stdio" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "add_numbers" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" assert span["attributes"][SPANDATA.MCP_REQUEST_ID] == "req-123" - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in span["attributes"] @pytest.mark.parametrize("FastMCP", fastmcp_implementations, ids=fastmcp_ids) @@ -285,21 +286,21 @@ async def multiply_numbers(x: int, y: int) -> dict: spans = [ span for span in spans - if span["attributes"].get("mcp.method.name") == "tools/call" + if span["attributes"].get(ATTRIBUTE_NAMES.MCP_METHOD_NAME) == "tools/call" ] assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.MCP_SERVER - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "tools/call multiply_numbers" - assert span["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "multiply_numbers" - assert span["attributes"][SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert span["attributes"][SPANDATA.MCP_TRANSPORT] == "http" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "multiply_numbers" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_TRANSPORT] == "http" assert span["attributes"][SPANDATA.MCP_REQUEST_ID] == "req-456" - assert span["attributes"][SPANDATA.MCP_SESSION_ID] == session_id + assert span["attributes"][ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT in span["attributes"] @pytest.mark.parametrize("FastMCP", fastmcp_implementations, ids=fastmcp_ids) @@ -360,21 +361,21 @@ async def multiply_numbers(x: int, y: int) -> dict: spans = [ span for span in spans - if span["attributes"].get("mcp.method.name") == "tools/call" + if span["attributes"].get(ATTRIBUTE_NAMES.MCP_METHOD_NAME) == "tools/call" ] assert len(spans) == 1 span = spans[0] - assert span["attributes"]["sentry.op"] == OP.MCP_SERVER - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "tools/call multiply_numbers" - assert span["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "multiply_numbers" - assert span["attributes"][SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert span["attributes"][SPANDATA.MCP_TRANSPORT] == "http" + assert span["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "multiply_numbers" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_TRANSPORT] == "http" assert span["attributes"][SPANDATA.MCP_REQUEST_ID] == "req-456" - assert span["attributes"][SPANDATA.MCP_SESSION_ID] == session_id + assert span["attributes"][ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in span["attributes"] + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in span["attributes"] @pytest.mark.asyncio @@ -414,7 +415,11 @@ def failing_tool(value: int) -> int: sentry_sdk.flush() # Check span was created spans = [item.payload for item in items if item.type == "span"] - tool_spans = [s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER] + tool_spans = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER + ] assert len(tool_spans) == 1 @@ -473,11 +478,17 @@ def get_user_data(user_id: int) -> dict: sentry_sdk.flush() # Verify span was created with complex data spans = [item.payload for item in items] - tool_spans = [s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER] + tool_spans = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER + ] assert len(tool_spans) == 1 - assert tool_spans[0]["attributes"]["sentry.op"] == OP.MCP_SERVER - assert tool_spans[0]["attributes"][SPANDATA.GEN_AI_TOOL_NAME] == "get_user_data" - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT in tool_spans[0]["attributes"] + assert tool_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert ( + tool_spans[0]["attributes"][ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "get_user_data" + ) + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT in tool_spans[0]["attributes"] # ============================================================================= @@ -541,15 +552,19 @@ def code_help_prompt(language: str): # Verify prompt span was created spans = [item.payload for item in items] prompt_spans = [ - s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER ] assert len(prompt_spans) == 1 span = prompt_spans[0] - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "prompts/get code_help_prompt" - assert span["attributes"][SPANDATA.GEN_AI_PROMPT_NAME] == "code_help_prompt" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROMPT_NAME] == "code_help_prompt" + ) - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT in span["attributes"] + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT in span["attributes"] @pytest.mark.asyncio @@ -608,15 +623,21 @@ def code_help_prompt(language: str): # Verify prompt span was created spans = [item.payload for item in items] prompt_spans = [ - s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER ] assert len(prompt_spans) == 1 span = prompt_spans[0] - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "prompts/get code_help_prompt" - assert span["attributes"][SPANDATA.GEN_AI_PROMPT_NAME] == "code_help_prompt" + assert ( + span["attributes"][ATTRIBUTE_NAMES.GEN_AI_PROMPT_NAME] == "code_help_prompt" + ) - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in span["attributes"] + assert ( + ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in span["attributes"] + ) # ============================================================================= @@ -668,13 +689,15 @@ def read_file(path: str): # Verify resource span was created spans = [item.payload for item in items] resource_spans = [ - s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER ] assert len(resource_spans) == 1 span = resource_spans[0] - assert span["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" assert span["name"] == "resources/read file:///test.txt" - assert span["attributes"][SPANDATA.MCP_RESOURCE_PROTOCOL] == "file" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_RESOURCE_PROTOCOL] == "file" @pytest.mark.parametrize("FastMCP", fastmcp_implementations, ids=fastmcp_ids) @@ -732,12 +755,13 @@ async def read_url(resource: str): spans = [ span for span in spans - if span["attributes"].get("mcp.method.name") == "resources/read" + if span["attributes"].get(ATTRIBUTE_NAMES.MCP_METHOD_NAME) + == "resources/read" ] assert len(spans) == 1 span = spans[0] - assert span["attributes"][SPANDATA.MCP_RESOURCE_PROTOCOL] == "https" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_RESOURCE_PROTOCOL] == "https" # ============================================================================= @@ -782,9 +806,13 @@ def test_tool(value: int) -> int: spans = [item.payload for item in items] # Verify MCP span has correct origin - mcp_spans = [s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER] + mcp_spans = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER + ] assert len(mcp_spans) == 1 - assert mcp_spans[0]["attributes"]["sentry.origin"] == "auto.ai.mcp" + assert mcp_spans[0]["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # ============================================================================= @@ -840,13 +868,13 @@ def http_tool(data: str) -> dict: spans = [ span for span in spans - if span["attributes"].get("mcp.method.name") == "tools/call" + if span["attributes"].get(ATTRIBUTE_NAMES.MCP_METHOD_NAME) == "tools/call" ] assert len(spans) == 1 span = spans[0] # Check that HTTP transport is detected - assert span["attributes"].get(SPANDATA.MCP_TRANSPORT) == "http" + assert span["attributes"].get(ATTRIBUTE_NAMES.MCP_TRANSPORT) == "http" @pytest.mark.asyncio @@ -884,10 +912,14 @@ def stdio_tool(n: int) -> dict: sentry_sdk.flush() # Find MCP spans spans = [item.payload for item in items] - mcp_spans = [s for s in spans if s["attributes"].get("sentry.op") == OP.MCP_SERVER] + mcp_spans = [ + s + for s in spans + if s["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.MCP_SERVER + ] assert len(mcp_spans) >= 1 span = mcp_spans[0] # Check that stdio transport is detected - assert span["attributes"].get(SPANDATA.MCP_TRANSPORT) == "stdio" + assert span["attributes"].get(ATTRIBUTE_NAMES.MCP_TRANSPORT) == "stdio" diff --git a/tests/integrations/huey/test_huey.py b/tests/integrations/huey/test_huey.py index 8ad45963be..16b7506a5f 100644 --- a/tests/integrations/huey/test_huey.py +++ b/tests/integrations/huey/test_huey.py @@ -4,9 +4,10 @@ from huey import __version__ as HUEY_VERSION from huey.api import MemoryHuey, Result from huey.exceptions import CancelExecution, RetryTask +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.huey import HueyIntegration from sentry_sdk.traces import SegmentNameSource, SpanStatus from sentry_sdk.utils import parse_version @@ -97,8 +98,11 @@ def division(a, b): (execute_span,) = payloads assert execute_span["is_segment"] - assert execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert execute_span["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] == huey.name + assert execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + assert ( + execute_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] + == huey.name + ) assert execute_span["name"] == "division" assert execute_span["status"] == (SpanStatus.ERROR if task_fails else SpanStatus.OK) @@ -126,10 +130,10 @@ def retry_task(context): re_enqueue_span, execute_span = payloads - assert re_enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH + assert re_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH assert not re_enqueue_span["is_segment"] - assert execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert execute_span["is_segment"] assert execute_span["name"] == "retry_task" assert execute_span["status"] == SpanStatus.OK @@ -168,7 +172,7 @@ def cancel_task(): assert len(payloads) == 1 (execute_span,) = payloads - assert execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert execute_span["is_segment"] assert execute_span["name"] == "cancel_task" assert execute_span["status"] == SpanStatus.OK @@ -199,7 +203,7 @@ def maybe_locked_task(): assert len(payloads) == 1 (execute_span,) = payloads - assert execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert execute_span["is_segment"] assert execute_span["name"] == "maybe_locked_task" @@ -261,15 +265,18 @@ def dummy_task(): enqueue_span = next( p for p in payloads - if p.get("attributes", {}).get("sentry.op") == OP.QUEUE_PUBLISH + if p.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ) segment_span = next(p for p in payloads if p.get("is_segment")) assert enqueue_span["trace_id"] == segment_span["trace_id"] assert enqueue_span["parent_span_id"] == segment_span["span_id"] assert enqueue_span["name"] == "different_task_name" - assert enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert enqueue_span["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] == huey.name + assert enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + assert ( + enqueue_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] + == huey.name + ) def test_huey_propagate_trace(init_huey, capture_items): @@ -292,7 +299,7 @@ def propagated_trace_task(): execute_span = next( p for p in payloads - if p.get("attributes", {}).get("sentry.op") == OP.QUEUE_PROCESS + if p.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) assert execute_span["name"] == "propagated_trace_task" @@ -318,10 +325,12 @@ def dummy_task(): enqueue_span = next( p for p in payloads - if p.get("attributes", {}).get("sentry.op") == OP.QUEUE_PUBLISH + if p.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PUBLISH ) - assert enqueue_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.huey" + ) def test_span_origin_consumer(init_huey, capture_items): @@ -342,10 +351,12 @@ def propagated_trace_task(): execute_span = next( p for p in payloads - if p.get("attributes", {}).get("sentry.op") == OP.QUEUE_PROCESS + if p.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) - assert execute_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.huey" + ) @pytest.mark.skipif(HUEY_VERSION < (3, 0), reason="group was added in 3.0") @@ -394,44 +405,76 @@ def task2(): assert group_span["parent_span_id"] == submission_span["span_id"] assert group_span["name"] == "Huey Task Group" assert group_span["status"] == "ok" - assert group_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert group_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert group_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + assert group_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.huey" assert task1_enqueue_span["name"] == "task1" assert task1_enqueue_span["status"] == "ok" assert task1_enqueue_span["parent_span_id"] == group_span["span_id"] - assert task1_enqueue_span["attributes"]["sentry.segment.name"] == "submission" - assert task1_enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert task1_enqueue_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] + == "submission" + ) + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + ) + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) assert task2_enqueue_span["name"] == "task2" assert task2_enqueue_span["status"] == "ok" assert task2_enqueue_span["parent_span_id"] == group_span["span_id"] - assert task2_enqueue_span["attributes"]["sentry.segment.name"] == "submission" - assert task2_enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert task2_enqueue_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] + == "submission" + ) + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + ) + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) assert task1_execute_span["name"] == "task1" assert task1_execute_span["status"] == "ok" assert task1_execute_span["attributes"]["messaging.message.system"] == "huey" assert task1_execute_span["parent_span_id"] == task1_enqueue_span["span_id"] - assert task1_execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert task1_execute_span["attributes"]["sentry.origin"] == "auto.queue.huey" assert ( - task1_execute_span["attributes"]["sentry.segment.name.source"] + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + ) + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == SegmentNameSource.TASK ) - assert task1_execute_span["attributes"]["messaging.message.id"] is not None - assert task1_execute_span["attributes"]["messaging.message.retry.count"] == 0 + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_MESSAGE_ID] + is not None + ) + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.MESSAGING_MESSAGE_RETRY_COUNT] + == 0 + ) assert task2_execute_span["name"] == "task2" assert task2_execute_span["status"] == "ok" assert task2_execute_span["parent_span_id"] == task2_enqueue_span["span_id"] assert task2_execute_span["attributes"]["messaging.message.system"] == "huey" - assert task2_execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert task2_execute_span["attributes"]["sentry.origin"] == "auto.queue.huey" assert ( - task2_execute_span["attributes"]["sentry.segment.name.source"] + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + ) + assert ( + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) + assert ( + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == SegmentNameSource.TASK ) @@ -481,41 +524,66 @@ def task2(results): assert chord_span["parent_span_id"] == submission_span["span_id"] assert chord_span["name"] == "Huey Chord" assert chord_span["status"] == "ok" - assert chord_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert chord_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert chord_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + assert chord_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.huey" assert task1_enqueue_span["name"] == "task1" assert task1_enqueue_span["status"] == "ok" assert task1_enqueue_span["parent_span_id"] == chord_span["span_id"] - assert task1_enqueue_span["attributes"]["sentry.segment.name"] == "submission" - assert task1_enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert task1_enqueue_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] + == "submission" + ) + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + ) + assert ( + task1_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) assert task1_execute_span["name"] == "task1" assert task1_execute_span["status"] == "ok" assert task1_execute_span["attributes"]["messaging.message.system"] == "huey" assert task1_execute_span["parent_span_id"] == task1_enqueue_span["span_id"] - assert task1_execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert task1_execute_span["attributes"]["sentry.origin"] == "auto.queue.huey" assert ( - task1_execute_span["attributes"]["sentry.segment.name.source"] + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + ) + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) + assert ( + task1_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == SegmentNameSource.TASK ) # chord callback (task2) is enqueued during task1's execution assert task2_enqueue_span["name"] == "task2" assert task2_enqueue_span["status"] == "ok" assert task2_enqueue_span["parent_span_id"] == task1_execute_span["span_id"] - assert task2_enqueue_span["attributes"]["sentry.segment.name"] == "task1" - assert task2_enqueue_span["attributes"]["sentry.op"] == OP.QUEUE_PUBLISH - assert task2_enqueue_span["attributes"]["sentry.origin"] == "auto.queue.huey" + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME] == "task1" + ) + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PUBLISH + ) + assert ( + task2_enqueue_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) assert task2_execute_span["name"] == "task2" assert task2_execute_span["status"] == "ok" assert task2_execute_span["parent_span_id"] == task2_enqueue_span["span_id"] assert task2_execute_span["attributes"]["messaging.message.system"] == "huey" - assert task2_execute_span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS - assert task2_execute_span["attributes"]["sentry.origin"] == "auto.queue.huey" assert ( - task2_execute_span["attributes"]["sentry.segment.name.source"] + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS + ) + assert ( + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] + == "auto.queue.huey" + ) + assert ( + task2_execute_span["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE] == SegmentNameSource.TASK ) diff --git a/tests/integrations/mcp/test_mcp.py b/tests/integrations/mcp/test_mcp.py index 311c13171a..3462ae4e25 100644 --- a/tests/integrations/mcp/test_mcp.py +++ b/tests/integrations/mcp/test_mcp.py @@ -22,6 +22,7 @@ import anyio import pytest +from sentry_conventions.attributes import ATTRIBUTE_NAMES from sentry_sdk.utils import package_version @@ -82,11 +83,11 @@ def _find_mcp_span(items, method_name=None): if item.type != "span": continue attrs = item.payload.get("attributes", {}) - if attrs.get("sentry.op") != OP.MCP_SERVER: + if attrs.get(ATTRIBUTE_NAMES.SENTRY_OP) != OP.MCP_SERVER: continue if ( method_name is not None - and attrs.get(SPANDATA.MCP_METHOD_NAME) != method_name + and attrs.get(ATTRIBUTE_NAMES.MCP_METHOD_NAME) != method_name ): continue return item.payload @@ -168,8 +169,8 @@ async def test_tool(ctx, params): assert span is not None data = span["attributes"] - assert data[SPANDATA.GEN_AI_TOOL_NAME] == "calculate" - assert data[SPANDATA.MCP_METHOD_NAME] == "tools/call" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "calculate" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" assert data[SPANDATA.MCP_REQUEST_ID] == "req-ctor" @@ -203,9 +204,9 @@ def add(a: int, b: int) -> int: sentry_sdk.flush() span = _find_mcp_span(items, method_name="tools/call") assert span is not None - assert span["attributes"]["sentry.op"] == OP.MCP_SERVER + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER assert span["name"] == "tools/call add" - assert span["attributes"][SPANDATA.MCP_METHOD_NAME] == "tools/call" + assert span["attributes"][ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" @pytest.mark.asyncio @@ -238,7 +239,8 @@ async def test_tool(ctx, params): item.payload for item in items if item.type == "span" - and item.payload.get("attributes", {}).get("sentry.op") == OP.MCP_SERVER + and item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.MCP_SERVER ] assert len(mcp_spans) == 1 @@ -298,20 +300,20 @@ async def test_tool(ctx, params): assert span is not None assert span["name"] == "tools/call calculate" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_TOOL_NAME] == "calculate" - assert data[SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert data[SPANDATA.MCP_TRANSPORT] == "stdio" - assert data[SPANDATA.NETWORK_TRANSPORT] == "pipe" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "calculate" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "pipe" assert data[SPANDATA.MCP_REQUEST_ID] == "req-123" - assert SPANDATA.MCP_SESSION_ID not in data + assert ATTRIBUTE_NAMES.MCP_SESSION_ID not in data assert data["mcp.request.argument.x"] == "10" assert data["mcp.request.argument.y"] == "5" - assert data[SPANDATA.GEN_AI_TOOL_CALL_RESULT] == json.dumps( + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT] == json.dumps( { "result": "success", "value": 42, @@ -373,18 +375,18 @@ async def test_tool(ctx, params): assert span is not None assert span["name"] == "tools/call calculate" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_TOOL_NAME] == "calculate" - assert data[SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert data[SPANDATA.MCP_TRANSPORT] == "stdio" - assert data[SPANDATA.NETWORK_TRANSPORT] == "pipe" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "calculate" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "pipe" assert data[SPANDATA.MCP_REQUEST_ID] == "req-123" - assert SPANDATA.MCP_SESSION_ID not in data + assert ATTRIBUTE_NAMES.MCP_SESSION_ID not in data - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in data + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in data @pytest.mark.asyncio @@ -447,19 +449,21 @@ async def test_tool_async(ctx, params): assert span is not None assert span["name"] == "tools/call process" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_TOOL_NAME] == "process" - assert data[SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert data[SPANDATA.MCP_TRANSPORT] == "http" - assert data[SPANDATA.NETWORK_TRANSPORT] == "tcp" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "process" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "http" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "tcp" assert data[SPANDATA.MCP_REQUEST_ID] == "req-456" - assert data[SPANDATA.MCP_SESSION_ID] == session_id + assert data[ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id assert data["mcp.request.argument.data"] == "test" - assert data[SPANDATA.GEN_AI_TOOL_CALL_RESULT] == json.dumps({"status": "completed"}) + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT] == json.dumps( + {"status": "completed"} + ) @pytest.mark.asyncio @@ -524,18 +528,18 @@ async def test_tool_async(ctx, params): assert span is not None assert span["name"] == "tools/call process" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_TOOL_NAME] == "process" - assert data[SPANDATA.MCP_METHOD_NAME] == "tools/call" - assert data[SPANDATA.MCP_TRANSPORT] == "http" - assert data[SPANDATA.NETWORK_TRANSPORT] == "tcp" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME] == "process" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "tools/call" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "http" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "tcp" assert data[SPANDATA.MCP_REQUEST_ID] == "req-456" - assert data[SPANDATA.MCP_SESSION_ID] == session_id + assert data[ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in data + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in data @pytest.mark.asyncio @@ -586,10 +590,10 @@ async def test_tool_async(ctx, params): assert span is not None data = span["attributes"] - assert data[SPANDATA.MCP_TRANSPORT] == "http" - assert data[SPANDATA.NETWORK_TRANSPORT] == "tcp" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "http" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "tcp" assert data[SPANDATA.MCP_REQUEST_ID] == "req-789" - assert SPANDATA.MCP_SESSION_ID not in data + assert ATTRIBUTE_NAMES.MCP_SESSION_ID not in data @pytest.mark.asyncio @@ -692,21 +696,24 @@ async def test_prompt(ctx, params): assert span is not None assert span["name"] == "prompts/get code_help" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_PROMPT_NAME] == "code_help" - assert data[SPANDATA.MCP_METHOD_NAME] == "prompts/get" - assert data[SPANDATA.MCP_TRANSPORT] == "stdio" + assert data[ATTRIBUTE_NAMES.GEN_AI_PROMPT_NAME] == "code_help" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "prompts/get" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" assert data[SPANDATA.MCP_REQUEST_ID] == "req-prompt" assert data["mcp.request.argument.language"] == "python" # Message count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE] == "user" - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT] == "Tell me about Python" + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE] == "user" + assert ( + data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT] + == "Tell me about Python" + ) @pytest.mark.asyncio @@ -765,20 +772,20 @@ async def test_prompt(ctx, params): assert span is not None assert span["name"] == "prompts/get code_help" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.GEN_AI_PROMPT_NAME] == "code_help" - assert data[SPANDATA.MCP_METHOD_NAME] == "prompts/get" - assert data[SPANDATA.MCP_TRANSPORT] == "stdio" + assert data[ATTRIBUTE_NAMES.GEN_AI_PROMPT_NAME] == "code_help" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "prompts/get" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" assert data[SPANDATA.MCP_REQUEST_ID] == "req-prompt" # Message count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data @pytest.mark.asyncio @@ -843,13 +850,13 @@ async def test_prompt_async(ctx, params): assert span is not None assert span["name"] == "prompts/get mcp_info" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # For multi-message prompts, count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 2 - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 2 + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data @pytest.mark.asyncio @@ -916,13 +923,13 @@ async def test_prompt_async(ctx, params): assert span is not None assert span["name"] == "prompts/get mcp_info" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # For multi-message prompts, count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 2 - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 2 + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data @pytest.mark.asyncio @@ -1008,17 +1015,17 @@ async def test_resource(ctx, params): assert span is not None assert span["name"] == "resources/read file:///path/to/file.txt" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" # Check span data - assert data[SPANDATA.MCP_RESOURCE_URI] == "file:///path/to/file.txt" - assert data[SPANDATA.MCP_METHOD_NAME] == "resources/read" - assert data[SPANDATA.MCP_TRANSPORT] == "stdio" + assert data[ATTRIBUTE_NAMES.MCP_RESOURCE_URI] == "file:///path/to/file.txt" + assert data[ATTRIBUTE_NAMES.MCP_METHOD_NAME] == "resources/read" + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "stdio" assert data[SPANDATA.MCP_REQUEST_ID] == "req-resource" - assert data[SPANDATA.MCP_RESOURCE_PROTOCOL] == "file" + assert data[ATTRIBUTE_NAMES.MCP_RESOURCE_PROTOCOL] == "file" # Resources don't capture result content - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in data + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in data @pytest.mark.asyncio @@ -1075,12 +1082,12 @@ async def test_resource_async(ctx, params): assert span is not None assert span["name"] == "resources/read https://example.com/resource" data = span["attributes"] - assert data["sentry.op"] == OP.MCP_SERVER - assert data["sentry.origin"] == "auto.ai.mcp" + assert data[ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER + assert data[ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.ai.mcp" - assert data[SPANDATA.MCP_RESOURCE_URI] == "https://example.com/resource" - assert data[SPANDATA.MCP_RESOURCE_PROTOCOL] == "https" - assert data[SPANDATA.MCP_SESSION_ID] == session_id + assert data[ATTRIBUTE_NAMES.MCP_RESOURCE_URI] == "https://example.com/resource" + assert data[ATTRIBUTE_NAMES.MCP_RESOURCE_PROTOCOL] == "https" + assert data[ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id @pytest.mark.asyncio @@ -1166,7 +1173,7 @@ async def test_tool_tuple(ctx, params): assert span is not None data = span["attributes"] - assert data[SPANDATA.GEN_AI_TOOL_CALL_RESULT] == json.dumps( + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT] == json.dumps( { "key": "value", "count": 5, @@ -1218,7 +1225,7 @@ async def test_tool_tuple(ctx, params): assert span is not None data = span["attributes"] - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in data + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in data @pytest.mark.asyncio @@ -1269,7 +1276,7 @@ async def test_tool_unstructured(ctx, params): assert span is not None data = span["attributes"] - assert data[SPANDATA.GEN_AI_TOOL_CALL_RESULT] == "First part Second part" + assert data[ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT] == "First part Second part" @pytest.mark.asyncio @@ -1320,7 +1327,7 @@ async def test_tool_unstructured(ctx, params): assert span is not None data = span["attributes"] - assert SPANDATA.GEN_AI_TOOL_CALL_RESULT not in data + assert ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT not in data @pytest.mark.asyncio @@ -1395,10 +1402,13 @@ async def prompt_handler(ctx, params): item.payload for item in items if item.type == "span" - and item.payload.get("attributes", {}).get("sentry.op") == OP.MCP_SERVER + and item.payload.get("attributes", {}).get(ATTRIBUTE_NAMES.SENTRY_OP) + == OP.MCP_SERVER ] assert len(mcp_spans) == 3 - assert all(s["attributes"]["sentry.op"] == OP.MCP_SERVER for s in mcp_spans) + assert all( + s["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.MCP_SERVER for s in mcp_spans + ) span_names = [s["name"] for s in mcp_spans] assert "tools/call tool_a" in span_names assert "tools/call tool_b" in span_names @@ -1454,10 +1464,10 @@ async def test_prompt_dict(ctx, params): data = span["attributes"] # Message count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE] == "user" - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT] == "Hello from dict" + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE] == "user" + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT] == "Hello from dict" @pytest.mark.asyncio @@ -1509,10 +1519,10 @@ async def test_prompt_dict(ctx, params): data = span["attributes"] # Message count is always captured - assert data[SPANDATA.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 + assert data[ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_COUNT] == 1 - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data - assert SPANDATA.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_ROLE not in data + assert ATTRIBUTE_NAMES.MCP_PROMPT_RESULT_MESSAGE_CONTENT not in data @pytest.mark.asyncio @@ -1640,9 +1650,9 @@ async def test_tool(ctx, params): assert span is not None data = span["attributes"] - assert data[SPANDATA.MCP_TRANSPORT] == "sse" - assert data[SPANDATA.NETWORK_TRANSPORT] == "tcp" - assert data[SPANDATA.MCP_SESSION_ID] == session_id + assert data[ATTRIBUTE_NAMES.MCP_TRANSPORT] == "sse" + assert data[ATTRIBUTE_NAMES.NETWORK_TRANSPORT] == "tcp" + assert data[ATTRIBUTE_NAMES.MCP_SESSION_ID] == session_id @pytest.mark.asyncio diff --git a/tests/integrations/ray/test_ray.py b/tests/integrations/ray/test_ray.py index 8abcfa3ce6..d6aaeb2537 100644 --- a/tests/integrations/ray/test_ray.py +++ b/tests/integrations/ray/test_ray.py @@ -6,6 +6,7 @@ import pytest import ray +from sentry_conventions.attributes import ATTRIBUTE_NAMES import sentry_sdk from sentry_sdk.consts import OP @@ -140,7 +141,7 @@ def example_task(): with sentry_sdk.start_span( name="example task step", attributes={ - "sentry.op": "task", + ATTRIBUTE_NAMES.SENTRY_OP: "task", }, ): ... @@ -149,7 +150,7 @@ def example_task(): # RayIntegration must leave variadic keyword arguments at the end def example_task_with_kwargs(**kwargs): with sentry_sdk.start_span( - name="example task step", attributes={"sentry.op": "task"} + name="example task step", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"} ): ... @@ -198,7 +199,7 @@ def test_tracing_in_ray_tasks( ) with sentry_sdk.start_span( - name="ray test parent", attributes={"sentry.op": "task"} + name="ray test parent", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"} ): future = example_task.remote() ray.get(future) @@ -220,20 +221,24 @@ def test_tracing_in_ray_tasks( assert worker_spans[1]["name"] == f"tests.integrations.ray.test_ray.{task.__name__}" assert ( - worker_spans[1]["attributes"]["sentry.segment.name.source"]["value"] + worker_spans[1]["attributes"][ATTRIBUTE_NAMES.SENTRY_SEGMENT_NAME_SOURCE][ + "value" + ] == SegmentNameSource.TASK ) span = client_spans[0] - assert span["attributes"]["sentry.op"]["value"] == OP.QUEUE_PUBLISH - assert span["attributes"]["sentry.origin"]["value"] == "auto.queue.ray" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP]["value"] == OP.QUEUE_PUBLISH + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN]["value"] == "auto.queue.ray" + ) assert span["name"] == f"tests.integrations.ray.test_ray.{task.__name__}" assert span["parent_span_id"] == client_spans[1]["span_id"] assert span["trace_id"] == client_spans[1]["trace_id"] span = worker_spans[0] - assert span["attributes"]["sentry.op"]["value"] == "task" - assert span["attributes"]["sentry.origin"]["value"] == "manual" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP]["value"] == "task" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN]["value"] == "manual" assert span["name"] == "example task step" assert span["parent_span_id"] == worker_spans[1]["span_id"] assert span["trace_id"] == worker_spans[1]["trace_id"] @@ -267,7 +272,7 @@ def example_task(): 1 / 0 with sentry_sdk.start_span( - name="ray test parent", attributes={"sentry.op": "task"} + name="ray test parent", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"} ): with pytest.raises(ZeroDivisionError): future = example_task.remote() @@ -312,7 +317,8 @@ def __init__(self): def increment(self): with sentry_sdk.start_span( - name="example actor execution", attributes={"sentry.op": "task"} + name="example actor execution", + attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"}, ): self.n += 1 else: @@ -324,7 +330,8 @@ def __init__(self): def increment(self): with sentry_sdk.start_span( - name="example actor execution", attributes={"sentry.op": "task"} + name="example actor execution", + attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"}, ): self.n += 1 @@ -341,7 +348,7 @@ def increment(self): ) with sentry_sdk.start_span( - name="ray test parent", attributes={"sentry.op": "task"} + name="ray test parent", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"} ): counter = Counter.remote() future = counter.increment.remote() @@ -398,14 +405,15 @@ def __init__(self): def increment(self): with sentry_sdk.start_span( - name="example actor execution", attributes={"sentry.op": "task"} + name="example actor execution", + attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"}, ): 1 / 0 return sentry_sdk.get_client().transport.envelopes with sentry_sdk.start_span( - name="ray test parent", attributes={"sentry.op": "task"} + name="ray test parent", attributes={ATTRIBUTE_NAMES.SENTRY_OP: "task"} ): with pytest.raises(ZeroDivisionError): counter = Counter.remote() diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index cacd7a6690..b67807d7c4 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -3,9 +3,10 @@ import pytest import rq 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.rq import RqIntegration from sentry_sdk.utils import parse_version from tests.integrations.utils import DATA_COLLECTION_QUEUES_CASES @@ -174,18 +175,21 @@ def test_worker_span_with_error( (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PROCESS + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) assert span["trace_id"] == error_event["contexts"]["trace"]["trace_id"] assert span["span_id"] == error_event["contexts"]["trace"]["span_id"] - assert span["attributes"]["sentry.op"] == error_event["contexts"]["trace"]["op"] assert ( - span["attributes"]["sentry.origin"] + span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] + == error_event["contexts"]["trace"]["op"] + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == error_event["contexts"]["trace"]["origin"] ) assert ( - span["attributes"][SPANDATA.CODE_FUNCTION_NAME] + span["attributes"][ATTRIBUTE_NAMES.CODE_FUNCTION_NAME] == "tests.integrations.rq.test_rq.chew_up_shoes" ) @@ -226,7 +230,7 @@ def test_tracing_enabled( with sentry_sdk.start_span( name="custom parent", attributes={ - "sentry.op": "rq transaction", + ATTRIBUTE_NAMES.SENTRY_OP: "rq transaction", }, ) as span: queue.enqueue(crashing_job, foo=None) @@ -241,14 +245,17 @@ def test_tracing_enabled( (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PROCESS + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) assert span["trace_id"] == error_event["contexts"]["trace"]["trace_id"] assert span["span_id"] == error_event["contexts"]["trace"]["span_id"] - assert span["attributes"]["sentry.op"] == error_event["contexts"]["trace"]["op"] assert ( - span["attributes"]["sentry.origin"] + span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] + == error_event["contexts"]["trace"]["op"] + ) + assert ( + span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == error_event["contexts"]["trace"]["origin"] ) @@ -294,12 +301,12 @@ def test_worker_span_no_error( (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PROCESS + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) - assert span["attributes"]["sentry.op"] == OP.QUEUE_PROCESS + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_OP] == OP.QUEUE_PROCESS assert span["name"] == "tests.integrations.rq.test_rq.do_trick" - assert span["attributes"][SPANDATA.MESSAGING_DESTINATION_NAME] == queue.name + assert span["attributes"][ATTRIBUTE_NAMES.MESSAGING_DESTINATION_NAME] == queue.name def test_traces_sampler_gets_correct_values_in_sampling_context( @@ -378,7 +385,7 @@ def test_span_origin( (span,) = ( span for span in spans - if span["attributes"].get("sentry.op") == OP.QUEUE_PROCESS + if span["attributes"].get(ATTRIBUTE_NAMES.SENTRY_OP) == OP.QUEUE_PROCESS ) - assert span["attributes"]["sentry.origin"] == "auto.queue.rq" + assert span["attributes"][ATTRIBUTE_NAMES.SENTRY_ORIGIN] == "auto.queue.rq"