feat(instrumentation-aws-lambda): support ESM handlers and other less common handler patterns #2000
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Which problem is this PR solving?
The Lambda instrumentation doesn't currently support instrumenting ESM handlers, which are now the default, and other less common handler patterns supported by Lambda such as extensionless handler modules and nested handler functions.
Short description of the changes
The previous logic to determine the handler to patch is replaced with an implementation based on the one present in the Lambda runtime. I tried to keep the function names and structure as close to possible to the original to make it easier to cross-reference.
The tests also now use code copied from the Lambda runtime to load handlers, as opposed to just calling
require
which doesn't properly reflect a real-world scenario and doesn't work at all for ESM.I also updated the notes about absolute path support in the patching infrastructure. The instrumentation keeps using the same hack but it would probably be a good idea to fix the issue upstream, which is that while absolute paths are taken into account while registering hooks, they are not when matching against the module name within the hook function, which ignores the
baseDir
.