Skip to content

Commit 7d54dcc

Browse files
committed
Asynchronously detect if there are duplicate installations of the Lambda library, and log a warning if so.
1 parent 79c6a8a commit 7d54dcc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import {
2121
setSandboxInit,
2222
setLogger,
2323
setLogLevel,
24+
logWarning,
2425
} from "./utils";
2526
import { getEnhancedMetricTags } from "./metrics/enhanced-metrics";
2627
import { DatadogTraceHeaders } from "./trace/context/extractor";
2728
import { SpanWrapper } from "./trace/span-wrapper";
2829
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";
30+
import path from "path";
31+
import fs from "fs";
2932

3033
// Backwards-compatible export, TODO deprecate in next major
3134
export { DatadogTraceHeaders as TraceHeaders } from "./trace/context/extractor";
@@ -127,6 +130,15 @@ export function datadog<TEvent, TResult>(
127130

128131
const traceListener = new TraceListener(finalConfig);
129132

133+
// Check for duplicate installations of the Lambda library
134+
detectDuplicateInstallations().then((duplicateFound) => {
135+
if (duplicateFound) {
136+
logWarning(
137+
`Detected duplicate installations of datadog-lambda-js. This can cause: (1) increased cold start times, (2) broken metrics, and (3) other unexpected behavior. Please use either the Lambda layer version or the package in node_modules, but not both. See: https://docs.datadoghq.com/serverless/aws_lambda/installation/nodejs/?tab=custom`,
138+
);
139+
}
140+
});
141+
130142
// Only wrap the handler once unless forced
131143
const _ddWrappedKey = "_ddWrapped";
132144
if ((handler as any)[_ddWrappedKey] !== undefined && !finalConfig.forceWrap) {
@@ -465,3 +477,23 @@ export async function emitTelemetryOnErrorOutsideHandler(
465477
await metricsListener.onCompleteInvocation();
466478
}
467479
}
480+
481+
async function detectDuplicateInstallations() {
482+
try {
483+
const layerPath = "/opt/nodejs/node_modules/datadog-lambda-js";
484+
const localPath = path.join(process.cwd(), "node_modules/datadog-lambda-js");
485+
486+
const checkPathExistsAsync = (libraryPath: string): Promise<boolean> =>
487+
new Promise((resolve) => fs.access(libraryPath, (err) => resolve(!err)));
488+
489+
const [layerExists, localExists] = await Promise.all([
490+
checkPathExistsAsync(layerPath),
491+
checkPathExistsAsync(localPath),
492+
]);
493+
494+
return layerExists && localExists;
495+
} catch (err) {
496+
logDebug("Failed to check for duplicate installations.");
497+
return false;
498+
}
499+
}

0 commit comments

Comments
 (0)