Skip to content

Commit

Permalink
feat(lambda): include enhanced CPU and memory alarm annotations in da…
Browse files Browse the repository at this point in the history
…shboard (#298)

Closes #292

---

_By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
  • Loading branch information
echeung-amzn authored Jan 13, 2023
1 parent d7a3cd4 commit 901f3f3
Show file tree
Hide file tree
Showing 3 changed files with 1,847 additions and 162 deletions.
2 changes: 2 additions & 0 deletions lib/monitoring/aws-lambda/LambdaFunctionMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
/* eslint-enable @typescript-eslint/no-non-null-assertion */
],
leftYAxis: TimeAxisMillisFromZero,
leftAnnotations: this.cpuTotalTimeAnnotations,
});
}

Expand All @@ -656,6 +657,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
/* eslint-enable @typescript-eslint/no-non-null-assertion */
],
leftYAxis: PercentageAxisFromZeroToHundred,
leftAnnotations: this.memoryUsageAnnotations,
});
}

Expand Down
135 changes: 130 additions & 5 deletions test/monitoring/aws-lambda/LambdaFunctionMonitoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "aws-cdk-lib/aws-lambda";

import { AlarmWithAnnotation, LambdaFunctionMonitoring } from "../../../lib";
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
import { TestMonitoringScope } from "../TestMonitoringScope";

test("snapshot test: no alarms", () => {
Expand Down Expand Up @@ -55,7 +56,7 @@ test("snapshot test: all alarms", () => {

let numAlarmsCreated = 0;

new LambdaFunctionMonitoring(scope, {
const monitoring = new LambdaFunctionMonitoring(scope, {
lambdaFunction,
humanReadableName: "Dummy Lambda for testing",
alarmFriendlyName: "DummyLambda",
Expand Down Expand Up @@ -143,6 +144,7 @@ test("snapshot test: all alarms", () => {
},
});

addMonitoringDashboardsToStack(stack, monitoring);
expect(numAlarmsCreated).toStrictEqual(13);
expect(Template.fromStack(stack)).toMatchSnapshot();
});
Expand Down Expand Up @@ -201,10 +203,11 @@ test("snapshot test: all alarms, alarmPrefix on error dedupeString", () => {

let numAlarmsCreated = 0;

new LambdaFunctionMonitoring(scope, {
const monitoring = new LambdaFunctionMonitoring(scope, {
lambdaFunction,
humanReadableName: "Dummy Lambda for testing",
alarmFriendlyName: "DummyLambda",
lambdaInsightsEnabled: true,
addFaultRateAlarm: {
Warning: {
maxErrorRate: 1,
Expand Down Expand Up @@ -282,14 +285,45 @@ test("snapshot test: all alarms, alarmPrefix on error dedupeString", () => {
maxAgeInMillis: 1_000_000,
},
},
addEnhancedMonitoringMaxCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringP90CpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringAvgCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringMaxMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringP90MemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringAvgMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
useCreatedAlarms: {
consume(alarms: AlarmWithAnnotation[]) {
numAlarmsCreated = alarms.length;
},
},
});

expect(numAlarmsCreated).toStrictEqual(13);
addMonitoringDashboardsToStack(stack, monitoring);
expect(numAlarmsCreated).toStrictEqual(19);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

Expand All @@ -307,10 +341,11 @@ test("snapshot test: all alarms, alarmPrefix on latency dedupeString", () => {

let numAlarmsCreated = 0;

new LambdaFunctionMonitoring(scope, {
const monitoring = new LambdaFunctionMonitoring(scope, {
lambdaFunction,
humanReadableName: "Dummy Lambda for testing",
alarmFriendlyName: "DummyLambda",
lambdaInsightsEnabled: true,
addFaultRateAlarm: {
Warning: {
maxErrorRate: 1,
Expand Down Expand Up @@ -388,13 +423,103 @@ test("snapshot test: all alarms, alarmPrefix on latency dedupeString", () => {
maxAgeInMillis: 1_000_000,
},
},
addEnhancedMonitoringMaxCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringP90CpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringAvgCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringMaxMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringP90MemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringAvgMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
useCreatedAlarms: {
consume(alarms: AlarmWithAnnotation[]) {
numAlarmsCreated = alarms.length;
},
},
});

expect(numAlarmsCreated).toStrictEqual(13);
addMonitoringDashboardsToStack(stack, monitoring);
expect(numAlarmsCreated).toStrictEqual(19);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

test("doesn't create alarms for enhanced Lambda Insights metrics if not enabled", () => {
const stack = new Stack();

const scope = new TestMonitoringScope(stack, "Scope");

const lambdaFunction = new Function(stack, "Function", {
functionName: "DummyLambda",
runtime: Runtime.NODEJS_12_X,
code: InlineCode.fromInline("{}"),
handler: "Dummy::handler",
});

let numAlarmsCreated = 0;

new LambdaFunctionMonitoring(scope, {
lambdaFunction,
humanReadableName: "Dummy Lambda for testing",
alarmFriendlyName: "DummyLambda",
lambdaInsightsEnabled: false,
addEnhancedMonitoringMaxCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringP90CpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringAvgCpuTotalTimeAlarm: {
Warning: {
maxDuration: Duration.millis(100),
},
},
addEnhancedMonitoringMaxMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringP90MemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
addEnhancedMonitoringAvgMemoryUtilizationAlarm: {
Warning: {
maxUsagePercent: 50,
},
},
useCreatedAlarms: {
consume(alarms: AlarmWithAnnotation[]) {
numAlarmsCreated = alarms.length;
},
},
});

expect(numAlarmsCreated).toStrictEqual(0);
});
Loading

0 comments on commit 901f3f3

Please sign in to comment.