Skip to content

Commit fb85f5b

Browse files
feat(boto3): Instrument Bedrock Runtime streaming calls as gen_ai spans
Record ConverseStream and InvokeModelWithResponseStream calls as gen_ai spans that finish once the caller has received the stream: when it's exhausted, closed, iteration stops early, or it fails. The finish reason, token usage, and output messages are accumulated from the events the caller receives, which are unchanged. `EventStream.__iter__` and `close` are patched globally, since the iterator is a method; only streams of instrumented calls are recorded. Refs #5470 Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com> Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com>
1 parent 79fee6a commit fb85f5b

4 files changed

Lines changed: 1208 additions & 41 deletions

File tree

‎sentry_sdk/integrations/boto3/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
22
from sentry_sdk.integrations.boto3._client import _patch_botocore_client
3+
from sentry_sdk.integrations.boto3._instrumentation import _patch_event_stream
34
from sentry_sdk.integrations.boto3.consts import IDENTIFIER, ORIGIN
45
from sentry_sdk.utils import parse_version
56

@@ -24,3 +25,4 @@ def setup_once() -> None:
2425
_check_minimum_version(Boto3Integration, version, "botocore")
2526

2627
_patch_botocore_client()
28+
_patch_event_stream()

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
)
2626

2727
if TYPE_CHECKING:
28-
from typing import Any, Callable, Dict, Mapping, Optional, Union
28+
from typing import Any, Callable, Dict, Iterator, Mapping, Optional, Union
2929

3030
from sentry_sdk._types import Attributes
3131
from sentry_sdk.integrations.boto3._context import AwsCallContext
3232
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
3333

3434
try:
3535
from botocore.awsrequest import AWSRequest
36+
from botocore.eventstream import EventStream
3637
from botocore.exceptions import ClientError
3738
from botocore.response import StreamingBody
3839
except ImportError:
@@ -391,6 +392,92 @@ def sentry_raw_stream_close(*args: "Any", **kwargs: "Any") -> None:
391392
raise
392393

393394

395+
class _EventStreamHooks:
396+
"""
397+
Callbacks of an instrumented `EventStream`, stored on the stream.
398+
"""
399+
400+
__slots__ = ("on_event", "on_finish", "finished")
401+
402+
def __init__(
403+
self,
404+
on_event: "Callable[[Any], None]",
405+
on_finish: "Callable[[Optional[BaseException]], None]",
406+
) -> None:
407+
self.on_event = on_event
408+
self.on_finish = on_finish
409+
self.finished = False
410+
411+
def finish(self, error: "Optional[BaseException]" = None) -> None:
412+
# only finish once across iteration and close.
413+
if self.finished:
414+
return
415+
416+
self.finished = True
417+
with capture_internal_exceptions():
418+
self.on_finish(error)
419+
420+
421+
def _instrument_event_stream(
422+
stream: "EventStream[Any]",
423+
on_event: "Callable[[Any], None]",
424+
on_finish: "Callable[[Optional[BaseException]], None]",
425+
) -> None:
426+
"""
427+
Call `on_event` with each event the caller receives from `stream`, and
428+
`on_finish` once, when `stream` is exhausted, closed, stops being iterated,
429+
or fails.
430+
"""
431+
stream._sentry_hooks = _EventStreamHooks(on_event, on_finish) # type: ignore[attr-defined]
432+
433+
434+
def _patch_event_stream() -> None:
435+
"""
436+
Patch `EventStream` globally, since its iterator is a method; streams without
437+
hooks are unchanged.
438+
"""
439+
orig_iter = EventStream.__iter__
440+
orig_close = EventStream.close
441+
442+
def sentry_event_stream_iter(self: "EventStream[Any]") -> "Iterator[Any]":
443+
hooks: "Optional[_EventStreamHooks]" = getattr(self, "_sentry_hooks", None)
444+
if hooks is None:
445+
yield from orig_iter(self)
446+
return
447+
448+
try:
449+
for event in orig_iter(self):
450+
with capture_internal_exceptions():
451+
hooks.on_event(event)
452+
yield event
453+
except GeneratorExit:
454+
# the caller stopped iterating, e.g. with `break`.
455+
hooks.finish()
456+
raise
457+
except BaseException as error:
458+
hooks.finish(error)
459+
raise
460+
461+
hooks.finish()
462+
463+
def sentry_event_stream_close(
464+
self: "EventStream[Any]", *args: "Any", **kwargs: "Any"
465+
) -> None:
466+
hooks: "Optional[_EventStreamHooks]" = getattr(self, "_sentry_hooks", None)
467+
try:
468+
orig_close(self, *args, **kwargs)
469+
except BaseException as error:
470+
if hooks is not None:
471+
hooks.finish(error)
472+
raise
473+
474+
if hooks is not None:
475+
hooks.finish()
476+
477+
EventStream.__iter__ = sentry_event_stream_iter # type: ignore[method-assign]
478+
EventStream.close = sentry_event_stream_close # type: ignore[method-assign]
479+
480+
394481
def _set_request_attributes(
395482
span: "Union[Span, StreamedSpan]",
396483
request: "AWSRequest",

0 commit comments

Comments
 (0)