Skip to content

Commit 0d25cba

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

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import {
2020
promisifiedHandler,
2121
setSandboxInit,
2222
setLogger,
23-
setLogLevel,
23+
setLogLevel, logWarning,
2424
} from "./utils";
2525
import { getEnhancedMetricTags } from "./metrics/enhanced-metrics";
2626
import { DatadogTraceHeaders } from "./trace/context/extractor";
2727
import { SpanWrapper } from "./trace/span-wrapper";
2828
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";
29+
import path from "path";
30+
import fs from "fs";
31+
import * as sea from "node:sea";
2932

3033
// Backwards-compatible export, TODO deprecate in next major
3134
export { DatadogTraceHeaders as TraceHeaders } from "./trace/context/extractor";
@@ -127,6 +130,13 @@ 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(`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`)
137+
}
138+
})
139+
130140
// Only wrap the handler once unless forced
131141
const _ddWrappedKey = "_ddWrapped";
132142
if ((handler as any)[_ddWrappedKey] !== undefined && !finalConfig.forceWrap) {
@@ -465,3 +475,23 @@ export async function emitTelemetryOnErrorOutsideHandler(
465475
await metricsListener.onCompleteInvocation();
466476
}
467477
}
478+
479+
async function detectDuplicateInstallations() {
480+
try {
481+
const layerPath = '/opt/nodejs/node_modules/datadog-lambda-js';
482+
const localPath = path.join(process.cwd(), 'node_modules/datadog-lambda-js');
483+
484+
const checkPathExistsAsync = (libraryPath: string): Promise<boolean> =>
485+
new Promise(resolve => fs.access(libraryPath, err => resolve(!err)));
486+
487+
const [layerExists, localExists] = await Promise.all([
488+
checkPathExistsAsync(layerPath),
489+
checkPathExistsAsync(localPath)
490+
]);
491+
492+
return layerExists && localExists;
493+
} catch (err) {
494+
logDebug('Failed to check for duplicate installations.');
495+
return false;
496+
}
497+
}

0 commit comments

Comments
 (0)