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

fix(instrumentation-node): recursively discover package.json #2206

Conversation

adambartholomew
Copy link

Recursively discover package.json from module parent directories

Which problem is this PR solving?

Fixes #2193

Short description of the changes

  • Recursively attempting to discover package.json from parent directories in order to set a moduleVersion
  • Resolving as 0.0.0 when undiscovered rather than a hard crash

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 13, 2021

CLA Signed

The committers are authorized under a signed CLA.

protected getModuleVersion(directory: string): string {
const modulePackage = this.findModulePackage(directory);
if (!modulePackage) {
return '0.0.0';
Copy link
Member

Choose a reason for hiding this comment

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

i don't think we should return a invalid version if the version isn't found, i believe module.moduleVersion can already be undefined

Copy link
Author

Choose a reason for hiding this comment

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

version is used further down with isSupported and ultimately semver.satisfies(). I'm not familiar enough to determine the behaviour if we were to let undefined into these methods.

@codecov
Copy link

codecov bot commented May 13, 2021

Codecov Report

Merging #2206 (b6da388) into main (e379e59) will decrease coverage by 0.23%.
The diff coverage is 100.00%.

❗ Current head b6da388 differs from pull request most recent head 159eb90. Consider uploading reports for the commit 159eb90 to get more accurate results

@@            Coverage Diff             @@
##             main    #2206      +/-   ##
==========================================
- Coverage   92.72%   92.49%   -0.24%     
==========================================
  Files         141      124      -17     
  Lines        5089     4146     -943     
  Branches     1047      848     -199     
==========================================
- Hits         4719     3835     -884     
+ Misses        370      311      -59     
Impacted Files Coverage Δ
...strumentation/src/platform/node/instrumentation.ts 47.12% <100.00%> (+23.51%) ⬆️
packages/opentelemetry-web/src/types.ts
packages/opentelemetry-web/src/utils.ts
...lemetry-exporter-collector/src/transformMetrics.ts
...ntelemetry-web/src/enums/PerformanceTimingNames.ts
...es/opentelemetry-context-zone-peer-dep/src/util.ts
...-instrumentation-fetch/src/enums/AttributeNames.ts
...kages/opentelemetry-exporter-collector/src/util.ts
...mentation-xml-http-request/src/enums/EventNames.ts
...ry-context-zone-peer-dep/src/ZoneContextManager.ts
... and 9 more

@@ -56,4 +57,62 @@ describe('BaseInstrumentation', () => {
assert.strictEqual(called, true);
});
});

describe('_onRequire', () => {
Copy link
Member

Choose a reason for hiding this comment

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

it should not be added here but in node test only. Common is run for browser and for node

Copy link
Author

Choose a reason for hiding this comment

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

This explains the CI failure. The browser tests won't run on my machine. I'll move these tests. Apologies for being new, appreciate all the help.

Copy link
Member

Choose a reason for hiding this comment

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

No need to apology, we are here to help :), in tests you have folders: browser, node, common, just move your tests to folder node. This way your tests will be run only in node environment

@dyladan
Copy link
Member

dyladan commented May 13, 2021

Tell me if i'm wrong but recursively crawling up the node_modules directory tree is quite error prone. Picture an application which has 2 instrumentations which each depend on a not-quite-equal version of @opentelemetry/instrumentation. You then could end up with:

  • app/node_modules/instr-1/node_modules/@opentelemetry/instrumenatation
  • app/node_modules/instr-1/package.json
  • app/node_modules/instr-2/node_modules/@opentelemetry/instrumenatation
  • app/node_modules/instr-2/package.json

If you try to crawl up from either of these instrumentation modules, you will hit the instrumentation itself before you hit the user application.

If you have 2 instrumentations which agree on an instrumentation version though, you might have this:

  • app/node_modules/instr-1/package.json
  • app/node_modules/instr-2/package.json
  • app/node_modules/@opentelemetry/instrumenatation

If you crawl up from this instrumentation (deduplicated by npm) you will hit the user application, not the instrumentation you were looking for.

This is assuming you don't have an even more complex installation potentially involving metapackages which install many instrumentations.

@adambartholomew
Copy link
Author

I tend to agree this could be problematic. Is there a better approach for this? In the bug the issue was for a require within the application, not the instrumentation. Is the application package.json version number important for this tooling? Is there a way to safely bypass looking for a package file, or capture the exception?

@dyladan
Copy link
Member

dyladan commented May 14, 2021

Maybe we can change the logic to look through the supported module versions to see if the package name matches any of them before we even attempt to look for a version. We only need to look for the version if the name matches (and there is a version to check against).

@adambartholomew
Copy link
Author

Lots of good discussions in the github issue. This doesn't seem like the correct approach.
#2193

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.

crash when using aws-lambda instrumentation
4 participants