Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ To further configure your plugin, use the following custom parameters in your `s
| `llmObsEnabled` | Toggle to enable submitting data to LLM Observability. Defaults to `false`. |
| `llmObsMlApp` | The name of your LLM application, service, or project, under which all traces and spans are grouped. This helps distinguish between different applications or experiments. See [Application naming guidelines](https://docs.datadoghq.com/llm_observability/sdk/?tab=nodejs#application-naming-guidelines) for allowed characters and other constraints. To override this value for a given root span, see [Tracing multiple applications](https://docs.datadoghq.com/llm_observability/sdk/?tab=nodejs#tracing-multiple-applications). Required if `llmObsEnabled` is `true` |
| `llmObsAgentlessEnabled` | Only required if you are not using the Datadog Lambda Extension, in which case this should be set to `true`. Defaults to `false`. |
| `suppressLowMemoryWarning` | When set to `true`, silences the warning the plugin prints for each instrumented function with less than 256 MB of memory (reason `lambda_low_memory`). The plugin still instruments these functions. The Datadog Lambda Extension and tracer add memory overhead (about 70 MB for Python), which can cause out-of-memory errors and timeouts. To determine a function's memory, the plugin uses the function's `memorySize`, then `provider.memorySize`, then the Serverless Framework default of 1024 MB. Defaults to `false`. |

To use any of these parameters, add a `custom` > `datadog` section to your `serverless.yml` similar to this example:

Expand Down
16 changes: 16 additions & 0 deletions src/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ describe("getConfig", () => {
subscribeToAccessLogs: true,
subscribeToExecutionLogs: false,
subscribeToStepFunctionLogs: false,
suppressLowMemoryWarning: false,
enableSourceCodeIntegration: true,
uploadGitMetadata: true,
failOnError: false,
Expand Down Expand Up @@ -234,6 +235,7 @@ describe("getConfig", () => {
subscribeToAccessLogs: true,
subscribeToExecutionLogs: false,
subscribeToStepFunctionLogs: false,
suppressLowMemoryWarning: false,
enableSourceCodeIntegration: true,
uploadGitMetadata: true,
failOnError: false,
Expand Down Expand Up @@ -275,6 +277,7 @@ describe("getConfig", () => {
subscribeToAccessLogs: true,
subscribeToExecutionLogs: false,
subscribeToStepFunctionLogs: false,
suppressLowMemoryWarning: false,
customHandler: "/src/custom-handler.handler",
enableSourceCodeIntegration: true,
uploadGitMetadata: true,
Expand Down Expand Up @@ -318,6 +321,7 @@ it("disable source code integration", () => {
subscribeToAccessLogs: true,
subscribeToExecutionLogs: false,
subscribeToStepFunctionLogs: false,
suppressLowMemoryWarning: false,
enableSourceCodeIntegration: false,
uploadGitMetadata: true,
failOnError: false,
Expand Down Expand Up @@ -359,6 +363,7 @@ it("disable git metadata upload", () => {
subscribeToAccessLogs: true,
subscribeToExecutionLogs: false,
subscribeToStepFunctionLogs: false,
suppressLowMemoryWarning: false,
enableSourceCodeIntegration: true,
uploadGitMetadata: false,
failOnError: false,
Expand Down Expand Up @@ -2349,3 +2354,14 @@ describe("setEnvConfiguration", () => {
});
});
});

describe("suppressLowMemoryWarning", () => {
it("defaults to false", () => {
expect(getConfig({ custom: {} } as any).suppressLowMemoryWarning).toBe(false);
});

it("can be set to true", () => {
const result = getConfig({ custom: { datadog: { suppressLowMemoryWarning: true } } } as any);
expect(result.suppressLowMemoryWarning).toBe(true);
});
});
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export interface Configuration {

// Only required if you are not using the Datadog Agent.
llmObsAgentlessEnabled?: boolean;

// When set to `true`, the plugin does not warn about instrumented functions with less
// than LOW_MEMORY_THRESHOLD_MB of memory. Defaults to `false`.
suppressLowMemoryWarning?: boolean;
}
const webpackPluginName = "serverless-webpack";
const apiKeyEnvVar = "DD_API_KEY";
Expand Down Expand Up @@ -213,6 +217,7 @@ export const defaultConfiguration: Configuration = {
propagateUpstreamTrace: false,
enableStepFunctionsTracing: false,
redirectHandlers: true,
suppressLowMemoryWarning: false,
};

export function setEnvConfiguration(
Expand Down
147 changes: 147 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,153 @@ describe("ServerlessPlugin", () => {
});
});

describe("low-memory warning", () => {
const lowMemoryWarning = (name: string, memorySize: number) =>
`Warning: Function ${name} has ${memorySize} MB of memory, below the recommended minimum of 256 MB (lambda_low_memory). The Datadog Lambda Extension and tracer add memory overhead (about 70 MB for Python), which can cause out-of-memory errors and timeouts. Increase the function's memory to at least 256 MB, or set custom.datadog.suppressLowMemoryWarning to true to silence this warning.`;

afterEach(() => {
mock.restore();
});

async function runBeforePackage(
functions: Record<string, Record<string, unknown>>,
provider: Record<string, unknown> = {},
datadog: Record<string, unknown> = {},
): Promise<{ logs: string[]; serverless: any }> {
mock({});
const logs: string[] = [];
const serverless = {
cli: { log: (message: string) => logs.push(message) },
getProvider: (_name: string) => awsMock(),
service: {
getServiceName: () => "dev",
provider: { region: "us-east-1", ...provider },
functions,
custom: { datadog: { apiKey: 1234, ...datadog } },
},
};
const plugin = new ServerlessPlugin(serverless, {});
await plugin.hooks["before:package:createDeploymentArtifacts"]();
return { logs, serverless };
}

it("warns for a function below 256 MB and still instruments it", async () => {
const { logs, serverless } = await runBeforePackage({
python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 },
});
expect(logs).toContain(lowMemoryWarning("python1", 128));
expect(serverless.service.functions.python1.layers).toEqual([
expect.stringMatching(/arn:aws:lambda:us-east-1:.*:layer:Datadog-Python312:.*/),
expect.stringMatching(/arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Extension:.*/),
]);
});

it("does not warn for a function with exactly 256 MB", async () => {
const { logs } = await runBeforePackage({
python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 256 },
});
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([]);
});

it("inherits provider.memorySize when the function doesn't set it", async () => {
const { logs } = await runBeforePackage(
{
python1: { handler: "my-func.ev", runtime: "python3.12" },
python2: { handler: "my-func.ev", runtime: "python3.12", memorySize: 512 },
},
{ memorySize: 128 },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([lowMemoryWarning("python1", 128)]);
});

it("does not warn when memorySize is unset (Serverless default of 1024 MB)", async () => {
const { logs } = await runBeforePackage({
python1: { handler: "my-func.ev", runtime: "python3.12" },
});
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([]);
});

it("does not warn when memorySize is not a plain number", async () => {
const { logs } = await runBeforePackage(
{
python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: "${self:custom.memory}" },
python2: { handler: "my-func.ev", runtime: "python3.12", memorySize: { Ref: "Memory" } },
python3: { handler: "my-func.ev", runtime: "python3.12" },
},
{ memorySize: "${param:memory}" },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([]);
});

it("does not warn when suppressLowMemoryWarning is true", async () => {
const { logs, serverless } = await runBeforePackage(
{ python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 } },
{},
{ suppressLowMemoryWarning: true },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([]);
expect(serverless.service.functions.python1.layers).toHaveLength(2);
});

it("does not warn for an excluded function", async () => {
const { logs } = await runBeforePackage(
{
python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 },
python2: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 },
},
{},
{ exclude: ["python1"] },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([lowMemoryWarning("python2", 128)]);
});

it("does not warn when addLayers and addExtension are both false", async () => {
const { logs, serverless } = await runBeforePackage(
{ python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 } },
{},
{ addLayers: false, addExtension: false, flushMetricsToLogs: true },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([]);
expect(serverless.service.functions.python1.layers).toBeUndefined();
});

it("does not warn for Java or .NET functions when addExtension is false", async () => {
const { logs, serverless } = await runBeforePackage(
{
java1: { handler: "my-func.ev", runtime: "java21", memorySize: 128 },
dotnet1: { handler: "my-func.ev", runtime: "dotnet8", memorySize: 128 },
python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 },
},
{},
{ addExtension: false, flushMetricsToLogs: true },
);
expect(logs.filter((log) => log.includes("lambda_low_memory"))).toEqual([lowMemoryWarning("python1", 128)]);
expect(serverless.service.functions.java1.layers).toBeUndefined();
expect(serverless.service.functions.dotnet1.layers).toBeUndefined();
});

it("warns when addLayers is false but the extension is added", async () => {
const { logs, serverless } = await runBeforePackage(
{ python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 } },
{},
{ addLayers: false },
);
expect(logs).toContain(lowMemoryWarning("python1", 128));
expect(serverless.service.functions.python1.layers).toEqual([
expect.stringMatching(/arn:aws:lambda:us-east-1:464622532012:layer:Datadog-Extension:.*/),
]);
});

it("does not warn when the plugin is disabled", async () => {
const { logs } = await runBeforePackage(
{ python1: { handler: "my-func.ev", runtime: "python3.12", memorySize: 128 } },
{},
{ enabled: false },
);
expect(logs).toEqual([]);
});
});

describe("validateConfiguration", () => {
afterEach(() => {
mock.restore();
Expand Down
32 changes: 30 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
applyLambdaLibraryLayers,
findHandlers,
FunctionInfo,
getLowMemoryWarnings,
RuntimeType,
getDefaultIsFIPSEnabledFlag,
} from "./layer";
Expand Down Expand Up @@ -147,10 +148,20 @@ module.exports = class ServerlessPlugin {

const allLayers = { regions: { ...layers.regions, ...govLayers.regions } };
const accountId = config.useLayersFromAccount;
// Functions that receive a library layer or the extension layer, used for the low-memory warning.
const layeredHandlers = new Set<FunctionInfo>();
if (config.addLayers) {
this.serverless.cli.log("Adding Lambda Library Layers to functions");
this.debugLogHandlers(handlers);
applyLambdaLibraryLayers(this.serverless.service, handlers, allLayers, accountId, config.addExtension);
for (const handler of applyLambdaLibraryLayers(
this.serverless.service,
handlers,
allLayers,
accountId,
config.addExtension,
)) {
layeredHandlers.add(handler);
}
if (hasWebpackPlugin(this.serverless.service)) {
forceExcludeDepsFromWebpack(this.serverless.service);
}
Expand All @@ -163,11 +174,28 @@ module.exports = class ServerlessPlugin {
this.debugLogHandlers(handlers);
const isFIPSEnabled =
config.isFIPSEnabled ?? getDefaultIsFIPSEnabledFlag(config, this.serverless.service.provider.region);
applyExtensionLayer(this.serverless.service, handlers, allLayers, accountId, isFIPSEnabled);
for (const handler of applyExtensionLayer(
this.serverless.service,
handlers,
allLayers,
accountId,
isFIPSEnabled,
)) {
layeredHandlers.add(handler);
}
} else {
this.serverless.cli.log("Skipping adding Lambda Extension Layer");
}

// Warn only: low-memory functions are still instrumented as before. Functions that got no
// Datadog layer carry no Datadog memory overhead, so they are not warned about.
if (config.suppressLowMemoryWarning !== true) {
const warnedHandlers = handlers.filter((handler) => layeredHandlers.has(handler));
for (const warning of getLowMemoryWarnings(this.serverless.service, warnedHandlers)) {
this.logToCliOnce(`Warning: ${warning}`);
}
}

if (config.addExtension) {
this.serverless.cli.log("Adding Datadog Env Vars");
this.addDDEnvVars(handlers);
Expand Down
Loading
Loading