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): implement require-in-the-middle singleton #3161

Merged
merged 17 commits into from
Oct 20, 2022

Conversation

mhassan1
Copy link
Contributor

@mhassan1 mhassan1 commented Aug 11, 2022

Which problem is this PR solving?

This PR changes the behavior of instrumentation plugins to create a single require patch, instead of one per plugin. This should be a significant performance improvement for projects using many instrumentation plugins.

Fixes #3029.

Short description of the changes

This PR creates a require-in-the-middle singleton that looks for registered onRequire functions for a given module and executes them.

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

This PR is mostly covered by existing tests in InstrumentationBase.test.ts. It adds unit tests for the logic in the _shouldHook function.

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

@mhassan1 mhassan1 requested a review from a team August 11, 2022 19:54
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 11, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: mhassan1 / name: Marc Hassan (d5e4bf2)

@weyert
Copy link
Contributor

weyert commented Aug 11, 2022

Awesome, this sounds promising, thank you for the PR :))

@codecov
Copy link

codecov bot commented Aug 12, 2022

Codecov Report

Merging #3161 (ed6e3ef) into main (0d4c71f) will increase coverage by 0.02%.
The diff coverage is 96.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3161      +/-   ##
==========================================
+ Coverage   93.44%   93.47%   +0.02%     
==========================================
  Files         241      243       +2     
  Lines        7260     7321      +61     
  Branches     1507     1517      +10     
==========================================
+ Hits         6784     6843      +59     
- Misses        476      478       +2     
Impacted Files Coverage Δ
...n/src/platform/node/RequireInTheMiddleSingleton.ts 92.85% <92.85%> (ø)
...nstrumentation/src/platform/node/ModuleNameTrie.ts 100.00% <100.00%> (ø)
...strumentation/src/platform/node/instrumentation.ts 64.13% <100.00%> (+0.39%) ⬆️

@mhassan1 mhassan1 force-pushed the ritm-singleton branch 2 times, most recently from 76fcc07 to 160a2b4 Compare August 12, 2022 13:21
Copy link
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Love this idea. Very excited for this as its something we've been considering for a while

@weyert
Copy link
Contributor

weyert commented Aug 26, 2022

How can we progress this PR? I am really interested to speed up the start up of my projects now feels like it takes a few minutes

@dyladan
Copy link
Member

dyladan commented Sep 6, 2022

I am also interested in getting this PR across the finish line. We are receiving more complaints now that we have more and more instrumentations in auto-instrumentations packages (example: #3229 (reply in thread))

@dyladan
Copy link
Member

dyladan commented Sep 9, 2022

@mhassan1 curious what is required for you to bring this PR into a state ready for review? I have been getting increasing questions about startup cost and this would be a huge win for us.

@mhassan1 mhassan1 marked this pull request as ready for review September 9, 2022 21:27
@mhassan1
Copy link
Contributor Author

mhassan1 commented Sep 9, 2022

I've moved it out of Draft. I see some failing checks that I will address now.

@weyert
Copy link
Contributor

weyert commented Sep 9, 2022

Awesome @mhassan1 looking forward trying it out :)

@mhassan1
Copy link
Contributor Author

I've cleaned up the PR and added more tests. This is ready for review.

@mhassan1 mhassan1 force-pushed the ritm-singleton branch 4 times, most recently from d91b05c to 18bcfea Compare September 10, 2022 14:21
@mhassan1
Copy link
Contributor Author

Unit tests are failing. I will look into it.

@dyladan
Copy link
Member

dyladan commented Sep 12, 2022

With this change we are making a hard promise never to change the interface of this because of the global singleton. IMO we should upgrade this to 1.0 in order to make this promise explicit. @open-telemetry/javascript-maintainers wdyt

@dyladan
Copy link
Member

dyladan commented Sep 20, 2022

I was indeed involved in that PR. It was required in order to instrument aws lambda using a layer.

Thanks for the research. Sounds like a huge performance improvement. Did you happen to step through to see if it is possible to work around the issue in the aws lambda or did you just run the test? If we're intercepting all require calls, shouldn't we be able to match on an exact path the same way ritm does?

@mhassan1
Copy link
Contributor Author

mhassan1 commented Sep 20, 2022

I don't think there's anything that @opentelemetry/instrumentation-aws-lambda can do as a workaround, since RITM will never call its onRequire function for an absolute require when null is passed to RITM. We will need to pass something other than null to RITM, or we will need a patch to RITM to support absolute requires when null is passed.

I also noticed that RITM passes name and basedir a little differently, in this scenario, so we will need to update our logic in RequireInTheMiddleSingleton to handle it (in the current branch's logic, we don't use basedir).

@weyert
Copy link
Contributor

weyert commented Sep 20, 2022

Personally, I think it’s worth it to revisit lambda in the future and merge this PR as it will benefit everyone that’s not using AWS Lambda’s. I can imagine AWS/Amazon can have a closer look at it :)

Yes, I am bias as I can’t use AWS in the cases I am using Opentelemetry but do have slow starts (feels to take minutes) when enabling instrumentation ☺️

@dyladan dyladan self-requested a review September 22, 2022 12:18
@dyladan
Copy link
Member

dyladan commented Sep 22, 2022

I dismissed my review until I have time to review the updated logic. Don't want this to get merged accidentally before that based on my outdated ✅

@mhassan1
Copy link
Contributor Author

mhassan1 commented Sep 28, 2022

@dyladan Is there anything outstanding that is blocking this PR?

@dyladan
Copy link
Member

dyladan commented Sep 28, 2022

@dyladan Is there anything outstanding that is blocking this PR?

Nothing particular just trying to make sure it's sufficiently reviewed because it's quite fundamental to the instrumentation. I asked more people to review it at the SIG meeting today.

@dyladan dyladan self-requested a review October 5, 2022 15:52
Copy link
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Requesting changes here so this isn't accidentally merged before @rauno56 comment is addressed

@dyladan dyladan self-requested a review October 20, 2022 19:12
Copy link
Member

@rauno56 rauno56 left a comment

Choose a reason for hiding this comment

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

Fantastic job, @mhassan1. Thanks for the work and your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[@opentelemetry/instrumentation] require performance grows linearly with each instrumentation plugin
8 participants