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
165 changes: 96 additions & 69 deletions sentry_sdk/integrations/langchain.py

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions sentry_sdk/integrations/langgraph.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from functools import wraps
from typing import TYPE_CHECKING, Any, Callable, List, Optional

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.ai.utils import (
normalize_message_roles,
set_data_normalized,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version

# This is fine because langgraph depends on langchain-base, and LangchainIntegration only imports from langchain-base.
Expand Down Expand Up @@ -123,14 +125,14 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
with sentry_sdk.start_span(
name=f"invoke_agent {graph_name}".strip() if graph_name else "invoke_agent",
attributes={
"sentry.op": OP.GEN_AI_INVOKE_AGENT,
"sentry.origin": LanggraphIntegration.origin,
SPANDATA.GEN_AI_OPERATION_NAME: "invoke_agent",
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_INVOKE_AGENT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: LanggraphIntegration.origin,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "invoke_agent",
},
) as span:
if graph_name:
span.set_attribute(SPANDATA.GEN_AI_PIPELINE_NAME, graph_name)
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, graph_name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PIPELINE_NAME, graph_name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_AGENT_NAME, graph_name)

# Store input messages to later compare with output
input_messages = None
Expand All @@ -143,7 +145,7 @@ def new_invoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
normalized_input_messages = normalize_message_roles(input_messages)
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES,
normalized_input_messages,
unpack=False,
)
Expand All @@ -170,14 +172,14 @@ async def new_ainvoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
with sentry_sdk.start_span(
name=f"invoke_agent {graph_name}".strip() if graph_name else "invoke_agent",
attributes={
"sentry.op": OP.GEN_AI_INVOKE_AGENT,
"sentry.origin": LanggraphIntegration.origin,
SPANDATA.GEN_AI_OPERATION_NAME: "invoke_agent",
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_INVOKE_AGENT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: LanggraphIntegration.origin,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "invoke_agent",
},
) as span:
if graph_name:
span.set_attribute(SPANDATA.GEN_AI_PIPELINE_NAME, graph_name)
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, graph_name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PIPELINE_NAME, graph_name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_AGENT_NAME, graph_name)

input_messages = None
if len(args) > 0:
Expand All @@ -189,7 +191,7 @@ async def new_ainvoke(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
normalized_input_messages = normalize_message_roles(input_messages)
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES,
normalized_input_messages,
unpack=False,
)
Expand Down Expand Up @@ -268,17 +270,19 @@ def _set_response_attributes(

llm_response_text = _extract_llm_response_text(new_messages)
if llm_response_text:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, llm_response_text)
set_data_normalized(
span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, llm_response_text
)
elif new_messages:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, new_messages)
set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, new_messages)
else:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, result)
set_data_normalized(span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, result)

tool_calls = _extract_tool_calls(new_messages)
if tool_calls:
set_data_normalized(
span,
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TOOL_CALLS,
safe_serialize(tool_calls),
unpack=False,
)
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/openai_agents/patches/agent_run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
from typing import TYPE_CHECKING

from sentry_sdk.consts import SPANDATA
from sentry_conventions.attributes import ATTRIBUTE_NAMES

from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.traces import Span
from sentry_sdk.utils import capture_internal_exceptions, reraise
Expand Down Expand Up @@ -70,7 +71,7 @@ def _maybe_start_agent_span(
if not is_streaming:
return span

span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, True)

return span

Expand Down
9 changes: 6 additions & 3 deletions sentry_sdk/integrations/openai_agents/patches/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from functools import wraps
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.traces import BAGGAGE_HEADER_NAME, Span
from sentry_sdk.tracing_utils import (
Expand Down Expand Up @@ -139,7 +140,9 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
for hosted_tool in hosted_tools:
_inject_trace_propagation_headers(hosted_tool, span=context.span)

context.span.set_attribute(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
context.span.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_STREAMING, True
)

streaming_response = None
ttft_recorded = False
Expand All @@ -151,7 +154,7 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
if not ttft_recorded and hasattr(event, "delta"):
ttft = time.perf_counter() - start_time
context.span.set_attribute(
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, ttft
)
ttft_recorded = True

Expand Down
7 changes: 5 additions & 2 deletions sentry_sdk/integrations/openai_agents/patches/runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
from functools import wraps

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.utils import (
capture_internal_exceptions,
Expand Down Expand Up @@ -57,7 +58,9 @@ async def on_tool_start(
if not client.options["data_collection"]["gen_ai"]["inputs"]:
return

span.set_attribute(SPANDATA.GEN_AI_TOOL_CALL_ARGUMENTS, context.tool_arguments)
span.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_ARGUMENTS, context.tool_arguments
)

async def on_tool_end(
self,
Expand Down
36 changes: 20 additions & 16 deletions sentry_sdk/integrations/openai_agents/spans/ai_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.traces import Span, _AgentFrameworkChatGenerationContext

Expand Down Expand Up @@ -210,14 +212,14 @@ def ai_client_context(
context = _AgentFrameworkChatGenerationContext(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_CHAT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "chat",
},
)

context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")
context.span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
context.span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_PROVIDER_NAME, "openai")
context.span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_AGENT_NAME, agent.name)

request_model_name: "Optional[str]" = None
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
Expand All @@ -228,38 +230,40 @@ def ai_client_context(
request_model_name = agent._sentry_request_model

if request_model_name:
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, request_model_name)
context.span.set_attribute(
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MODEL, request_model_name
)

if agent.model_settings.max_tokens:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MAX_TOKENS, agent.model_settings.max_tokens
)

if agent.model_settings.presence_penalty:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_PRESENCE_PENALTY,
agent.model_settings.presence_penalty,
)

if agent.model_settings.temperature:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
ATTRIBUTE_NAMES.GEN_AI_REQUEST_TEMPERATURE, agent.model_settings.temperature
)

if agent.model_settings.top_p:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p
ATTRIBUTE_NAMES.GEN_AI_REQUEST_TOP_P, agent.model_settings.top_p
)

if agent.model_settings.frequency_penalty:
context.span.set_attribute(
SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_FREQUENCY_PENALTY,
agent.model_settings.frequency_penalty,
)

if client_options["data_collection"]["gen_ai"]["inputs"]:
context.span.set_attribute(
SPANDATA.GEN_AI_TOOL_DEFINITIONS,
ATTRIBUTE_NAMES.GEN_AI_TOOL_DEFINITIONS,
json.dumps(_transform_tool_definitions(agent.tools)),
)

Expand All @@ -282,12 +286,12 @@ def update_ai_client_span(
_set_output_data(span, response)

if response_model is not None:
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, response_model)
elif hasattr(response, "model") and response.model:
span.set_attribute(SPANDATA.GEN_AI_RESPONSE_MODEL, str(response.model))
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_RESPONSE_MODEL, str(response.model))

# Set conversation ID from agent if available
if agent:
conv_id = getattr(agent, "_sentry_conversation_id", None)
if conv_id:
span.set_attribute(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conv_id)
20 changes: 11 additions & 9 deletions sentry_sdk/integrations/openai_agents/spans/execute_tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.traces import Span, SpanStatus

from ..consts import SPAN_ORIGIN
Expand All @@ -18,11 +20,11 @@ def execute_tool_span(
return sentry_sdk.start_span(
name=f"execute_tool {tool.name}",
attributes={
"sentry.op": OP.GEN_AI_EXECUTE_TOOL,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "execute_tool",
SPANDATA.GEN_AI_TOOL_NAME: tool.name,
SPANDATA.GEN_AI_TOOL_DESCRIPTION: tool.description,
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_EXECUTE_TOOL,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "execute_tool",
ATTRIBUTE_NAMES.GEN_AI_TOOL_NAME: tool.name,
ATTRIBUTE_NAMES.GEN_AI_TOOL_DESCRIPTION: tool.description,
},
)

Expand All @@ -35,17 +37,17 @@ def update_execute_tool_span(
) -> None:
client = sentry_sdk.get_client()

span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_AGENT_NAME, agent.name)

if isinstance(result, str) and result.startswith(
"An error occurred while running the tool"
):
span.status = SpanStatus.ERROR

if client.options["data_collection"]["gen_ai"]["outputs"]:
span.set_attribute(SPANDATA.GEN_AI_TOOL_CALL_RESULT, result)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_TOOL_CALL_RESULT, result)

# Add conversation ID from agent
conv_id = getattr(agent, "_sentry_conversation_id", None)
if conv_id:
span.set_attribute(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conv_id)
20 changes: 12 additions & 8 deletions sentry_sdk/integrations/openai_agents/spans/invoke_agent.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.ai.utils import (
normalize_message_roles,
set_data_normalized,
)
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.traces import Span
from sentry_sdk.utils import safe_serialize

Expand All @@ -25,9 +27,9 @@ def invoke_agent_span(
span = sentry_sdk.start_span(
name=f"invoke_agent {agent.name}",
attributes={
"sentry.op": OP.GEN_AI_INVOKE_AGENT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "invoke_agent",
ATTRIBUTE_NAMES.SENTRY_OP: OP.GEN_AI_INVOKE_AGENT,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
ATTRIBUTE_NAMES.GEN_AI_OPERATION_NAME: "invoke_agent",
},
)

Expand Down Expand Up @@ -64,12 +66,12 @@ def invoke_agent_span(
normalized_messages = normalize_message_roles(messages)
set_data_normalized(
span,
SPANDATA.GEN_AI_REQUEST_MESSAGES,
ATTRIBUTE_NAMES.GEN_AI_REQUEST_MESSAGES,
normalized_messages,
unpack=False,
)

span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_AGENT_NAME, agent.name)

return span

Expand All @@ -81,9 +83,11 @@ def update_invoke_agent_span(
) -> None:
client = sentry_sdk.get_client()
if client.options["data_collection"]["gen_ai"]["outputs"]:
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output, unpack=False)
set_data_normalized(
span, ATTRIBUTE_NAMES.GEN_AI_RESPONSE_TEXT, output, unpack=False
)

# Add conversation ID from agent
conv_id = getattr(agent, "_sentry_conversation_id", None)
if conv_id:
span.set_attribute(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id)
span.set_attribute(ATTRIBUTE_NAMES.GEN_AI_CONVERSATION_ID, conv_id)
Loading
Loading