From 3cf469540441d226e16d763739c916341cd6ba50 Mon Sep 17 00:00:00 2001 From: kazuho cryer-shinozuka Date: Tue, 13 Jun 2023 10:20:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20evaluationPeriods=E3=82=92=E5=BC=95?= =?UTF-8?q?=E6=95=B0=E3=81=AB=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 12 +++++++++ src/lambda.ts | 68 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/API.md b/API.md index 153a9761..704384f4 100644 --- a/API.md +++ b/API.md @@ -154,8 +154,11 @@ new WatchLambdaFunction(scope: Construct, id: string, props: WatchLambdaFunction * **scope** ([Construct](#constructs-construct)) *No description* * **id** (string) *No description* * **props** ([WatchLambdaFunctionProps](#cdk-watchful-watchlambdafunctionprops)) *No description* + * **durationEvaluationPeriods** (number) Number of periods to evaluate for the duration alarms. __*Default*__: 3 * **durationThresholdPercent** (number) Threshold for the duration alarm as percentage of the function's timeout value. __*Default*__: 80 + * **errorsEvaluationPeriods** (number) Number of periods to evaluate for the errors alarms. __*Default*__: 3 * **errorsPerMinuteThreshold** (number) Number of allowed errors per minute. __*Default*__: 0 + * **throttlesEvaluationPeriods** (number) Number of periods to evaluate for the throttles alarms. __*Default*__: 3 * **throttlesPerMinuteThreshold** (number) Number of allowed throttles per minute. __*Default*__: 0 * **fn** ([aws_lambda.Function](#aws-cdk-lib-aws-lambda-function)) *No description* * **title** (string) *No description* @@ -382,8 +385,11 @@ watchLambdaFunction(title: string, fn: Function, options?: WatchLambdaFunctionOp * **title** (string) *No description* * **fn** ([aws_lambda.Function](#aws-cdk-lib-aws-lambda-function)) *No description* * **options** ([WatchLambdaFunctionOptions](#cdk-watchful-watchlambdafunctionoptions)) *No description* + * **durationEvaluationPeriods** (number) Number of periods to evaluate for the duration alarms. __*Default*__: 3 * **durationThresholdPercent** (number) Threshold for the duration alarm as percentage of the function's timeout value. __*Default*__: 80 + * **errorsEvaluationPeriods** (number) Number of periods to evaluate for the errors alarms. __*Default*__: 3 * **errorsPerMinuteThreshold** (number) Number of allowed errors per minute. __*Default*__: 0 + * **throttlesEvaluationPeriods** (number) Number of periods to evaluate for the throttles alarms. __*Default*__: 3 * **throttlesPerMinuteThreshold** (number) Number of allowed throttles per minute. __*Default*__: 0 __Returns__: @@ -682,8 +688,11 @@ Name | Type | Description Name | Type | Description -----|------|------------- +**durationEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the duration alarms.
__*Default*__: 3 **durationThresholdPercent**?🔹 | number | Threshold for the duration alarm as percentage of the function's timeout value.
__*Default*__: 80 +**errorsEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the errors alarms.
__*Default*__: 3 **errorsPerMinuteThreshold**?🔹 | number | Number of allowed errors per minute.
__*Default*__: 0 +**throttlesEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the throttles alarms.
__*Default*__: 3 **throttlesPerMinuteThreshold**?🔹 | number | Number of allowed throttles per minute.
__*Default*__: 0 @@ -700,8 +709,11 @@ Name | Type | Description **fn**🔹 | [aws_lambda.Function](#aws-cdk-lib-aws-lambda-function) | **title**🔹 | string | **watchful**🔹 | [IWatchful](#cdk-watchful-iwatchful) | +**durationEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the duration alarms.
__*Default*__: 3 **durationThresholdPercent**?🔹 | number | Threshold for the duration alarm as percentage of the function's timeout value.
__*Default*__: 80 +**errorsEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the errors alarms.
__*Default*__: 3 **errorsPerMinuteThreshold**?🔹 | number | Number of allowed errors per minute.
__*Default*__: 0 +**throttlesEvaluationPeriods**?🔹 | number | Number of periods to evaluate for the throttles alarms.
__*Default*__: 3 **throttlesPerMinuteThreshold**?🔹 | number | Number of allowed throttles per minute.
__*Default*__: 0 diff --git a/src/lambda.ts b/src/lambda.ts index bc9b1372..e69419be 100644 --- a/src/lambda.ts +++ b/src/lambda.ts @@ -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 { @@ -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; @@ -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({ @@ -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 };