Skip to content

feat: add Bedrock InvokeModelWithResponseStream instrumentation #2845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

yuliia-fryshko
Copy link
Contributor

Which problem is this PR solving?

Adds instrumentation of the InvokeModelWithResponseStreamCommand in the AWS Bedrock SDK.

Short description of the changes

  • Captures GenAI semantic span attributes for Amazon Titan, Anthropic Claude, Amazon Nova models.
  • Ensures span.end() is deferred until the full stream is consumed.
  • instrumentAsyncIterable is used to inspect streamed chunks in real time and extract relevant telemetry.
  • Includes unit tests for Titan, Claude and Nova models.

@yuliia-fryshko yuliia-fryshko requested a review from a team as a code owner May 22, 2025 16:25
@github-actions github-actions bot requested review from blumamir, jj22ee and trivikr May 22, 2025 16:25
@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch 2 times, most recently from 09f9777 to eeb1e84 Compare May 23, 2025 09:56
@@ -102,6 +102,13 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
return this.requestPreSpanHookConverse(request, config, diag, true);
case 'InvokeModel':
return this.requestPreSpanHookInvokeModel(request, config, diag);
case 'InvokeModelWithResponseStream':
return this.requestPreSpanHookInvokeModelWithResponseStream(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to not re-use requestPreSpanHookInvokeModel: add a 4th isStream argument and pass in false for 'InvokeModel', true for 'InvokeModelWithResponseStream', and then make the minor update to the implementation? This is how it was done for 'Converse' and 'ConverseStream'.

It looks to me like the requestPreSpanHookInvokeModel and requestPreSpanHookInvokeModelWithResponseStream functions are almost identical ... except that the latter doesn't have blocks for 'meta.llama', 'cohere.*', and 'mistral'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, @trentm ! You are absolutely right! I've updated the code to consolidate requestPreSpanHookInvokeModel and requestPreSpanHookInvokeModelWithResponseStream into a single method using isStream parameter as you suggested.

Copy link

codecov bot commented May 27, 2025

Codecov Report

❌ Patch coverage is 88.09524% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.81%. Comparing base (8a93515) to head (44a3dec).

Files with missing lines Patch % Lines
...umentation-aws-sdk/src/services/bedrock-runtime.ts 88.09% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2845      +/-   ##
==========================================
- Coverage   89.83%   89.81%   -0.02%     
==========================================
  Files         188      188              
  Lines        9294     9376      +82     
  Branches     1907     1938      +31     
==========================================
+ Hits         8349     8421      +72     
- Misses        945      955      +10     
Files with missing lines Coverage Δ
...umentation-aws-sdk/src/services/bedrock-runtime.ts 95.72% <88.09%> (-3.27%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch from eeb1e84 to 0d197f0 Compare June 2, 2025 08:36
): Promise<any> {
const stream = response.data?.body;
const modelId = response.request.commandInput?.modelId;
if (!stream || !span.isRecording()) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!stream || !span.isRecording()) return;
if (!stream) return;

!span.isRecording() is already checked before responseHookInvokeModelWithResponseStream() is called

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, @jj22ee! I removed this unnecessary check

return response.data;

// Tap into the stream at the chunk level without modifying the chunk itself.
function instrumentAsyncIterable<T>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more efficient to declare these following functions outside of this member function scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @jj22ee , for the review and good catches! Yes, these helper functions could be declared outside the method, at the class level, instead of being re-created every time the method is called.
Changed!

@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch from fe8416b to ff1cb2d Compare June 27, 2025 18:54
@JacksonWeber
Copy link
Contributor

Update this to pull in the main branch and I can take another look over. Thanks!

parsedChunk,
span
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-stream responseHookInvokeModel() is adding some attributes for a few more models: 'meta.llama', 'cohere.command-r', etc.
Should this function also add relevant attributes for those models? Or is it possible streaming is not supported for those models?

Also, perhaps the recordNovaAttributes(), recordClaudeAttributes(), etc. methods could be used by both responseHookInvokeModel and responseHookInvokeModelWithResponseStream. Or perhaps all the record*Attribute() methods could be moved to one setInvokeModelResponseAttributes() that is used by both responseHookInvokeModel*() methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// while OpenTelemetry can record span attributes from streamed data.
response.data.body = (async function* () {
try {
for await (const item of wrappedStream) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the result here is that two async iterators are created. Could the chunk handling above be moved into this for await ... and then not bother having a wrappedStream at all? I haven't tried this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const str = Buffer.from(bytes).toString('utf-8');
return JSON.parse(str);
} catch (err) {
console.warn('Failed to parse streamed chunk', err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't use console in instrumentation code. Instead use the DiagLogger that every intsrumentation instance has.

Suggested change
console.warn('Failed to parse streamed chunk', err);
this._diag.warn('Failed to parse streamed chunk', err);

This will mean you'll need to not use static for this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @trentm , for the review . I addressed all your comments and would appreciate next round of a review. Thanks

@pichlermarc
Copy link
Member

@yuliia-fryshko do you have bandwidth to work on this? Looks like there are some unaddressed comments that are blocking this from getting merged.

@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch from 0e2fef3 to dcd6b28 Compare August 7, 2025 14:18
@yuliia-fryshko
Copy link
Contributor Author

@yuliia-fryshko do you have bandwidth to work on this? Looks like there are some unaddressed comments that are blocking this from getting merged.

Hi @pichlermarc ! Sorry for the long reply, I just back from the vacation today and I will try to address all comments today

@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch from 1941501 to d86f003 Compare August 11, 2025 07:48
@yuliia-fryshko yuliia-fryshko force-pushed the bedrock-invoke-model-stream branch from 71c7a6e to f7bbec1 Compare August 12, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants