Skip to content

Commit 5f42eb4

Browse files
Merge branch 'master' into webb/openai-agents/transaction
2 parents ef81df7 + 4f3b56a commit 5f42eb4

7 files changed

Lines changed: 34 additions & 44 deletions

File tree

scripts/test-lambda-locally/uv.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry_sdk/integrations/cohere.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from sentry_sdk.ai.monitoring import record_token_usage
77
from sentry_sdk.ai.utils import get_start_span_function, set_data_normalized
88
from sentry_sdk.consts import SPANDATA
9-
from sentry_sdk.tracing_utils import set_span_errored
109

1110
if TYPE_CHECKING:
1211
from typing import Any, Callable, Iterator
@@ -83,8 +82,6 @@ def setup_once() -> None:
8382

8483

8584
def _capture_exception(exc: "Any") -> None:
86-
set_span_errored()
87-
8885
event, hint = event_from_exception(
8986
exc,
9087
client_options=sentry_sdk.get_client().options,
@@ -154,7 +151,7 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":
154151
exc_info = sys.exc_info()
155152
with capture_internal_exceptions():
156153
_capture_exception(e)
157-
span.__exit__(None, None, None)
154+
span.__exit__(*exc_info)
158155
reraise(*exc_info)
159156

160157
with capture_internal_exceptions():

sentry_sdk/integrations/langchain.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from sentry_sdk.consts import OP, SPANDATA
2020
from sentry_sdk.integrations import DidNotEnable, Integration
2121
from sentry_sdk.scope import should_send_default_pii
22-
from sentry_sdk.tracing_utils import _get_value, set_span_errored
22+
from sentry_sdk.tracing_utils import _get_value
2323
from sentry_sdk.utils import capture_internal_exceptions, logger
2424

2525
if TYPE_CHECKING:
@@ -273,11 +273,10 @@ def _handle_error(self, run_id: "UUID", error: "Any") -> None:
273273

274274
span_data = self.span_map[run_id]
275275
span = span_data.span
276-
set_span_errored(span)
277276

278277
sentry_sdk.capture_exception(error, span.scope)
279278

280-
span.__exit__(None, None, None)
279+
span.__exit__(type(error), error, error.__traceback__)
281280
del self.span_map[run_id]
282281

283282
def _normalize_langchain_message(self, message: "BaseMessage") -> "Any":
@@ -1112,13 +1111,13 @@ def new_iterator() -> "Iterator[Any]":
11121111
and integration.include_prompts
11131112
):
11141113
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output)
1114+
1115+
span.__exit__(None, None, None)
11151116
except Exception:
11161117
exc_info = sys.exc_info()
1117-
set_span_errored(span)
1118+
with capture_internal_exceptions():
1119+
span.__exit__(*exc_info)
11181120
raise
1119-
finally:
1120-
# Ensure cleanup happens even if iterator is abandoned or fails
1121-
span.__exit__(*exc_info)
11221121

11231122
async def new_iterator_async() -> "AsyncIterator[Any]":
11241123
exc_info: "tuple[Any, Any, Any]" = (None, None, None)
@@ -1137,13 +1136,13 @@ async def new_iterator_async() -> "AsyncIterator[Any]":
11371136
and integration.include_prompts
11381137
):
11391138
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output)
1139+
1140+
span.__exit__(None, None, None)
11401141
except Exception:
11411142
exc_info = sys.exc_info()
1142-
set_span_errored(span)
1143+
with capture_internal_exceptions():
1144+
span.__exit__(*exc_info)
11431145
raise
1144-
finally:
1145-
# Ensure cleanup happens even if iterator is abandoned or fails
1146-
span.__exit__(*exc_info)
11471146

11481147
if str(type(result)) == "<class 'async_generator'>":
11491148
result = new_iterator_async()

sentry_sdk/integrations/pydantic_ai/utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sentry_sdk
55
from sentry_sdk.consts import SPANDATA
66
from sentry_sdk.scope import should_send_default_pii
7-
from sentry_sdk.tracing_utils import set_span_errored
87
from sentry_sdk.utils import event_from_exception, safe_serialize
98

109
if TYPE_CHECKING:
@@ -207,8 +206,6 @@ def _set_available_tools(span: "sentry_sdk.tracing.Span", agent: "Any") -> None:
207206

208207

209208
def _capture_exception(exc: "Any", handled: bool = False) -> None:
210-
set_span_errored()
211-
212209
event, hint = event_from_exception(
213210
exc,
214211
client_options=sentry_sdk.get_client().options,

sentry_sdk/integrations/strawberry.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ def _sentry_patched_schema_init(
119119
]
120120

121121
# add our extension
122-
extensions.append(
122+
extensions = [
123123
SentryAsyncExtension if should_use_async_extension else SentrySyncExtension
124-
)
124+
] + extensions
125125

126126
kwargs["extensions"] = extensions
127127

@@ -152,7 +152,7 @@ def hash_query(self, query: str) -> str:
152152
return hashlib.md5(query.encode("utf-8")).hexdigest()
153153

154154
def on_operation(self) -> "Generator[None, None, None]":
155-
self._operation_name = self.execution_context.operation_name
155+
operation_name = self.execution_context.operation_name
156156

157157
operation_type = "query"
158158
op = OP.GRAPHQL_QUERY
@@ -168,13 +168,13 @@ def on_operation(self) -> "Generator[None, None, None]":
168168
op = OP.GRAPHQL_SUBSCRIPTION
169169

170170
description = operation_type
171-
if self._operation_name:
172-
description += " {}".format(self._operation_name)
171+
if operation_name:
172+
description += " {}".format(operation_name)
173173

174174
sentry_sdk.add_breadcrumb(
175175
category="graphql.operation",
176176
data={
177-
"operation_name": self._operation_name,
177+
"operation_name": operation_name,
178178
"operation_type": operation_type,
179179
},
180180
)
@@ -183,50 +183,50 @@ def on_operation(self) -> "Generator[None, None, None]":
183183
event_processor = _make_request_event_processor(self.execution_context)
184184
scope.add_event_processor(event_processor)
185185

186-
self.graphql_span = sentry_sdk.start_span(
186+
graphql_span = sentry_sdk.start_span(
187187
op=op,
188188
name=description,
189189
origin=StrawberryIntegration.origin,
190190
)
191-
self.graphql_span.__enter__()
191+
graphql_span.__enter__()
192192

193-
self.graphql_span.set_data("graphql.operation.type", operation_type)
194-
self.graphql_span.set_data("graphql.operation.name", self._operation_name)
193+
graphql_span.set_data("graphql.operation.type", operation_type)
194+
graphql_span.set_data("graphql.operation.name", operation_name)
195195
if should_send_default_pii():
196-
self.graphql_span.set_data("graphql.document", self.execution_context.query)
197-
self.graphql_span.set_data("graphql.resource_name", self._resource_name)
196+
graphql_span.set_data("graphql.document", self.execution_context.query)
197+
graphql_span.set_data("graphql.resource_name", self._resource_name)
198198

199199
yield
200200

201-
transaction = self.graphql_span.containing_transaction
201+
transaction = graphql_span.containing_transaction
202202
if transaction and self.execution_context.operation_name:
203203
transaction.name = self.execution_context.operation_name
204204
transaction.source = TransactionSource.COMPONENT
205205
transaction.op = op
206206

207-
self.graphql_span.__exit__(None, None, None)
207+
graphql_span.__exit__(None, None, None)
208208

209209
def on_validate(self) -> "Generator[None, None, None]":
210-
self.validation_span = self.graphql_span.start_child(
210+
validation_span = sentry_sdk.start_span(
211211
op=OP.GRAPHQL_VALIDATE,
212212
name="validation",
213213
origin=StrawberryIntegration.origin,
214214
)
215215

216216
yield
217217

218-
self.validation_span.finish()
218+
validation_span.finish()
219219

220220
def on_parse(self) -> "Generator[None, None, None]":
221-
self.parsing_span = self.graphql_span.start_child(
221+
parsing_span = sentry_sdk.start_span(
222222
op=OP.GRAPHQL_PARSE,
223223
name="parsing",
224224
origin=StrawberryIntegration.origin,
225225
)
226226

227227
yield
228228

229-
self.parsing_span.finish()
229+
parsing_span.finish()
230230

231231
def should_skip_tracing(
232232
self,
@@ -263,7 +263,7 @@ async def resolve(
263263

264264
field_path = "{}.{}".format(info.parent_type, info.field_name)
265265

266-
with self.graphql_span.start_child(
266+
with sentry_sdk.start_span(
267267
op=OP.GRAPHQL_RESOLVE,
268268
name="resolving {}".format(field_path),
269269
origin=StrawberryIntegration.origin,
@@ -290,7 +290,7 @@ def resolve(
290290

291291
field_path = "{}.{}".format(info.parent_type, info.field_name)
292292

293-
with self.graphql_span.start_child(
293+
with sentry_sdk.start_span(
294294
op=OP.GRAPHQL_RESOLVE,
295295
name="resolving {}".format(field_path),
296296
origin=StrawberryIntegration.origin,

tests/integrations/cohere/test_cohere.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ def test_span_status_error(sentry_init, capture_events):
183183
assert error["level"] == "error"
184184
assert transaction["spans"][0]["status"] == "internal_error"
185185
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
186-
assert transaction["contexts"]["trace"]["status"] == "internal_error"
187186

188187

189188
@pytest.mark.parametrize(

tests/integrations/langchain/test_langchain.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,8 +2561,6 @@ def _llm_type(self) -> str:
25612561
assert transaction["spans"][0]["status"] == "internal_error"
25622562
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
25632563

2564-
assert transaction["contexts"]["trace"]["status"] == "internal_error"
2565-
25662564

25672565
def test_manual_callback_no_duplication(sentry_init):
25682566
"""

0 commit comments

Comments
 (0)