Skip to content

Commit f3f3f24

Browse files
committed
fix: using local variable to track start time
1 parent 77e9e20 commit f3f3f24

File tree

3 files changed

+8
-18
lines changed

3 files changed

+8
-18
lines changed

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,16 @@ async def wrapped_stream_response(*args: "Any", **kwargs: "Any") -> "Any":
150150

151151
streaming_response = None
152152
ttft_recorded = False
153+
# Capture start time locally to avoid race conditions with concurrent requests
154+
start_time = time.perf_counter()
153155

154156
async for event in original_stream_response(*args, **kwargs):
155157
# Detect first content token (text delta event)
156158
if not ttft_recorded and hasattr(event, "delta"):
157-
start_time = getattr(
158-
agent, "_sentry_chat_ttft_start_time", None
159+
ttft = time.perf_counter() - start_time
160+
span.set_data(
161+
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
159162
)
160-
if start_time is not None:
161-
ttft = time.perf_counter() - start_time
162-
span.set_data(
163-
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft
164-
)
165163
ttft_recorded = True
166164

167165
# Capture the full response from ResponseCompletedEvent

sentry_sdk/integrations/openai_agents/spans/ai_client.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import time
2-
31
import sentry_sdk
42
from sentry_sdk.consts import OP, SPANDATA
53

@@ -38,9 +36,6 @@ def ai_client_span(
3836
# TODO-anton: remove hardcoded stuff and replace something that also works for embedding and so on
3937
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
4038

41-
# Store start time for TTFT calculation on the agent object
42-
agent._sentry_chat_ttft_start_time = time.perf_counter()
43-
4439
_set_agent_data(span, agent)
4540
_set_input_data(span, get_response_kwargs)
4641

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,15 +2276,12 @@ async def stream_response(self, *args, **kwargs):
22762276
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, True)
22772277

22782278
ttft_recorded = False
2279-
start_time = getattr(test_agent, "_sentry_chat_ttft_start_time", None)
2279+
# Capture start time locally (same as production code after race condition fix)
2280+
start_time = time.perf_counter()
22802281

22812282
async for event in mock_model.stream_response():
22822283
# This is the same logic used in the actual integration
2283-
if (
2284-
not ttft_recorded
2285-
and hasattr(event, "delta")
2286-
and start_time is not None
2287-
):
2284+
if not ttft_recorded and hasattr(event, "delta"):
22882285
ttft = time.perf_counter() - start_time
22892286
span.set_data(SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN, ttft)
22902287
ttft_recorded = True

0 commit comments

Comments
 (0)