Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add evaluation periods #1183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 56 additions & 12 deletions src/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ export interface WatchLambdaFunctionOptions {
* @default 80
*/
readonly durationThresholdPercent?: number;

/**
* Number of periods to evaluate for the errors alarms.
*
* @default 3
*/
readonly errorsEvaluationPeriods?: number;

/**
* Number of periods to evaluate for the throttles alarms.
*
* @default 3
*/
readonly throttlesEvaluationPeriods?: number;

/**
* Number of periods to evaluate for the duration alarms.
*
* @default 3
*/
readonly durationEvaluationPeriods?: number;
}

export interface WatchLambdaFunctionProps extends WatchLambdaFunctionOptions {
Expand All @@ -40,7 +61,6 @@ export interface WatchLambdaFunctionProps extends WatchLambdaFunctionOptions {
}

export class WatchLambdaFunction extends Construct {

private readonly watchful: IWatchful;
private readonly fn: lambda.Function;
private readonly metrics: LambdaMetricFactory;
Expand All @@ -62,10 +82,22 @@ export class WatchLambdaFunction extends Construct {
],
});

const { errorsMetric, errorsAlarm } = this.createErrorsMonitor(props.errorsPerMinuteThreshold);
const { throttlesMetric, throttlesAlarm } = this.createThrottlesMonitor(props.throttlesPerMinuteThreshold);
const { durationMetric, durationAlarm } = this.createDurationMonitor(timeoutSec, props.durationThresholdPercent);
const invocationsMetric = this.metrics.metricInvocations(this.fn.functionName);
const { errorsMetric, errorsAlarm } = this.createErrorsMonitor(
props.errorsPerMinuteThreshold,
props.errorsEvaluationPeriods,
);
const { throttlesMetric, throttlesAlarm } = this.createThrottlesMonitor(
props.throttlesPerMinuteThreshold,
props.throttlesEvaluationPeriods,
);
const { durationMetric, durationAlarm } = this.createDurationMonitor(
timeoutSec,
props.durationThresholdPercent,
props.durationEvaluationPeriods,
);
const invocationsMetric = this.metrics.metricInvocations(
this.fn.functionName,
);

this.watchful.addWidgets(
new cloudwatch.GraphWidget({
Expand Down Expand Up @@ -94,41 +126,53 @@ export class WatchLambdaFunction extends Construct {
);
}

private createErrorsMonitor(errorsPerMinuteThreshold = 0) {
private createErrorsMonitor(
errorsPerMinuteThreshold = 0,
evaluationPeriods = 3,
) {
const fn = this.fn;
const errorsMetric = this.metrics.metricErrors(fn.functionName);
const errorsAlarm = errorsMetric.createAlarm(this, 'ErrorsAlarm', {
alarmDescription: `Over ${errorsPerMinuteThreshold} errors per minute`,
threshold: errorsPerMinuteThreshold,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
evaluationPeriods: 3,
evaluationPeriods,
});
this.watchful.addAlarm(errorsAlarm);
return { errorsMetric, errorsAlarm };
}

private createThrottlesMonitor(throttlesPerMinuteThreshold = 0) {
private createThrottlesMonitor(
throttlesPerMinuteThreshold = 0,
evaluationPeriods = 3,
) {
const fn = this.fn;
const throttlesMetric = this.metrics.metricThrottles(fn.functionName);
const throttlesAlarm = throttlesMetric.createAlarm(this, 'ThrottlesAlarm', {
alarmDescription: `Over ${throttlesPerMinuteThreshold} throttles per minute`,
threshold: throttlesPerMinuteThreshold,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
evaluationPeriods: 3,
evaluationPeriods,
});
this.watchful.addAlarm(throttlesAlarm);
return { throttlesMetric, throttlesAlarm };
}

private createDurationMonitor(timeoutSec: number, durationPercentThreshold: number = DEFAULT_DURATION_THRESHOLD_PERCENT) {
private createDurationMonitor(
timeoutSec: number,
durationPercentThreshold: number = DEFAULT_DURATION_THRESHOLD_PERCENT,
evaluationPeriods = 3,
) {
const fn = this.fn;
const durationMetric = this.metrics.metricDuration(fn.functionName).p99;
const durationThresholdSec = Math.floor(durationPercentThreshold / 100 * timeoutSec);
const durationThresholdSec = Math.floor(
(durationPercentThreshold / 100) * timeoutSec,
);
const durationAlarm = durationMetric.createAlarm(this, 'DurationAlarm', {
alarmDescription: `p99 latency >= ${durationThresholdSec}s (${durationPercentThreshold}%)`,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
threshold: durationThresholdSec * 1000, // milliseconds
evaluationPeriods: 3,
evaluationPeriods,
});
this.watchful.addAlarm(durationAlarm);
return { durationMetric, durationAlarm };
Expand Down