Skip to content

Commit

Permalink
feat(cloudwatch): allow arbitrary statistics in Metric and Alarm (aws…
Browse files Browse the repository at this point in the history
…#15387)

This change maintains the existing logic which parses statistic strings provided by the user for Simple statistics or Percentile statistics, but it also allows for any string to be passed.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
madeline-k authored and hollanddd committed Aug 26, 2021
1 parent 49298f2 commit 04f83ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/@aws-cdk/aws-cloudwatch/lib/private/statistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ export interface PercentileStatistic {
type: 'percentile';
percentile: number;
}

export interface GenericStatistic {
type: 'generic';
statistic: string;
}

/**
* Parse a statistic, returning the type of metric that was used (simple or percentile)
*/
export function parseStatistic(stat: string): SimpleStatistic | PercentileStatistic {
export function parseStatistic(stat: string): SimpleStatistic | PercentileStatistic | GenericStatistic {
const lowerStat = stat.toLowerCase();

// Simple statistics
Expand Down Expand Up @@ -45,7 +51,10 @@ export function parseStatistic(stat: string): SimpleStatistic | PercentileStatis
};
}

throw new Error(`Not a valid statistic: '${stat}', must be one of Average | Minimum | Maximum | SampleCount | Sum | pNN.NN`);
return {
type: 'generic',
statistic: lowerStat,
};
}

export function normalizeStatistic(stat: string): string {
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,14 @@ export = {

test.done();
},

'metric accepts a variety of statistics'(test: Test) {
new Metric({
namespace: 'Test',
metricName: 'Metric',
statistic: 'myCustomStatistic',
});

test.done();
},
};

0 comments on commit 04f83ef

Please sign in to comment.