Skip to content

Commit

Permalink
feat: evaluationPeriodsを引数に追加
Browse files Browse the repository at this point in the history
  • Loading branch information
badmintoncryer committed Jun 13, 2023
1 parent 529d329 commit c48a4dc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
4 changes: 4 additions & 0 deletions API.md

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

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

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

export interface WatchLambdaFunctionProps extends WatchLambdaFunctionOptions {
Expand All @@ -40,7 +47,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 +68,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.evaluationPeriods,
);
const { throttlesMetric, throttlesAlarm } = this.createThrottlesMonitor(
props.throttlesPerMinuteThreshold,
props.evaluationPeriods,
);
const { durationMetric, durationAlarm } = this.createDurationMonitor(
timeoutSec,
props.durationThresholdPercent,
props.evaluationPeriods,
);
const invocationsMetric = this.metrics.metricInvocations(
this.fn.functionName,
);

this.watchful.addWidgets(
new cloudwatch.GraphWidget({
Expand Down Expand Up @@ -94,41 +112,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

0 comments on commit c48a4dc

Please sign in to comment.