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: handle missing package.json file when checking for version #2450

Merged
merged 8 commits into from Sep 8, 2021

Conversation

nozik
Copy link
Contributor

@nozik nozik commented Sep 3, 2021

Which problem is this PR solving?

  • In some cases (typically, AWS lambda; but can potentially happen in other cases) the package.json file may be missing at runtime for a certain module. Currently OTEL crashes in this case, unnecessarily.
  • See discussion - crash when using aws-lambda instrumentation #2193

Short description of the changes

  • In case the module's version is missing (due to a missing package.json file), we patch the module/file if and only if the supported versions contain a wildcard (*). Until now, an exception was thrown, crashing the app.
  • Added tests that cover these scenarios/

@nozik nozik requested a review from a team as a code owner September 3, 2021 06:50
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require(path.join(baseDir, 'package.json')).version;
const version = existsSync(packageJsonPath) ? require(packageJsonPath).version : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

even if a file exists it may be not readable (e.g. access rights). What about using a try/catch block instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@codecov
Copy link

codecov bot commented Sep 4, 2021

Codecov Report

Merging #2450 (8bc0f66) into main (bec7791) will increase coverage by 0.48%.
The diff coverage is 90.00%.

@@            Coverage Diff             @@
##             main    #2450      +/-   ##
==========================================
+ Coverage   92.71%   93.20%   +0.48%     
==========================================
  Files         137      137              
  Lines        4996     5002       +6     
  Branches     1057     1057              
==========================================
+ Hits         4632     4662      +30     
+ Misses        364      340      -24     
Impacted Files Coverage Δ
...strumentation/src/platform/node/instrumentation.ts 60.25% <90.00%> (+36.64%) ⬆️

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.

I guess it's quite obvious for most people, but I'd add a comment somewhere that we are dealing with packages here for which we haven't detected a version at all. The describe kinda point in that direction, but the setup doesn't make that clear.

Something like

    describe('_onRequire - module version is not available', () => {
    // For all of these cases, there is no indication of the actual module version,
    // so we require there to be a wildcard supported version.

@nozik
Copy link
Contributor Author

nozik commented Sep 6, 2021

@rauno56 👍

@vmarchaud
Copy link
Member

@nozik You'll need to rebase the PR (or give us access to your fork so we can do it for you)

@nozik
Copy link
Contributor Author

nozik commented Sep 6, 2021

@vmarchaud done

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

lgtm, just some suggestion for tests description

@nozik
Copy link
Contributor Author

nozik commented Sep 8, 2021

@obecny Done

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

lgtm, thx for changes

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

Successfully merging this pull request may close these issues.

None yet

6 participants