Skip to content

Commit cb2bca9

Browse files
feat: Add region and account parameters as optional global defaults (#311)
This lets me create cross-account and cross-region monitoring. Currently only the current account is supported. My PR adds these parameters at the *global level* because I didn't want to go through every resource and add these extra parameters. This creates a limitation where an instance of `MonitoringFacade` would refer only to those specific region/accounts. This is good enough for me though, so please advise if it's not desired. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 6414368 commit cb2bca9

File tree

4 files changed

+234
-6
lines changed

4 files changed

+234
-6
lines changed

API.md

Lines changed: 53 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/common/metric/MetricFactory.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ export interface MetricFactoryDefaults {
3030
* @default - DefaultMetricPeriod
3131
*/
3232
readonly period?: Duration;
33+
34+
/**
35+
* Region where the metrics exist.
36+
*
37+
* @default The region configured by the construct holding the Monitoring construct
38+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
39+
*/
40+
readonly region?: string;
41+
/**
42+
* Account where the metrics exist.
43+
*
44+
* @default The account configured by the construct holding the Monitoring construct
45+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
46+
*/
47+
readonly account?: string;
3348
}
3449

3550
export interface MetricFactoryProps {
@@ -56,6 +71,8 @@ export class MetricFactory {
5671
* @param color metric color; if undefined, uses a CloudWatch provided color (preferred)
5772
* @param namespace specify a custom namespace; if undefined, uses the global default
5873
* @param period specify a custom period; if undefined, uses the global default
74+
* @param region specify a custom region; if undefined, uses the global default
75+
* @param account specify a custom account; if undefined, uses the global default
5976
*/
6077
createMetric(
6178
metricName: string,
@@ -64,7 +81,9 @@ export class MetricFactory {
6481
dimensionsMap?: DimensionsMap,
6582
color?: string,
6683
namespace?: string,
67-
period?: Duration
84+
period?: Duration,
85+
region?: string,
86+
account?: string
6887
): MetricWithAlarmSupport {
6988
return new Metric({
7089
statistic,
@@ -76,6 +95,8 @@ export class MetricFactory {
7695
: undefined,
7796
namespace: this.getNamespaceWithFallback(namespace),
7897
period: period ?? this.globalDefaults.period ?? DefaultMetricPeriod,
98+
region: region ?? this.globalDefaults.region,
99+
account: account ?? this.globalDefaults.account,
79100
});
80101
}
81102

test/common/metric/MetricFactory.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Duration } from "aws-cdk-lib";
12
import { Metric } from "aws-cdk-lib/aws-cloudwatch";
23

34
import {
45
MetricFactory,
6+
MetricFactoryDefaults,
57
MetricStatistic,
68
RateComputationMethod,
79
} from "../../../lib";
@@ -19,6 +21,41 @@ test("createMetric without global namespace throws an error", () => {
1921
).toThrowError();
2022
});
2123

24+
test("snapshot test: global defaults", () => {
25+
function testMetric(props: MetricFactoryDefaults) {
26+
const metricFactory = new MetricFactory({
27+
globalDefaults: {
28+
namespace: "DummyNamespace",
29+
...props,
30+
},
31+
});
32+
const metric = metricFactory.createMetric(
33+
"DummyMetricName",
34+
MetricStatistic.P90
35+
);
36+
expect(metric).toMatchSnapshot();
37+
}
38+
39+
testMetric({
40+
namespace: "SomeNamespace",
41+
});
42+
testMetric({
43+
period: Duration.minutes(15),
44+
});
45+
testMetric({
46+
region: "us-west-2",
47+
});
48+
testMetric({
49+
account: "123456789",
50+
});
51+
testMetric({
52+
namespace: "SomeNamespace",
53+
period: Duration.minutes(15),
54+
region: "us-west-2",
55+
account: "123456789",
56+
});
57+
});
58+
2259
test("snapshot test: createMetric", () => {
2360
const metricFactory = new MetricFactory({
2461
globalDefaults: {
@@ -53,7 +90,10 @@ test("snapshot test: createMetric", () => {
5390
AnotherDummyDimension: undefined as unknown as string,
5491
},
5592
DummyColor,
56-
"DummyNamespaceOverride"
93+
"DummyNamespaceOverride",
94+
Duration.minutes(15),
95+
"us-west-2",
96+
"123456789"
5797
);
5898

5999
expect(metricWithUndefinedDimensions).toMatchSnapshot();

test/common/metric/__snapshots__/MetricFactory.test.ts.snap

Lines changed: 118 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)