Skip to content

Commit

Permalink
feat: allow specifying regions/accounts at a more granular level (#533)
Browse files Browse the repository at this point in the history
This allows users to target different regions/account for specific
things within a facade rather than relying on 1 facade per
region/account with the facade's global settings, so something like:

```ts
this.facade
  .monitorDynamoTable({
    ...,
  })
  .monitorDynamoTable({
    ...,
    region: <some_other_region>,
    account: <some_other_account>,
  })
```

---

_By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license_
  • Loading branch information
echeung-amzn authored Jul 3, 2024
1 parent 8ee4010 commit 4801636
Show file tree
Hide file tree
Showing 46 changed files with 4,703 additions and 92 deletions.
3,900 changes: 3,868 additions & 32 deletions API.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,48 @@ Now, this widget will be added to both dashboards and will show different conten
* Dashboard Name: "ExampleDashboards-Infrastructure"
* Content: "This shows metrics for the AWS Infrastructure supporting your hosted service"

### Cross-account cross-Region Dashboards

Facades can be configured for different regions/accounts as a whole:

```ts
new MonitoringFacade(stack, "Monitoring", {
metricFactoryDefaults: {
// Different region/account than what you're deploying to
region: "us-west-2",
account: "01234567890",
}
});
```

Or at a more granular level:

```ts
monitoring
.monitorDynamoTable({
// Table from the same account/region
table: Table.fromTableName(stack, "ImportedTable", "MyTableName"),
})
.monitorDynamoTable({
// Table from another account/region
table: Table.fromTableArn(
stack,
"XaXrImportedTable",
"arn:aws:dynamodb:us-west-2:01234567890:table/my-other-table",
),
region: "us-west-2",
account: "01234567890",
});
```

The order of precedence of the region/account values is:

1. The individual metric factory's props (e.g. via the `monitorDynamoTable` props).
1. The facade's `metricFactoryDefaults` props.
1. The region/account that the stack is deployed to.

Note that certain metrics are based on [math expressions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html) and cannot be alarmed upon in a cross-account cross-Region context, and you will see an error at synthesis time.

### Monitoring scopes

You can monitor complete CDK construct scopes using an aspect. It will automatically discover all monitorable resources within the scope recursively and add them to your dashboard.
Expand Down
22 changes: 20 additions & 2 deletions lib/common/metric/BaseMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { MetricFactory } from "./MetricFactory";

export interface BaseMetricFactoryProps {
// TODO: this will eventually include other common things like account/region
/**
* Region where the metrics exist.
*
* @default The region configured by the construct holding the Monitoring construct
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
*/
readonly region?: string;

/**
* Account where the metrics exist.
*
* @default The account configured by the construct holding the Monitoring construct
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
*/
readonly account?: string;
}

export abstract class BaseMetricFactory<
PropsType extends BaseMetricFactoryProps,
> {
protected readonly metricFactory: MetricFactory;
protected readonly account?: string;
protected readonly region?: string;

constructor(metricFactory: MetricFactory, _props: PropsType) {
constructor(metricFactory: MetricFactory, props: PropsType) {
this.metricFactory = metricFactory;
this.account = props.account;
this.region = props.region;
}
}
15 changes: 0 additions & 15 deletions lib/common/metric/MetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ export interface MetricFactoryDefaults extends BaseMetricFactoryProps {
* @default - DefaultMetricPeriod
*/
readonly period?: Duration;

/**
* Region where the metrics exist.
*
* @default The region configured by the construct holding the Monitoring construct
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
*/
readonly region?: string;
/**
* Account where the metrics exist.
*
* @default The account configured by the construct holding the Monitoring construct
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Cross-Account-Cross-Region.html
*/
readonly account?: string;
}

export interface MetricFactoryProps {
Expand Down
3 changes: 3 additions & 0 deletions lib/monitoring/aws-acm/CertificateManagerMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class CertificateManagerMetricFactory extends BaseMetricFactory<Certifica
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}
}
12 changes: 12 additions & 0 deletions lib/monitoring/aws-apigateway/ApiGatewayMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -105,6 +108,9 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -126,6 +132,9 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand Down Expand Up @@ -169,6 +178,9 @@ export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricF
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -122,6 +125,9 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand Down Expand Up @@ -186,6 +192,9 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -198,6 +207,9 @@ export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatew
this.dimensionsMap,
undefined,
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}
}
18 changes: 18 additions & 0 deletions lib/monitoring/aws-appsync/AppSyncMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -86,6 +89,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -97,6 +103,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -108,6 +117,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -119,6 +131,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -140,6 +155,9 @@ export class AppSyncMetricFactory extends BaseMetricFactory<AppSyncMetricFactory
this.dimensionsMap,
undefined,
Namespace,
undefined,
this.region,
this.account,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand Down Expand Up @@ -108,6 +109,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand All @@ -121,6 +123,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand All @@ -139,6 +142,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand All @@ -152,6 +156,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand All @@ -165,6 +170,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}

Expand All @@ -178,6 +184,7 @@ export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<Cloud
CloudFrontNamespace,
undefined,
CloudFrontDefaultMetricRegion,
this.account,
);
}
}
3 changes: 3 additions & 0 deletions lib/monitoring/aws-cloudwatch/CloudWatchLogsMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class CloudWatchLogsMetricFactory extends BaseMetricFactory<CloudWatchLog
this.dimensionsMap,
undefined,
CloudWatchLogsNamespace,
undefined,
this.region,
this.account,
);
}
}
27 changes: 24 additions & 3 deletions lib/monitoring/aws-codebuild/CodeBuildProjectMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,30 @@ export class CodeBuildProjectMetricFactory extends BaseMetricFactory<CodeBuildPr
}

metricBuildCount() {
return this.metricFactory.adaptMetric(this.project.metricBuilds());
return this.metricFactory.adaptMetric(
this.project.metricBuilds({
region: this.region,
account: this.account,
}),
);
}

metricSucceededBuildCount() {
return this.metricFactory.adaptMetric(this.project.metricSucceededBuilds());
return this.metricFactory.adaptMetric(
this.project.metricSucceededBuilds({
region: this.region,
account: this.account,
}),
);
}

metricFailedBuildCount() {
return this.metricFactory.adaptMetric(this.project.metricFailedBuilds());
return this.metricFactory.adaptMetric(
this.project.metricFailedBuilds({
region: this.region,
account: this.account,
}),
);
}

metricFailedBuildRate() {
Expand All @@ -52,6 +67,8 @@ export class CodeBuildProjectMetricFactory extends BaseMetricFactory<CodeBuildPr
this.project.metricDuration({
label: "P99",
statistic: MetricStatistic.P99,
region: this.region,
account: this.account,
}),
);
}
Expand All @@ -61,6 +78,8 @@ export class CodeBuildProjectMetricFactory extends BaseMetricFactory<CodeBuildPr
this.project.metricDuration({
label: "P90",
statistic: MetricStatistic.P90,
region: this.region,
account: this.account,
}),
);
}
Expand All @@ -70,6 +89,8 @@ export class CodeBuildProjectMetricFactory extends BaseMetricFactory<CodeBuildPr
this.project.metricDuration({
label: "P50",
statistic: MetricStatistic.P50,
region: this.region,
account: this.account,
}),
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/monitoring/aws-docdb/DocumentDbMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export class DocumentDbMetricFactory extends BaseMetricFactory<DocumentDbMetricF
this.dimensionsMap,
undefined,
DocumentDbNamespace,
undefined,
this.region,
this.account,
);
}
}
Loading

0 comments on commit 4801636

Please sign in to comment.