Skip to content

fix(instrumentation-aws-lambda): support Node 24 Lambda without baseDir#3319

Draft
overbalance wants to merge 2 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/lambda-node24-instrumentation
Draft

fix(instrumentation-aws-lambda): support Node 24 Lambda without baseDir#3319
overbalance wants to merge 2 commits intoopen-telemetry:mainfrom
embrace-io:overbalance/lambda-node24-instrumentation

Conversation

@overbalance
Copy link
Contributor

@overbalance overbalance commented Dec 18, 2025

Which problem is this PR solving?

Fixes #3314 cc @mduesterhoeft

The @opentelemetry/instrumentation-aws-lambda package doesn't work on Node 24 Lambda when not using Lambda Layers. The handler function is never patched, resulting in no telemetry being collected.

Root Cause

On Node 24 Lambda, require-in-the-middle cannot identify modules when there's no package.json in the directory hierarchy. This means the instrumentation hooks never fire, regardless of how InstrumentationNodeModuleDefinition is configured.

Solution

Proactively require() and patch the handler module directly in init(). The patched module is cached by Node.js, so when Lambda subsequently requires it, it gets the already-patched version.

Changes:

  • Added proactive require and patch of the handler module in init()
  • Added _patchedHandler and _handlerFunctionName properties to track the patched handler
  • Added disable() override to clean up the proactively patched handler
  • Added regression test with handler in /tmp (truly isolated, no package.json anywhere in path)

Testing Instructions for Issue Reporter (@mduesterhoeft)

To test this fix before it's released, you can install the package directly from this PR branch:

Option 1: Using npm/yarn with git

npm install "git+https://github.com/embrace-io/opentelemetry-js-contrib.git#overbalance/lambda-node24-instrumentation" --workspace=packages/instrumentation-aws-lambda

Option 2: Build locally

  1. Clone the repo and checkout this branch:
    git clone https://github.com/embrace-io/opentelemetry-js-contrib.git
    cd opentelemetry-js-contrib
    git checkout overbalance/lambda-node24-instrumentation
  2. Build the package:
    npm install
    cd packages/instrumentation-aws-lambda
    npm run compile
  3. Copy the built build/ directory to your project's node_modules/@opentelemetry/instrumentation-aws-lambda/

Expected behavior after applying this fix:

You should see the debug log message:

Proactively patched lambda handler

And spans should be created for your Lambda invocations on Node 24.

@overbalance overbalance force-pushed the overbalance/lambda-node24-instrumentation branch from 580ccc0 to 474d252 Compare December 19, 2025 20:06
Comment on lines +207 to +208
// eslint-disable-next-line @typescript-eslint/no-require-imports
const handlerModule = require(filename);

Choose a reason for hiding this comment

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

How does this behave for ESM handlers ? I assume it'll enter the catch and not cause any issues, but I'd like to make sure given we don't really have test coverage for it.

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.

@opentelemetry/instrumentation-aws-lambda not working for Node 24 (when not using Layers)

3 participants