Skip to content

Commit

Permalink
fix(cloudwatch): can use percentile override in Alarm (#4253)
Browse files Browse the repository at this point in the history
Correctly render percentile statistic to the `ExtendedStatistic` field, instead
of the `Statistic` field.

Fixes #3845.
  • Loading branch information
rix0rrr authored and mergify[bot] committed Sep 27, 2019
1 parent a9fef66 commit 859e4d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
27 changes: 25 additions & 2 deletions packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnAlarm } from './cloudwatch.generated';
import { HorizontalAnnotation } from './graph';
import { CreateAlarmOptions } from './metric';
import { IMetric } from './metric-types';
import { normalizeStatistic } from './util.statistic';
import { parseStatistic } from './util.statistic';

export interface IAlarm extends IResource {
/**
Expand Down Expand Up @@ -147,7 +147,8 @@ export class Alarm extends Resource implements IAlarm {
...dropUndef({
// Alarm overrides
period: props.period && props.period.toSeconds(),
statistic: props.statistic && normalizeStatistic(props.statistic),
statistic: renderIfSimpleStatistic(props.statistic),
extendedStatistic: renderIfExtendedStatistic(props.statistic),
})
});

Expand Down Expand Up @@ -248,3 +249,25 @@ function dropUndef<T extends object>(x: T): T {
}
return ret;
}

function renderIfSimpleStatistic(statistic?: string): string | undefined {
if (statistic === undefined) { return undefined; }

const parsed = parseStatistic(statistic);
if (parsed.type === 'simple') {
return parsed.statistic;
}
return undefined;
}

function renderIfExtendedStatistic(statistic?: string): string | undefined {
if (statistic === undefined) { return undefined; }

const parsed = parseStatistic(statistic);
if (parsed.type === 'percentile') {
// Already percentile. Avoid parsing because we might get into
// floating point rounding issues, return as-is but lowercase the p.
return statistic.toLowerCase();
}
return undefined;
}
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ export = {
test.done();
},

'can use percentile in Alarm'(test: Test) {
// GIVEN
const stack = new Stack();

// WHEN
new Alarm(stack, 'Alarm', {
metric: testMetric,
statistic: 'P99',
threshold: 1000,
evaluationPeriods: 3,
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: "GreaterThanOrEqualToThreshold",
EvaluationPeriods: 3,
MetricName: "Metric",
Namespace: "CDK/Test",
Period: 300,
ExtendedStatistic: 'p99',
Threshold: 1000,
}));

test.done();
},

'can set DatapointsToAlarm'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit 859e4d1

Please sign in to comment.