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
110 changes: 110 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,116 @@ 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 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
8 changes: 8 additions & 0 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 @@ -137,6 +138,13 @@ module.exports = class ServerlessPlugin {
const defaultRuntime = this.serverless.service.provider.runtime;
const handlers = findHandlers(this.serverless.service, config.exclude, defaultRuntime);

// Warn only: low-memory functions are still instrumented as before.
if (config.suppressLowMemoryWarning !== true) {
for (const warning of getLowMemoryWarnings(this.serverless.service, handlers)) {
this.logToCliOnce(`Warning: ${warning}`);
}
}
Comment on lines +142 to +146

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This runs before checking config.addLayers / config.addExtension, and getLowMemoryWarnings only skips RuntimeType.UNSUPPORTED. As a result, functions that get no Datadog layers still warn -- for example:

  • A 128 MB Python function with addLayers: false and addExtension: false receives no layers but still prints a warning blaming extension and tracer layer overhead.
  • A 128 MB .NET or Java function with addExtension: false receives no layers (since applyLambdaLibraryLayers skips those runtimes without the extension) but still warns.

This also differs from the PR description ("those the plugin doesn't add layers to, are skipped"). Consider gating on config.addLayers || config.addExtension and skipping extension-only runtimes when !config.addExtension (or updating the PR description if warning unconditionally is intended).


// Don't assign `provider.environment` here: without the extension nothing else creates it, and an empty one
// would add an Environment block to excluded functions. (addDDEnvVars creates it when the extension is used.)
const providerEnvironment = (this.serverless.service.provider as Provider).environment as
Expand Down
41 changes: 41 additions & 0 deletions src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,47 @@ export function findHandlers(service: Service, exclude: string[], defaultRuntime
) as FunctionInfo[];
}

// Functions with less memory than this can run out of memory or time out once the Datadog
// Lambda Extension and tracer layers are added (about 70 MB of overhead for Python).
export const LOW_MEMORY_THRESHOLD_MB = 256;
export const LOW_MEMORY_REASON = "lambda_low_memory";
// Serverless Framework v1 through v4 default `memorySize` to 1024 MB when neither the
// function nor the provider sets it.
export const SERVERLESS_DEFAULT_MEMORY_SIZE_MB = 1024;

/**
* Returns the memory size the function is deployed with: `functions.<fn>.memorySize`, then
* `provider.memorySize`, then the Serverless Framework default. Returns undefined when the
* value is not a plain number (for example, an unresolved variable or an object).
*/
export function getEffectiveMemorySize(service: Service, handler: FunctionInfo): number | undefined {
const memorySize: unknown =
handler.handler.memorySize ??
(service.provider as unknown as { memorySize?: unknown }).memorySize ??
SERVERLESS_DEFAULT_MEMORY_SIZE_MB;
return typeof memorySize === "number" && Number.isFinite(memorySize) ? memorySize : undefined;
}

/**
* Returns a warning for each instrumented function with less than LOW_MEMORY_THRESHOLD_MB of memory.
* Functions with an unsupported runtime get no Datadog layers, so they are skipped.
*/
export function getLowMemoryWarnings(service: Service, handlers: FunctionInfo[]): string[] {
const warnings: string[] = [];
for (const handler of handlers) {
if (handler.type === RuntimeType.UNSUPPORTED) {
continue;
}
const memorySize = getEffectiveMemorySize(service, handler);
if (memorySize !== undefined && memorySize < LOW_MEMORY_THRESHOLD_MB) {
warnings.push(
`Function ${handler.name} has ${memorySize} MB of memory, below the recommended minimum of ${LOW_MEMORY_THRESHOLD_MB} MB (${LOW_MEMORY_REASON}). 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 ${LOW_MEMORY_THRESHOLD_MB} MB, or set custom.datadog.suppressLowMemoryWarning to true to silence this warning.`,
);
}
}
return warnings;
}

/**
* Normalize the runtime in the yml to match our layers.json keys
* For most runtimes the key in layers.json is the same as the string set in the
Expand Down
Loading