Skip to content

feat(instrumentation-aws-sdk): add gen ai conventions for converse stream span #2769

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 10 commits into
base: main
Choose a base branch
from

Conversation

anuraaga
Copy link
Contributor

Which problem is this PR solving?

  • Currently only non-streaming Converse populates gen ai conventions

Short description of the changes

  • Allows a service extension to override the response, needed to wrap streams
  • Don't end the span in middleware if an extension indicates the response is a stream as the extension needs to end it after it populates attributes during stream consumption. Unrelated to semconv, this should also make the timings more accurate instead of only being for the initial request before response consumption
  • Handles ConverseStream in bedrock extension using above

/cc @trentm @codefromthecrypt

if (override) {
response.output = override;
normalizedResponse.data = override;
}
self._callUserResponseHook(span, normalizedResponse);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered whether this should be done for the user hook too but didn't think there's enough use case for it. Currently the change is only internal since AFAIK, users can't define service extensions

Copy link

codecov bot commented Mar 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.71%. Comparing base (64fcbf3) to head (c908fe9).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2769      +/-   ##
==========================================
+ Coverage   89.69%   89.71%   +0.02%     
==========================================
  Files         184      184              
  Lines        8966     8988      +22     
  Branches     1835     1839       +4     
==========================================
+ Hits         8042     8064      +22     
  Misses        924      924              
Files with missing lines Coverage Δ
...entelemetry-instrumentation-aws-sdk/src/aws-sdk.ts 92.94% <100.00%> (+0.17%) ⬆️
...ntation-aws-sdk/src/services/ServicesExtensions.ts 96.87% <100.00%> (ø)
...umentation-aws-sdk/src/services/bedrock-runtime.ts 98.99% <100.00%> (+0.09%) ⬆️

... and 1 file with indirect coverage changes

🚀 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.

Copy link

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

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

Streaming is tricky but you handled it concisely. Thanks

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

Some initial review. I haven't looked carefully at the implementation yet.

// isStream - if true, then the response is a stream so the span should not be ended by the middleware.
// the ServiceExtension must end the span itself, generally by wrapping the stream and ending after it is
// consumed.
isStream?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know if the GenAI SIG discussed/documented wanting this behaviour of ending the span after the full stream is consumed? I've seen opinions vary when discussing HTTP streaming. See the guidance for HTTP client span duration here: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#http-client-span-duration

Is there any equivalent in the Python GenAI-related instrumentations?

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 link, that's interesting indeed in terms of response streaming. There isn't any guideline on gen ai spans, however I feel it's sort of implied by the conventions for token usage - there really isn't a way to populate them without keeping the span until the end of the stream. While the duration could keep streaming out of it, that would then need to override the end time of that span with the earlier timestamp, which I don't think the JS SDK supports.

Copy link
Contributor

Choose a reason for hiding this comment

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

While the duration could keep streaming out of it, that would then need to override the end time of that span with the earlier timestamp, which I don't think the JS SDK supports.

Correct, it does not support that. The (documented) behaviour then would be that the Span duration would be just up until the first response from the server, effectively TTFB.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah do you mean that we should use TTFB here? That would mean we couldn't record usage information though.

BTW, I realized that this might be closer to RPC than HTTP which has some specification for streaming

https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#message-event

an event for each message sent/received on client and server spans SHOULD be created

I think this also implies the overall span is for the whole stream. FWIW python keeps the span for the entire stream too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah do you mean that we should use TTFB here?

No, I did not mean to imply what this "should" do. I don't have a strong opinion one way or the other.

The HTTP span guidance from https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#http-client-span-duration says:

Because of the potential for confusion around this, HTTP client library instrumentations SHOULD document their behavior around ending HTTP client spans.

I only meant to say that if it is decided to handle this stream by having the span end just on the initial response that this should be documented.

FWIW python keeps the span for the entire stream too.

Sounds good to me to have the intention for the JS instrumentation to be the same.

import { AwsInstrumentation } from '../src';

export const instrumentation = new AwsInstrumentation();
export const metricReader = initMeterProvider(instrumentation);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized this pattern seemed to not work for multiple tests. I did try passing DELTA temporality in the MetricReader constructor since I thought it would fix it but it didn't. So I changed to a pattern inspired by what's used in Java

https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java#L122

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

LGTM, with a couple small nits.

@jj22ee, @blumamir, @trivikr It would be good to have one your opinions on this as component owners. @jj22ee I think this PR predates your being added as a component owner for instrumentation-aws-sdk, so it seems likely you haven't seen this.

) {
return {
...response.data,
stream: this.wrapConverseStreamResponse(
Copy link
Contributor

Choose a reason for hiding this comment

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

Does stream: this.wrapConverseStreamResponse(...) overwrite the stream from ...response.data?
I think it will be nice to have a comment to call this out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup - added a comment about it

yuliia-fryshko referenced this pull request May 21, 2025
The instrumentation is done for claude, titan and nova models
Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

LGTM. Waiting for an approval from one of the code owners before merging.

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.

6 participants