Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions sentry_sdk/integrations/arq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
49 changes: 30 additions & 19 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
)

Expand Down Expand Up @@ -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,
},
)

Expand All @@ -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":
Expand All @@ -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)
Expand All @@ -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,
)

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions sentry_sdk/integrations/dramatiq.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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,
)
Expand Down
24 changes: 14 additions & 10 deletions sentry_sdk/integrations/huey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 20 additions & 18 deletions sentry_sdk/integrations/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,22 +156,22 @@ 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:
span.set_attribute(SPANDATA.MCP_REQUEST_ID, request_id)

# 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():
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 (
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
Loading
Loading