From 839d06f97825eaa8427e422a7a68bb4b8f48b1f0 Mon Sep 17 00:00:00 2001
From: Eugene Cheung <81188333+echeung-amzn@users.noreply.github.com>
Date: Thu, 1 Aug 2024 10:02:10 -0400
Subject: [PATCH] feat(custom): handle cross-account/cross-region search
expressions for dashboards (#551)
Building on top of #533, but `monitorCustom` has its own API for
simplifying search expression queries so we need to explicitly handle
it.
Regular `new Metric(...)` calls just pass in the `account`/`metric` via
that API.
---
_By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license_
---
API.md | 32 +++++++++++++++++++
lib/monitoring/custom/CustomMonitoring.ts | 18 +++++++++++
.../custom/CustomMonitoring.test.ts | 16 +++++++++-
.../CustomMonitoring.test.ts.snap | 4 +--
4 files changed, 67 insertions(+), 3 deletions(-)
diff --git a/API.md b/API.md
index 345b02ad..f71bd50c 100644
--- a/API.md
+++ b/API.md
@@ -13586,10 +13586,12 @@ const customMetricSearch: CustomMetricSearch = { ... }
| dimensionsMap
| {[ key: string ]: string}
| search dimensions (can be empty). |
| searchQuery
| string
| search query (can be empty). |
| statistic
| MetricStatistic
| metric statistic. |
+| account
| string
| Account which this metric comes from. |
| label
| string
| custom label for the metrics. |
| namespace
| string
| metric namespace. |
| period
| aws-cdk-lib.Duration
| metric period. |
| position
| AxisPosition
| axis (right or left) on which to graph metric default: AxisPosition.LEFT. |
+| region
| string
| Region which this metric comes from. |
---
@@ -13629,6 +13631,21 @@ metric statistic.
---
+##### `account`Optional
+
+```typescript
+public readonly account: string;
+```
+
+- *Type:* string
+- *Default:* Deployment account.
+
+Account which this metric comes from.
+
+Note that alarms cannot be created for cross-account metrics.
+
+---
+
##### `label`Optional
```typescript
@@ -13680,6 +13697,21 @@ axis (right or left) on which to graph metric default: AxisPosition.LEFT.
---
+##### `region`Optional
+
+```typescript
+public readonly region: string;
+```
+
+- *Type:* string
+- *Default:* Deployment region.
+
+Region which this metric comes from.
+
+Note that alarms cannot be created for cross-region metrics.
+
+---
+
### CustomMetricWithAlarm
Custom metric with an alarm defined.
diff --git a/lib/monitoring/custom/CustomMonitoring.ts b/lib/monitoring/custom/CustomMonitoring.ts
index f736ec25..74aa7839 100644
--- a/lib/monitoring/custom/CustomMonitoring.ts
+++ b/lib/monitoring/custom/CustomMonitoring.ts
@@ -127,6 +127,22 @@ export interface CustomMetricSearch {
* default: AxisPosition.LEFT
*/
readonly position?: AxisPosition;
+
+ /**
+ * Account which this metric comes from.
+ * Note that alarms cannot be created for cross-account metrics.
+ *
+ * @default - Deployment account.
+ */
+ readonly account?: string;
+
+ /**
+ * Region which this metric comes from.
+ * Note that alarms cannot be created for cross-region metrics.
+ *
+ * @default - Deployment region.
+ */
+ readonly region?: string;
}
/**
@@ -482,6 +498,8 @@ export class CustomMonitoring extends Monitoring {
metric.namespace,
metric.label,
metric.period,
+ metric.region,
+ metric.account,
);
} else {
// general metric
diff --git a/test/monitoring/custom/CustomMonitoring.test.ts b/test/monitoring/custom/CustomMonitoring.test.ts
index a6138497..8068b275 100644
--- a/test/monitoring/custom/CustomMonitoring.test.ts
+++ b/test/monitoring/custom/CustomMonitoring.test.ts
@@ -44,7 +44,13 @@ test("snapshot test", () => {
// regular metric
new Metric({ metricName: "DummyMetric1", namespace, dimensionsMap }),
// metric with alarm
- new Metric({ metricName: "DummyMetric2", namespace, dimensionsMap }),
+ new Metric({
+ metricName: "DummyMetric2",
+ namespace,
+ dimensionsMap,
+ account: "01234567890",
+ region: "us-west-2",
+ }),
{
metric: new Metric({
metricName: "DummyMetric3",
@@ -71,6 +77,14 @@ test("snapshot test", () => {
dimensionsMap,
statistic: MetricStatistic.SUM,
},
+ {
+ searchQuery: "DummyMetric5-",
+ namespace,
+ dimensionsMap,
+ statistic: MetricStatistic.SUM,
+ account: "01234567890",
+ region: "us-west-2",
+ },
],
},
{
diff --git a/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap b/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
index efa55fc6..ca5f9f20 100644
--- a/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
+++ b/test/monitoring/custom/__snapshots__/CustomMonitoring.test.ts.snap
@@ -652,7 +652,7 @@ Object {
Object {
"Ref": "AWS::Region",
},
- "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric1\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric2\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric3\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric4-', 'Sum', 300)\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyMetric3 < 90 for 3 datapoints within 15 minutes\\",\\"value\\":90,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyMetric3 < 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{}}},{\\"type\\":\\"metric\\",\\"width\\":2,\\"height\\":100,\\"x\\":6,\\"y\\":3,\\"properties\\":{\\"view\\":\\"bar\\",\\"title\\":\\"DummyGroup2\\",\\"region\\":\\"",
+ "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric1\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric2\\",\\"CustomDimension\\",\\"CustomDimensionValue\\",{\\"accountId\\":\\"01234567890\\",\\"region\\":\\"us-west-2\\"}],[\\"DummyCustomNamespace\\",\\"DummyMetric3\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric4-', 'Sum', 300)\\"}],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric5-', 'Sum', 300)\\",\\"accountId\\":\\"01234567890\\",\\"region\\":\\"us-west-2\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyMetric3 < 90 for 3 datapoints within 15 minutes\\",\\"value\\":90,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyMetric3 < 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{}}},{\\"type\\":\\"metric\\",\\"width\\":2,\\"height\\":100,\\"x\\":6,\\"y\\":3,\\"properties\\":{\\"view\\":\\"bar\\",\\"title\\":\\"DummyGroup2\\",\\"region\\":\\"",
Object {
"Ref": "AWS::Region",
},
@@ -907,7 +907,7 @@ Min number of samples to alarm: 5",
Object {
"Ref": "AWS::Region",
},
- "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric1\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric2\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric3\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric4-', 'Sum', 300)\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyMetric3 < 90 for 3 datapoints within 15 minutes\\",\\"value\\":90,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyMetric3 < 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{}}}]}",
+ "\\",\\"metrics\\":[[\\"DummyCustomNamespace\\",\\"DummyMetric1\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[\\"DummyCustomNamespace\\",\\"DummyMetric2\\",\\"CustomDimension\\",\\"CustomDimensionValue\\",{\\"accountId\\":\\"01234567890\\",\\"region\\":\\"us-west-2\\"}],[\\"DummyCustomNamespace\\",\\"DummyMetric3\\",\\"CustomDimension\\",\\"CustomDimensionValue\\"],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric4-', 'Sum', 300)\\"}],[{\\"label\\":\\" \\",\\"expression\\":\\"SEARCH('{DummyCustomNamespace,CustomDimension} CustomDimension=\\\\\\"CustomDimensionValue\\\\\\" DummyMetric5-', 'Sum', 300)\\",\\"accountId\\":\\"01234567890\\",\\"region\\":\\"us-west-2\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"DummyMetric3 < 90 for 3 datapoints within 15 minutes\\",\\"value\\":90,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"DummyMetric3 < 50 for 3 datapoints within 15 minutes\\",\\"value\\":50,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{}}}]}",
],
],
},