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

Detect and warn if duplicate lambda libraries are detected #585

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

Conversation

nhulston
Copy link
Contributor

@nhulston nhulston commented Oct 25, 2024

What does this PR do?

Asynchronously detects if the Lambda library is installed both in node_modules and as a Lambda library. If so, it logs a warning.

Motivation

It's common for users to install the Lambda layers as instructed by the documentation, but then then see import errors in their code, causing them to run npm install datadog-lambda-js. Now they will have the Lambda library installed in two places, which will (1) increase package size and cold start duration, and (2) break custom metrics.

We've had many customers open support tickets recently about broken metrics caused by this issue, so this PR aims to avoid this problem in the future.

Testing Guidelines

Manual tests: logs a warning as expected when installed in both places, and doesn't log any warning if only installed in the layer or node_modules.

Added unit tests.

Additional Notes

This works by using fs and checking if the paths exist. I'm assuming the paths will always be:

  • Layer: '/opt/nodejs/node_modules/datadog-lambda-js'
  • Node modules: path.join(process.cwd(), 'node_modules/datadog-lambda-js')

We could use process.env.NODE_PATH and search for datadog-lambda-js/package.json in the paths. However, my solution is simpler and works perfectly fine. It is also faster than relying on node path because we don't have to parse the node path.

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@nhulston nhulston force-pushed the nicholas.hulston/warn-if-duplicated-lambda-libraries-detected branch from cc6b8da to 97937ec Compare October 25, 2024 21:45
Comment on lines +137 to +138
detectDuplicateInstallations()
.then((duplicateFound) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put this under the datadog() wrapper, but I'm not sure if this is the best place to make this call. If there's somewhere that's better, I'm open to suggestions!

@nhulston nhulston marked this pull request as ready for review October 28, 2024 14:23
@nhulston nhulston requested a review from a team as a code owner October 28, 2024 14:23
@nhulston nhulston marked this pull request as draft January 31, 2025 20:14
@nhulston nhulston marked this pull request as ready for review January 31, 2025 20:27
// Check for duplicate installations of the Lambda library
detectDuplicateInstallations()
.then((duplicateFound) => {
if (duplicateFound) {
Copy link
Contributor Author

@nhulston nhulston Jan 31, 2025

Choose a reason for hiding this comment

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

Also, this code might be relevant, so wanted to share for reviewers:

export class TracerWrapper {
private tracer: any;
constructor() {
try {
// Try and use the same version of the tracing library the user has installed.
// This handles edge cases where two versions of dd-trace are installed, one in the layer
// and one in the user's code.
const path = require.resolve("dd-trace", { paths: ["/var/task/node_modules", ...module.paths] });
this.tracer = require(path);
return;
} catch (err) {
if (err instanceof Object || err instanceof Error) {
logDebug("Couldn't require dd-trace from main", err);
}
}
}

Note that this code only loads in one version of dd-trace, not to be confused with the datadog-lambda-js; this PR detects duplicate installations of datadog-lambda-js

Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if there's a way for us to use similar logic like this one 🤔

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.

2 participants