Skip to content

Conversation

@RaphaelManke
Copy link
Contributor

@RaphaelManke RaphaelManke commented Nov 26, 2025

Description

This PR fixes issue #3251 by adding runtime-aware handler support that automatically detects the Node.js version from AWS_EXECUTION_ENV and adapts handler signatures accordingly.

Fixes: #3251

Problem

AWS Lambda deprecated callback-based handlers in Node.js 24 runtime, causing the instrumentation to fail with:

Runtime.CallbackHandlerDeprecated: ERROR: AWS Lambda has removed support for callback-based function handlers starting with Node.js 24

Solution

The instrumentation now automatically detects the Node.js runtime version and adapts:

  • Node.js 24+: Handler signature is (event, context) - no callback parameter (respects AWS deprecation)
  • Node.js 22 and lower: Handler signature is (event, context, callback?) - supports both callback and Promise-based handlers for backward compatibility

I tested it by building a lambda layer with this code version.

Changes

  • Runtime Detection: Added getNodeRuntimeVersion() function to detect Node.js version from AWS_EXECUTION_ENV environment variable
  • Runtime-Aware Handlers:
    • Node.js 24+: Only Promise-based handlers (callbacks not allowed)
    • Node.js 22 and lower: Both callback and Promise-based handlers supported
  • Backward Compatibility: Re-added _wrapCallback() method for callback support on older runtimes
  • Tests: Added callback-based handler tests that run when simulating Node.js 22 runtime

Testing

  • ✅ All 53 tests passing (including 3 new callback-based handler tests)
  • ✅ Code coverage: 93.28% statements, 83.11% branches, 97.07% functions, 96.87% lines
  • ✅ No linting errors
  • ✅ Verified backward compatibility on Node.js 22 and lower
  • ✅ Verified Node.js 24+ compliance (no callback parameter)

Backward Compatibility

This change maintains full backward compatibility:

  • Existing callback-based handlers continue to work on Node.js 22 and lower
  • Promise-based handlers work on all Node.js versions
  • Node.js 24+ enforces Promise-based handlers only (as per AWS Lambda deprecation)

Related Links

BREAKING CHANGE: Removed support for callback-based Lambda handlers.
Only Promise-based handlers (async/await or functions returning Promises)
are now supported. This aligns with AWS Lambda's deprecation of callbacks
in Node.js 24 runtime.

Changes:
- Removed Callback import and _wrapCallback method from instrumentation
- Updated patchedHandler to only accept (event, context) parameters
- Converted all test handlers from callback-based to Promise-based
- Updated all tests to use Promise-based handlers directly
- Added migration guide in README.md
- Documented breaking change in CHANGELOG.md

All 50 tests passing.
…ith backward compatibility

Added runtime-aware handler support that automatically detects Node.js version from AWS_EXECUTION_ENV and adapts handler signatures accordingly. Node.js 24+ only supports Promise-based handlers, while Node.js 22 and lower support both callback and Promise-based handlers for backward compatibility.
Copy link
Member

@maxday maxday left a comment

Choose a reason for hiding this comment

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

Thanks @RaphaelManke
I left minor comments, feel free to ignore

- Refactor getNodeRuntimeVersion() to isSupportingCallbacks() returning boolean
- Default to false (no callbacks) when AWS_EXECUTION_ENV is not set
- Update README example to use ESM syntax for Promise-based handler
};

exports.stringerror = function (event, context, callback) {
exports.callbackstringerror = async function (event, context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I suppose the "callback" name in this test fixture is a bit out of place now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah this is a leftover from an intermediate state. Deleted it as its covered in another test

@maxday
Copy link
Member

maxday commented Nov 26, 2025

Confirming it's working fine by

  1. building this package
  2. doing some magic with npm pack
  3. modifying https://github.com/open-telemetry/opentelemetry-lambda to use the packed node_modules
  4. running npm run build from the nodejs folder in https://github.com/open-telemetry/opentelemetry-lambda
  5. uploading the layer to my test AWS account
  6. creating a simple hello world function using nodejs 24
  7. attaching the layer
  8. setting AWS_LAMBDA_EXEC_WRAPPER="/opt/otel-handler"

was failing before, succeeding with this change

Copy link
Member

@maxday maxday left a comment

Choose a reason for hiding this comment

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

Thanks @RaphaelManke 💯

- Remove duplicate callbackstringerror handler (same as stringerror)
- Remove callbackStringError test (strings are not valid for Lambda callbacks)
- Update test to use stringerror instead of callbackstringerror
@RaphaelManke RaphaelManke force-pushed the remove-lambda-callback-support branch from 59f0dae to 3564271 Compare November 26, 2025 20:50

exports.callbackerror = function (event, context, callback) {
callback(new Error('handler error'));
exports.callbackerror = async function (event, context) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: have exports.callbackerror and exports.callbackError :) This one here is currently a bit misleading. Minor though.

@trentm trentm enabled auto-merge (squash) November 26, 2025 22:00
@trentm trentm merged commit bddcd78 into open-telemetry:main Nov 26, 2025
21 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Nov 26, 2025

Thank you for your contribution @RaphaelManke! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

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.

4 participants