Skip to content

Conversation

msonnb
Copy link

@msonnb msonnb commented Aug 4, 2025

Which problem is this PR solving?

Fixes #2827

Short description of the changes

Adds support for AWS Lambda streaming handlers by checking if the handler has the special aws.lambda.runtime.handler.streaming symbol.

@msonnb msonnb requested a review from a team as a code owner August 4, 2025 09:18
Copy link

linux-foundation-easycla bot commented Aug 4, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: msonnb / name: Martin Sonnberger (74ab471)

@github-actions github-actions bot requested a review from jj22ee August 4, 2025 09:18
@msonnb msonnb changed the title feat(instrumentation-aws-lamda): support streaming handlers feat(instrumentation-aws-lambda): support streaming handlers Aug 4, 2025
this._endSpan(span, undefined, () => resolve(value))
);
},
(err: Error | string) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it intentional that the promise fails silently in this scenario?

Copy link
Author

Choose a reason for hiding this comment

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

I'm not fully sure what you mean 🤔 we return a new promise that rejects with the error after the span is ended, so it shouldn't fail silently? (Also I didn't change this part, just extracted it into a function)

@pichlermarc
Copy link
Member

cc @jj22ee (component owner)

@jj22ee
Copy link
Contributor

jj22ee commented Aug 28, 2025

Hey, thanks for the fix. Planning to take a look this week.

Copy link
Contributor

@jj22ee jj22ee left a comment

Choose a reason for hiding this comment

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

Ty for this! So far I've just looked at the implementation changes, haven't fully looked at the unit tests yet.

Comment on lines +199 to 215
if (this._isStreamingHandler(original)) {
const patchedHandler = this._getPatchHandler(
original,
handlerLoadStartTime
);

// Streaming handlers have special symbols that we need to copy over to the patched handler.
for (const symbol of Object.getOwnPropertySymbols(original)) {
(patchedHandler as unknown as Record<symbol, unknown>)[symbol] = (
original as unknown as Record<symbol, unknown>
)[symbol];
}

return patchedHandler;
}

return this._getPatchHandler(original, handlerLoadStartTime);
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 (this._isStreamingHandler(original)) {
const patchedHandler = this._getPatchHandler(
original,
handlerLoadStartTime
);
// Streaming handlers have special symbols that we need to copy over to the patched handler.
for (const symbol of Object.getOwnPropertySymbols(original)) {
(patchedHandler as unknown as Record<symbol, unknown>)[symbol] = (
original as unknown as Record<symbol, unknown>
)[symbol];
}
return patchedHandler;
}
return this._getPatchHandler(original, handlerLoadStartTime);
const patchedHandler = this._getPatchHandler(
original,
handlerLoadStartTime
);
if (this._isStreamingHandler(original)) {
// Streaming handlers have special symbols that we need to copy over to the patched handler.
for (const symbol of Object.getOwnPropertySymbols(original)) {
(patchedHandler as unknown as Record<symbol, unknown>)[symbol] = (
original as unknown as Record<symbol, unknown>
)[symbol];
}
}
return patchedHandler;

Comment on lines +99 to +101
// Provide a temporary awslambda polyfill for CommonJS modules during loading
// This prevents ReferenceError when modules use awslambda.streamifyResponse at load time
if (typeof globalThis.awslambda === 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not familiar with polyfill workarounds. Wondering why this is needed inside the Lambda instrumentation? I don't see this method referenced inside this package apart from unit-tests, as your patchedStreamingHandler will just invoke original.apply as a generic way to run the streamified handler, without needing to know it is instance of awslambda.streamifyResponse.

  • Is this polyfill workaround just for the unit-tests?
  • Why will AWS Lambda users will not encounter this ReferenceError when not using Lambda Instrumentation, but will encounter ReferenceError when using Lambda Instrumentation?

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.

AWS Lambda Instrumentation causes lambda failures when using streaming response
4 participants