Skip to content

Commit

Permalink
feat: ability to set lambda latency alarms based on the timeout duration
Browse files Browse the repository at this point in the history
  • Loading branch information
AminFazlMondo committed Sep 4, 2024
1 parent b7587e9 commit 7f78d82
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/monitoring/aws-lambda/LambdaFunctionMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Duration } from "aws-cdk-lib";
import { IFunction } from "aws-cdk-lib/aws-lambda";

import {
Expand All @@ -11,9 +12,13 @@ import {
getLatencyTypeStatistic,
} from "../../common";

export interface IMonitoringFunction extends IFunction {
readonly timeout?: Duration;
}

export interface LambdaFunctionMetricFactoryProps
extends BaseMetricFactoryProps {
readonly lambdaFunction: IFunction;
readonly lambdaFunction: IMonitoringFunction;
/**
* @default - true
*/
Expand All @@ -34,7 +39,7 @@ export interface LambdaFunctionMetricFactoryProps
}

export class LambdaFunctionMetricFactory extends BaseMetricFactory<LambdaFunctionMetricFactoryProps> {
protected readonly lambdaFunction: IFunction;
protected readonly lambdaFunction: IMonitoringFunction;
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;

Expand Down
52 changes: 44 additions & 8 deletions lib/monitoring/aws-lambda/LambdaFunctionMonitoring.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Duration } from "aws-cdk-lib";
import {
GraphWidget,
HorizontalAnnotation,
Expand All @@ -8,6 +9,7 @@ import { CfnFunction, IFunction } from "aws-cdk-lib/aws-lambda";

import { LambdaFunctionEnhancedMetricFactory } from "./LambdaFunctionEnhancedMetricFactory";
import {
IMonitoringFunction,
LambdaFunctionMetricFactory,
LambdaFunctionMetricFactoryProps,
} from "./LambdaFunctionMetricFactory";
Expand All @@ -16,6 +18,7 @@ import {
AlarmFactory,
BaseMonitoringProps,
CountAxisFromZero,
CustomAlarmThreshold,
DefaultGraphWidgetHeight,
DefaultSummaryWidgetHeight,
DurationThreshold,
Expand Down Expand Up @@ -52,11 +55,19 @@ import {
MonitoringNamingStrategy,
} from "../../dashboard";

export interface LambdaLatencyThresholdPercentage extends CustomAlarmThreshold {
readonly maxLatencyPercentage: number;
}

export type LambdaLatencyThreshold =
| LambdaLatencyThresholdPercentage
| LatencyThreshold;

export interface LambdaFunctionMonitoringOptions extends BaseMonitoringProps {
readonly addLatencyP50Alarm?: Record<string, LatencyThreshold>;
readonly addLatencyP90Alarm?: Record<string, LatencyThreshold>;
readonly addLatencyP99Alarm?: Record<string, LatencyThreshold>;
readonly addMaxLatencyAlarm?: Record<string, LatencyThreshold>;
readonly addLatencyP50Alarm?: Record<string, LambdaLatencyThreshold>;
readonly addLatencyP90Alarm?: Record<string, LambdaLatencyThreshold>;
readonly addLatencyP99Alarm?: Record<string, LambdaLatencyThreshold>;
readonly addMaxLatencyAlarm?: Record<string, LambdaLatencyThreshold>;

readonly addFaultCountAlarm?: Record<string, ErrorCountThreshold>;
readonly addFaultRateAlarm?: Record<string, ErrorRateThreshold>;
Expand Down Expand Up @@ -116,6 +127,31 @@ export interface LambdaFunctionMonitoringProps
extends LambdaFunctionMetricFactoryProps,
LambdaFunctionMonitoringOptions {}

function isLatencyThreshold(
props: LambdaLatencyThreshold,
): props is LatencyThreshold {
return typeof (props as LatencyThreshold).maxLatency === "number";
}

function extractLatencyThreshold(
props: LambdaLatencyThreshold,
lambdaFunction: IMonitoringFunction,
): LatencyThreshold {
if (isLatencyThreshold(props)) {
return props as LatencyThreshold;
}
const { maxLatencyPercentage, ...rest } = props;
return {
maxLatency: Duration.seconds(
Math.floor(
(lambdaFunction.timeout?.toSeconds() ?? 0) *
(maxLatencyPercentage / 100),
),
),
...rest,
};
}

export class LambdaFunctionMonitoring extends Monitoring {
readonly title: string;
readonly functionUrl?: string;
Expand Down Expand Up @@ -342,7 +378,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
const createdAlarm = this.latencyAlarmFactory.addLatencyAlarm(
this.p50LatencyMetric,
LatencyType.P50,
alarmProps,
extractLatencyThreshold(alarmProps, props.lambdaFunction),
disambiguator,
);
this.latencyAnnotations.push(createdAlarm.annotation);
Expand All @@ -353,7 +389,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
const createdAlarm = this.latencyAlarmFactory.addLatencyAlarm(
this.p90LatencyMetric,
LatencyType.P90,
alarmProps,
extractLatencyThreshold(alarmProps, props.lambdaFunction),
disambiguator,
);
this.latencyAnnotations.push(createdAlarm.annotation);
Expand All @@ -364,7 +400,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
const createdAlarm = this.latencyAlarmFactory.addLatencyAlarm(
this.p99LatencyMetric,
LatencyType.P99,
alarmProps,
extractLatencyThreshold(alarmProps, props.lambdaFunction),
disambiguator,
);
this.latencyAnnotations.push(createdAlarm.annotation);
Expand All @@ -375,7 +411,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
const createdAlarm = this.latencyAlarmFactory.addLatencyAlarm(
this.maxLatencyMetric,
LatencyType.MAX,
alarmProps,
extractLatencyThreshold(alarmProps, props.lambdaFunction),
disambiguator,
);
this.latencyAnnotations.push(createdAlarm.annotation);
Expand Down

0 comments on commit 7f78d82

Please sign in to comment.