Skip to content

Commit 54f6f8a

Browse files
feat(bedrock): Set gen_ai.response.time_to_first_chunk
1 parent 80267a2 commit 54f6f8a

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎sentry_sdk/integrations/boto3/_services/bedrock.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import sys
3+
import time
34
from typing import TYPE_CHECKING, cast
45

56
import sentry_sdk
@@ -97,6 +98,7 @@ def sentry_patched_iter(
9798
parts: "dict[int, Union[TextPart, ReasoningPart, ToolCallPart, ToolCallResponsePart, UriPart, BlobPart]]" = {}
9899

99100
try:
101+
start_time = time.perf_counter()
100102
for event in original_iter(self):
101103
if "metadata" in event:
102104
usage = event["metadata"]["usage"]
@@ -132,6 +134,13 @@ def sentry_patched_iter(
132134
yield event
133135
continue
134136

137+
if "contentBlockDelta" in event and start_time is not None:
138+
set_on_span(
139+
SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN,
140+
time.perf_counter() - start_time,
141+
)
142+
start_time = None
143+
135144
if "messageStop" in event:
136145
finish_reasons: "list[str]" = [event["messageStop"]["stopReason"]]
137146
set_on_span(SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons)

‎tests/integrations/boto3/test_bedrock.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ def test_converse_stream(
995995
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == [
996996
"end_turn"
997997
]
998+
assert span["attributes"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN] > 0
998999

9991000
assert span["attributes"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20
10001001
assert span["attributes"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30
@@ -1035,6 +1036,7 @@ def test_converse_stream(
10351036
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
10361037
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9
10371038
assert span["data"][SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS] == ["end_turn"]
1039+
assert span["data"][SPANDATA.GEN_AI_RESPONSE_TIME_TO_FIRST_TOKEN] > 0
10381040

10391041
assert span["data"][SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 20
10401042
assert span["data"][SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 30

0 commit comments

Comments
 (0)