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(cloudwatch): Support region and account in math expressions #9034

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ export interface MetricExpressionConfig {
* How many seconds to aggregate over
*/
readonly period: number;

/**
* Region which this metric comes from.
*
* @default Deployment region.
*/
readonly region?: string;

/**
* Account which this metric comes from.
*
* @default Deployment account.
*/
readonly account?: string;
}

/**
Expand Down
28 changes: 27 additions & 1 deletion packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ export interface MathExpressionOptions {
* @default Duration.minutes(5)
*/
readonly period?: cdk.Duration;

/**
* Account which this metric comes from.
*
* @default - Deployment account.
*/
readonly account?: string;

/**
* Region which this metric comes from.
*
* @default - Deployment region.
*/
readonly region?: string;
}

/**
Expand Down Expand Up @@ -435,12 +449,20 @@ export class MathExpression implements IMetric {
*/
public readonly period: cdk.Duration;

/** Account which this metric comes from */
public readonly account?: string;

/** Region which this metric comes from. */
public readonly region?: string;

constructor(props: MathExpressionProps) {
this.period = props.period || cdk.Duration.minutes(5);
this.expression = props.expression;
this.usingMetrics = changeAllPeriods(props.usingMetrics, this.period);
this.label = props.label;
this.color = props.color;
this.account = props.account;
this.region = props.region;

const invalidVariableNames = Object.keys(props.usingMetrics).filter(x => !validVariableName(x));
if (invalidVariableNames.length > 0) {
Expand Down Expand Up @@ -471,6 +493,8 @@ export class MathExpression implements IMetric {
label: ifUndefined(props.label, this.label),
color: ifUndefined(props.color, this.color),
period: ifUndefined(props.period, this.period),
account: ifUndefined(props.account, this.account),
region: ifUndefined(props.region, this.region),
});
}

Expand All @@ -488,6 +512,8 @@ export class MathExpression implements IMetric {
period: this.period.toSeconds(),
expression: this.expression,
usingMetrics: this.usingMetrics,
region: this.region,
account: this.account,
},
renderingProperties: {
label: this.label,
Expand Down Expand Up @@ -709,4 +735,4 @@ interface IModifiableMetric {

function isModifiableMetric(m: any): m is IModifiableMetric {
return typeof m === 'object' && m !== null && !!m.with;
}
}
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function metricGraphJson(metric: IMetric, yAxis?: string, id?: string) {

withExpression(expr) {
options.expression = expr.expression;
if (expr.account) { options.accountId = accountIfDifferentFromStack(expr.account); }
if (expr.region) { options.region = regionIfDifferentFromStack(expr.region); }
},
});

Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/test.metric-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,34 @@ export = {
test.done();
},

'MathExpressions can be account and region specific'(test: Test) {
// GIVEN
const region = 'cn-north-1';
const accountA = '1234';
const accountB = '5678';
const regionalizedMetricA = new Metric({ namespace: 'Test', metricName: 'ACount', region, account: accountA });
const regionalizedMetricB = new Metric({ namespace: 'Test', metricName: 'BCount', statistic: 'Average', region, account: accountB });
const graph = new GraphWidget({
left: [
new MathExpression({
expression: 'a + b',
usingMetrics: { a: regionalizedMetricA, b: regionalizedMetricB },
account: accountA,
region,
}),
],
});

// THEN
graphMetricsAre(test, graph, [
[ { expression: 'a + b', label: 'a + b', region: 'cn-north-1', accountId: accountA } ],
[ 'Test', 'ACount', { visible: false, id: 'a', region: 'cn-north-1', accountId: accountA } ],
[ 'Test', 'BCount', { visible: false, id: 'b', region: 'cn-north-1', accountId: accountB } ],
]);

test.done();
},

'can nest MathExpressions in a graph'(test: Test) {
// GIVEN
const graph = new GraphWidget({
Expand Down