Skip to content
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

feat(instrumentation-aws-lambda): support ESM handlers and other less common handler patterns #2000

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

raphael-theriault-swi
Copy link
Contributor

@raphael-theriault-swi raphael-theriault-swi commented Mar 6, 2024

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.

Copy link

codecov bot commented Mar 6, 2024

Codecov Report

Attention: Patch coverage is 97.43590% with 2 lines in your changes missing coverage. Please review.

Project coverage is 90.66%. Comparing base (80d0c74) to head (cf7d4c3).

Files with missing lines Patch % Lines
...-instrumentation-aws-lambda/src/instrumentation.ts 97.05% 1 Missing ⚠️
...ry-instrumentation-aws-lambda/src/user-function.ts 97.72% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2000      +/-   ##
==========================================
- Coverage   90.79%   90.66%   -0.14%     
==========================================
  Files         169      165       -4     
  Lines        8009     7757     -252     
  Branches     1632     1610      -22     
==========================================
- Hits         7272     7033     -239     
+ Misses        737      724      -13     
Files with missing lines Coverage Δ
...-instrumentation-aws-lambda/src/instrumentation.ts 95.02% <97.05%> (+0.44%) ⬆️
...ry-instrumentation-aws-lambda/src/user-function.ts 97.72% <97.72%> (ø)

... and 7 files with indirect coverage changes

@raphael-theriault-swi

This comment was marked as outdated.

@raphael-theriault-swi raphael-theriault-swi changed the title feat(instrumentation-aws-lambda): support .mjs handlers feat(instrumentation-aws-lambda): support ESM handlers and other less common handler patterns Jun 21, 2024
@raphael-theriault-swi
Copy link
Contributor Author

@JamieDanielson I think you've been pushing a lot for ESM support as a whole, if I rebased this do you think you'd be able to take a look ?

@raphael-theriault-swi
Copy link
Contributor Author

raphael-theriault-swi commented Nov 6, 2024

@jj22ee a review would be much appreciated when you have some time

@jj22ee
Copy link
Contributor

jj22ee commented Nov 13, 2024

Hi, I see that there have been merged changes recently to support ESM handlers while not changing the implementation in instrumentation-aws-lambda. There are fixes related to the issue of baseDir.

It looks like that ESM handlers will be supported once all these changes are released in the latest OTel JS version.
Would your changes still be needed after this?

@raphael-theriault-swi
Copy link
Contributor Author

raphael-theriault-swi commented Nov 13, 2024

The changes wouldn't be needed for ESM support no, and this PR could be updated to take advantage of proper support for absolute path. I'd personally argue for still merging given it supports strictly more valid handler patterns and properly tests support for ESM (which the merged PR did not) and these other patterns.

@trentm sorry to ping you for the 10th time the past couple days, but as the reviewer of the ESM support PR do you have any thoughts wrt what to do with this one ?

@pragmaticivan
Copy link

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.

3 participants