Title: feat(boto3): Instrument Bedrock Runtime streaming calls as gen_ai spans by sentry-junior[bot] · Pull Request #8189 · getsentry/sentry-python · GitHub
Open Graph Title: feat(boto3): Instrument Bedrock Runtime streaming calls as gen_ai spans by sentry-junior[bot] · Pull Request #8189 · getsentry/sentry-python
X Title: feat(boto3): Instrument Bedrock Runtime streaming calls as gen_ai spans by sentry-junior[bot] · Pull Request #8189 · getsentry/sentry-python
Description: Description
This PR (3 of 3, stacked on #8187) records ConverseStream (chat) and InvokeModelWithResponseStream (generate_content) as gen_ai spans with gen_ai.response.streaming: true. The finish reason, token usage, and output messages (text, reasoning, and tool calls with streamed JSON arguments) are built from the events the caller receives. The span finishes once, when the stream is:
exhausted
close()d, read or unread
stopped early, e.g. break (the iterator's GeneratorExit)
failed, e.g. a modelStreamErrorException event or a read error. The error is re-raised unchanged and the span is marked as errored.
Decisions the diff doesn't show:
botocore.eventstream.EventStream.__iter__ and close are patched globally in setup_once. Python looks up __iter__ on the class, so it can't be patched per instance. The patch only acts on streams that carry a _sentry_hooks attribute, which this sets on instrumented Bedrock responses. Other event streams, e.g. S3 Select, go through yield from unchanged. This is the same approach as the Anthropic integration's Stream._iterator patch. The response object and the events the caller gets are untouched.
InvokeModelWithResponseStream chunks are parsed per model family, like JS. Nova chunks reuse the ConverseStream parser, since they share its format. Usage falls back to Bedrock's amazon-bedrock-invocationMetrics in the last chunk. Claude output tokens come from message_delta rather than message_start, which JS uses and which reports only the first token.
Output content is buffered only when data_collection.gen_ai.outputs (or, without data_collection, send_default_pii with include_prompts) allows recording it. That check, made when the stream starts, is the only gate on streamed outputs. Input messages are gated by data_collection.gen_ai.inputs when the request is made, as for the non-streaming calls.
Malformed InvokeModelWithResponseStream chunks are skipped quietly. The caller still gets them unchanged.
Changelog note
ConverseStream and InvokeModelWithResponseStream spans change from http.client / auto.http.boto3 to gen_ai.chat and gen_ai.generate_content / auto.ai.boto3, and their names change from aws.bedrock-runtime.ConverseStream and aws.bedrock-runtime.InvokeModelWithResponseStream to chat
Open Graph Description: Description This PR (3 of 3, stacked on #8187) records ConverseStream (chat) and InvokeModelWithResponseStream (generate_content) as gen_ai spans with gen_ai.response.streaming: true. The finish re...
X Description: Description This PR (3 of 3, stacked on #8187) records ConverseStream (chat) and InvokeModelWithResponseStream (generate_content) as gen_ai spans with gen_ai.response.streaming: true. The finish re...
Opengraph URL: https://github.com/getsentry/sentry-python/pull/8189
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d42a3c8b-711c-f5d4-7f96-a6f42e4d94dc |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D76C:14CE41:6FC321:9C8CCA:6B044C4A |
| html-safe-nonce | daac6dadde3e79683fbc487734ce4f971cfa83203ca1284179c37ea616e13e23 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzZDOjE0Q0U0MTo2RkMzMjE6OUM4Q0NBOjZCMDQ0QzRBIiwidmlzaXRvcl9pZCI6IjkxOTU5NTkxMjIxMjA2OTA3NjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1ae931c60558b99d09c25ecbe58484b4f1c6ac1ca2489b5dd1caa5cb1f7cf4da |
| hovercard-subject-tag | pull_request:5162913783 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/getsentry/sentry-python/pull/8189/files |
| twitter:image | https://avatars.githubusercontent.com/in/2960336?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/in/2960336?s=400&v=4 |
| og:image:alt | Description This PR (3 of 3, stacked on #8187) records ConverseStream (chat) and InvokeModelWithResponseStream (generate_content) as gen_ai spans with gen_ai.response.streaming: true. The finish re... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 002aa6b95ffa730b0aec436a5c383f536ad677284e60056037fb120afe37b730 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/getsentry/sentry-python git https://github.com/getsentry/sentry-python.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 137814779 |
| octolytics-dimension-repository_nwo | getsentry/sentry-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 137814779 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-python |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 059f24c781e3a0d7df3f27a97da0e93fab06ae39 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width