Skip to content

Commit 3f510ea

Browse files
merge
2 parents f1fee87 + f86880c commit 3f510ea

18 files changed

Lines changed: 272 additions & 442 deletions

File tree

‎sentry_sdk/integrations/flask.py‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
RequestExtractor,
99
)
1010
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
11-
from sentry_sdk.scope import should_send_default_pii
1211
from sentry_sdk.traces import SegmentNameSource
1312
from sentry_sdk.utils import (
1413
capture_internal_exceptions,
1514
ensure_integration_enabled,
1615
event_from_exception,
17-
has_data_collection_enabled,
1816
package_version,
1917
)
2018

@@ -146,13 +144,7 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None:
146144

147145
scope = sentry_sdk.get_isolation_scope()
148146

149-
if has_data_collection_enabled(client.options):
150-
if client.options["data_collection"]["user_info"]:
151-
with capture_internal_exceptions():
152-
user_properties = _get_flask_user_properties()
153-
if user_properties:
154-
scope.set_user(user_properties)
155-
elif should_send_default_pii():
147+
if client.options["data_collection"]["user_info"]:
156148
with capture_internal_exceptions():
157149
user_properties = _get_flask_user_properties()
158150
if user_properties:
@@ -205,11 +197,7 @@ def inner(event: "Event", hint: "dict[str, Any]") -> "Event":
205197
FlaskRequestExtractor(request).extract_into_event(event)
206198

207199
client_options = sentry_sdk.get_client().options
208-
if has_data_collection_enabled(client_options):
209-
if client_options["data_collection"]["user_info"]:
210-
with capture_internal_exceptions():
211-
_add_user_to_event(event)
212-
elif should_send_default_pii():
200+
if client_options["data_collection"]["user_info"]:
213201
with capture_internal_exceptions():
214202
_add_user_to_event(event)
215203

‎sentry_sdk/integrations/openai_agents/spans/ai_client.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
HostedMCPTool,
1616
ImageGenerationTool,
1717
LocalShellTool,
18+
Model,
1819
WebSearchTool,
1920
)
2021
except ImportError:
@@ -44,7 +45,6 @@
4445

4546
from ..consts import SPAN_ORIGIN
4647
from ..utils import (
47-
_set_agent_data,
4848
_set_input_data,
4949
_set_output_data,
5050
_set_usage_data,
@@ -216,7 +216,20 @@ def ai_client_context(
216216
},
217217
)
218218

219-
_set_agent_data(context.span, agent)
219+
context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, "openai")
220+
221+
request_model_name: "Optional[str]" = None
222+
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
223+
request_model_name = agent.model.model
224+
elif isinstance(agent.model, str):
225+
request_model_name = agent.model
226+
elif hasattr(agent, "_sentry_request_model"):
227+
request_model_name = agent._sentry_request_model
228+
229+
if request_model_name:
230+
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, request_model_name)
231+
232+
context.span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
220233

221234
if agent.model_settings.max_tokens:
222235
context.span.set_attribute(

‎sentry_sdk/integrations/openai_agents/spans/execute_tool.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from sentry_sdk.traces import Span, SpanStatus
66

77
from ..consts import SPAN_ORIGIN
8-
from ..utils import _set_agent_data
98

109
if TYPE_CHECKING:
1110
from typing import Any
@@ -36,7 +35,7 @@ def update_execute_tool_span(
3635
) -> None:
3736
client = sentry_sdk.get_client()
3837

39-
_set_agent_data(span, agent)
38+
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
4039

4140
if isinstance(result, str) and result.startswith(
4241
"An error occurred while running the tool"

‎sentry_sdk/integrations/openai_agents/spans/invoke_agent.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from sentry_sdk.utils import safe_serialize
1111

1212
from ..consts import SPAN_ORIGIN
13-
from ..utils import _set_agent_data
1413

1514
if TYPE_CHECKING:
1615
from typing import Any, Optional
@@ -70,7 +69,7 @@ def invoke_agent_span(
7069
unpack=False,
7170
)
7271

73-
_set_agent_data(span, agent)
72+
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
7473

7574
return span
7675

‎sentry_sdk/integrations/openai_agents/utils.py‎

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@
1414
set_data_normalized,
1515
)
1616
from sentry_sdk.consts import SPANDATA
17-
from sentry_sdk.integrations import DidNotEnable
1817
from sentry_sdk.traces import Span
1918
from sentry_sdk.utils import (
2019
event_from_exception,
2120
safe_serialize,
2221
)
2322

2423
if TYPE_CHECKING:
25-
from typing import Any, Optional
24+
from typing import Any
2625

2726
from agents import TResponseInputItem, Usage
2827

2928
from sentry_sdk._types import TextPart
3029

31-
try:
32-
import agents
33-
from agents import Model
34-
except ImportError:
35-
raise DidNotEnable("OpenAI Agents not installed")
36-
3730

3831
def _capture_exception(exc: "Any") -> None:
3932
event, hint = event_from_exception(
@@ -44,25 +37,6 @@ def _capture_exception(exc: "Any") -> None:
4437
sentry_sdk.capture_event(event, hint=hint)
4538

4639

47-
def _set_agent_data(span: "Span", agent: "agents.Agent") -> None:
48-
span.set_attribute(
49-
SPANDATA.GEN_AI_PROVIDER_NAME, "openai"
50-
) # See footnote for https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/#gen-ai-system for explanation why.
51-
52-
span.set_attribute(SPANDATA.GEN_AI_AGENT_NAME, agent.name)
53-
54-
model_name: "Optional[str]" = None
55-
if isinstance(agent.model, Model) and hasattr(agent.model, "model"):
56-
model_name = agent.model.model
57-
elif isinstance(agent.model, str):
58-
model_name = agent.model
59-
elif hasattr(agent, "_sentry_request_model"):
60-
model_name = agent._sentry_request_model
61-
62-
if model_name:
63-
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
64-
65-
6640
def _set_usage_data(span: "Span", usage: "Usage") -> None:
6741
span.set_attribute(SPANDATA.GEN_AI_USAGE_INPUT_TOKENS, usage.input_tokens)
6842
span.set_attribute(

‎sentry_sdk/integrations/pydantic_ai/__init__.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ async def sentry_wrap_run(
127127
with sentry_sdk.isolation_scope(), invoke_agent_span(
128128
user_prompt=ctx.prompt,
129129
agent=ctx.agent,
130-
model=ctx.model,
131130
model_settings=ctx.model_settings,
132131
) as span:
133132
try:

‎sentry_sdk/integrations/pydantic_ai/spans/ai_client.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,23 @@ def ai_client_context(
290290
)
291291

292292
_set_agent_data(context.span, agent)
293-
_set_model_data(context.span, agent, model, model_settings)
293+
294+
# Extract model information
295+
model_obj = model
296+
if not model_obj and agent and hasattr(agent, "model"):
297+
model_obj = agent.model
298+
299+
if model_obj:
300+
# Set system from model
301+
if hasattr(model_obj, "system"):
302+
context.span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, model_obj.system)
303+
304+
# Set model name
305+
model_name = _get_model_name(model_obj)
306+
if model_name:
307+
context.span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
308+
309+
_set_model_data(context.span, agent, model_settings)
294310
_set_available_tools(context.span, agent)
295311

296312
# Set input messages (full conversation history)

‎sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from typing import Any, Optional, Sequence, Union
2323

2424
from pydantic_ai import Agent, UserContent
25-
from pydantic_ai.models import AbstractModel
2625
from pydantic_ai.realtime.settings import RealtimeModelSettings
2726
from pydantic_ai.settings import ModelSettings
2827

@@ -36,7 +35,6 @@
3635
def invoke_agent_span(
3736
user_prompt: "Optional[Union[str, Sequence[UserContent]]]",
3837
agent: "Optional[Agent]",
39-
model: "AbstractModel",
4038
model_settings: "Optional[Union[ModelSettings, RealtimeModelSettings]]",
4139
) -> "Span":
4240
"""Create a span for invoking the agent."""
@@ -55,7 +53,7 @@ def invoke_agent_span(
5553
)
5654

5755
_set_agent_data(span, agent)
58-
_set_model_data(span, agent, model, model_settings)
56+
_set_model_data(span, agent, model_settings)
5957

6058
# Add user prompt and system prompts if available and prompts are enabled
6159
if not sentry_sdk.get_client().options["data_collection"]["gen_ai"]["inputs"]:

‎sentry_sdk/integrations/pydantic_ai/utils.py‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def _get_model_name(
5858
def _set_model_data(
5959
span: "Span",
6060
agent: "Optional[Agent]",
61-
model: "Union[Model, AbstractModel]",
6261
model_settings: "Optional[Union[ModelSettings, RealtimeModelSettings]]",
6362
) -> None:
6463
"""Set model-related data on a span.
@@ -68,21 +67,6 @@ def _set_model_data(
6867
model: Model object (can be None, will try to get from agent if not provided)
6968
model_settings: Model settings (can be None, will try to get from agent if not provided)
7069
"""
71-
# Extract model information
72-
model_obj = model
73-
if not model_obj and agent and hasattr(agent, "model"):
74-
model_obj = agent.model
75-
76-
if model_obj:
77-
# Set system from model
78-
if hasattr(model_obj, "system"):
79-
span.set_attribute(SPANDATA.GEN_AI_PROVIDER_NAME, model_obj.system)
80-
81-
# Set model name
82-
model_name = _get_model_name(model_obj)
83-
if model_name:
84-
span.set_attribute(SPANDATA.GEN_AI_REQUEST_MODEL, model_name)
85-
8670
# Extract model settings
8771
settings = model_settings
8872
if not settings and agent and hasattr(agent, "model_settings"):

0 commit comments

Comments
 (0)