Skip to content

Commit

Permalink
feat: allow specifying regions/accounts at a more granular level
Browse files Browse the repository at this point in the history
  • Loading branch information
echeung-amzn committed Jun 25, 2024
1 parent 23caa55 commit 8935e88
Show file tree
Hide file tree
Showing 40 changed files with 3,029 additions and 130 deletions.
2,786 changes: 2,768 additions & 18 deletions API.md

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions lib/common/metric/BaseMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface BaseMetricFactoryProps {
/**
* 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 {
protected readonly account?: string;
protected readonly region?: string;

constructor(props: BaseMetricFactoryProps) {
this.account = props.account;
this.region = props.region;
}
}
18 changes: 2 additions & 16 deletions lib/common/metric/MetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AnomalyDetectionMathExpression } from "./AnomalyDetectionMathExpression
import { MetricStatistic } from "./MetricStatistic";
import { MetricWithAlarmSupport } from "./MetricWithAlarmSupport";
import { RateComputationMethod } from "./RateComputationMethod";
import { BaseMetricFactoryProps } from "./BaseMetricFactory";

/**
* The most common default metric period used at Amazon is currently 5 minutes.
Expand All @@ -19,7 +20,7 @@ export const DefaultMetricPeriod = Duration.minutes(5);
/**
* These are the globals used for each metric, unless there is some kind of override.
*/
export interface MetricFactoryDefaults {
export interface MetricFactoryDefaults extends BaseMetricFactoryProps {
/**
* Each metric exists in a namespace. AWS Services have their own namespace, but here you can specify your custom one.
*/
Expand All @@ -30,21 +31,6 @@ export interface MetricFactoryDefaults {
* @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
1 change: 1 addition & 0 deletions lib/common/metric/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./AnomalyDetectionMathExpression";
export * from "./BaseMetricFactory";
export * from "./MetricFactory";
export * from "./MetricStatistic";
export * from "./MetricWithAlarmSupport";
Expand Down
4 changes: 3 additions & 1 deletion lib/common/monitoring/Monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
UserProvidedNames,
} from "../../dashboard";
import { AlarmWithAnnotation } from "../alarm";
import { BaseMetricFactoryProps } from "../metric";

export interface IAlarmConsumer {
consume(alarms: AlarmWithAnnotation[]): void;
Expand All @@ -19,7 +20,8 @@ export interface IAlarmConsumer {
* It contains (mostly optional) properties to specify naming, placement, and so on.
*/
export interface BaseMonitoringProps
extends UserProvidedNames,
extends BaseMetricFactoryProps,
UserProvidedNames,
MonitoringDashboardsOverrideProps {
/**
* Calls provided function to process all alarms created.
Expand Down
14 changes: 11 additions & 3 deletions lib/monitoring/aws-acm/CertificateManagerMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import {
BaseMetricFactory,
BaseMetricFactoryProps,
MetricFactory,
MetricStatistic,
MetricWithAlarmSupport,
} from "../../common";

const Namespace = "AWS/CertificateManager";

export interface CertificateManagerMetricFactoryProps {
export interface CertificateManagerMetricFactoryProps extends BaseMetricFactoryProps {
readonly certificate: ICertificate;
}

export class CertificateManagerMetricFactory {
export class CertificateManagerMetricFactory extends BaseMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly dimensionsMap: DimensionsMap;
protected readonly region?: string;

constructor(
metricFactory: MetricFactory,
props: CertificateManagerMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.dimensionsMap = {
CertificateArn: props.certificate.certificateArn,
Expand All @@ -34,7 +39,10 @@ export class CertificateManagerMetricFactory {
"Days to expiry",
this.dimensionsMap,
undefined,
Namespace
Namespace,
undefined,
this.region,
this.account,
);
}
}
28 changes: 22 additions & 6 deletions lib/monitoring/aws-apigateway/ApiGatewayMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IRestApi } from "aws-cdk-lib/aws-apigateway";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import {
BaseMetricFactory,
BaseMetricFactoryProps,
getLatencyTypeLabel,
getLatencyTypeStatistic,
LatencyType,
Expand All @@ -12,7 +14,7 @@ import {

const ApiGatewayNamespace = "AWS/ApiGateway";

export interface ApiGatewayMetricFactoryProps {
export interface ApiGatewayMetricFactoryProps extends BaseMetricFactoryProps {
/**
* API to monitor
*/
Expand All @@ -39,7 +41,7 @@ export interface ApiGatewayMetricFactoryProps {
readonly rateComputationMethod?: RateComputationMethod;
}

export class ApiGatewayMetricFactory {
export class ApiGatewayMetricFactory extends BaseMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;
Expand All @@ -49,6 +51,8 @@ export class ApiGatewayMetricFactory {
metricFactory: MetricFactory,
props: ApiGatewayMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
Expand Down Expand Up @@ -91,7 +95,10 @@ export class ApiGatewayMetricFactory {
"Count",
this.dimensionsMap,
undefined,
ApiGatewayNamespace
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -102,7 +109,10 @@ export class ApiGatewayMetricFactory {
"4XX Error",
this.dimensionsMap,
undefined,
ApiGatewayNamespace
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand All @@ -123,7 +133,10 @@ export class ApiGatewayMetricFactory {
"5XX Fault",
this.dimensionsMap,
undefined,
ApiGatewayNamespace
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}

Expand Down Expand Up @@ -166,7 +179,10 @@ export class ApiGatewayMetricFactory {
label,
this.dimensionsMap,
undefined,
ApiGatewayNamespace
ApiGatewayNamespace,
undefined,
this.region,
this.account,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IHttpApi } from "aws-cdk-lib/aws-apigatewayv2";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import {
BaseMetricFactory,
BaseMetricFactoryProps,
getLatencyTypeLabel,
getLatencyTypeStatistic,
LatencyType,
Expand All @@ -12,7 +14,7 @@ import {

const ApiGatewayNamespace = "AWS/ApiGateway";

export interface ApiGatewayV2HttpApiMetricFactoryProps {
export interface ApiGatewayV2HttpApiMetricFactoryProps extends BaseMetricFactoryProps {
readonly api: IHttpApi;
/**
* @default - $default
Expand All @@ -36,7 +38,7 @@ export interface ApiGatewayV2HttpApiMetricFactoryProps {
readonly rateComputationMethod?: RateComputationMethod;
}

export class ApiGatewayV2HttpApiMetricFactory {
export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;
Expand All @@ -46,6 +48,8 @@ export class ApiGatewayV2HttpApiMetricFactory {
metricFactory: MetricFactory,
props: ApiGatewayV2HttpApiMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
Expand Down
8 changes: 6 additions & 2 deletions lib/monitoring/aws-appsync/AppSyncMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { IGraphqlApi } from "aws-cdk-lib/aws-appsync";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import {
BaseMetricFactory,
BaseMetricFactoryProps,
MetricFactory,
MetricStatistic,
RateComputationMethod,
} from "../../common";

const Namespace = "AWS/AppSync";

export interface AppSyncMetricFactoryProps {
export interface AppSyncMetricFactoryProps extends BaseMetricFactoryProps {
/**
* the GraphQL API to monitor
*/
Expand All @@ -26,13 +28,15 @@ export interface AppSyncMetricFactoryProps {
readonly rateComputationMethod?: RateComputationMethod;
}

export class AppSyncMetricFactory {
export class AppSyncMetricFactory extends BaseMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;
protected readonly dimensionsMap: DimensionsMap;

constructor(metricFactory: MetricFactory, props: AppSyncMetricFactoryProps) {
super(props);

this.metricFactory = metricFactory;
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { IDistribution } from "aws-cdk-lib/aws-cloudfront";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import {
BaseMetricFactory,
BaseMetricFactoryProps,
MetricFactory,
MetricStatistic,
RateComputationMethod,
Expand All @@ -11,7 +13,7 @@ const CloudFrontNamespace = "AWS/CloudFront";
const CloudFrontGlobalRegion = "Global";
const CloudFrontDefaultMetricRegion = "us-east-1";

export interface CloudFrontDistributionMetricFactoryProps {
export interface CloudFrontDistributionMetricFactoryProps extends BaseMetricFactoryProps {
readonly distribution: IDistribution;

/**
Expand Down Expand Up @@ -39,7 +41,7 @@ export interface CloudFrontDistributionMetricFactoryProps {
* To get the CloudFront metrics from the CloudWatch API, you must use the US East (N. Virginia) Region (us-east-1).
* https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/programming-cloudwatch-metrics.html
*/
export class CloudFrontDistributionMetricFactory {
export class CloudFrontDistributionMetricFactory extends BaseMetricFactory {
private readonly metricFactory: MetricFactory;
private readonly fillTpsWithZeroes: boolean;
private readonly rateComputationMethod: RateComputationMethod;
Expand All @@ -49,6 +51,8 @@ export class CloudFrontDistributionMetricFactory {
metricFactory: MetricFactory,
props: CloudFrontDistributionMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
Expand Down
8 changes: 5 additions & 3 deletions lib/monitoring/aws-cloudwatch/CloudWatchLogsMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";

import { MetricFactory, MetricStatistic } from "../../common";
import { BaseMetricFactory, BaseMetricFactoryProps, MetricFactory, MetricStatistic } from "../../common";

const CloudWatchLogsNamespace = "AWS/Logs";

export interface CloudWatchLogsMetricFactoryProps {
export interface CloudWatchLogsMetricFactoryProps extends BaseMetricFactoryProps {
/**
* Name of the log group to monitor.
*/
readonly logGroupName: string;
}

export class CloudWatchLogsMetricFactory {
export class CloudWatchLogsMetricFactory extends BaseMetricFactory {
private readonly metricFactory: MetricFactory;
private readonly dimensionsMap: DimensionsMap;

constructor(
metricFactory: MetricFactory,
props: CloudWatchLogsMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.dimensionsMap = {
LogGroupName: props.logGroupName,
Expand Down
8 changes: 5 additions & 3 deletions lib/monitoring/aws-codebuild/CodeBuildProjectMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";
import { IProject } from "aws-cdk-lib/aws-codebuild";

import { MetricFactory, MetricStatistic } from "../../common";
import { BaseMetricFactory, BaseMetricFactoryProps, MetricFactory, MetricStatistic } from "../../common";

export interface CodeBuildProjectMetricFactoryProps {
export interface CodeBuildProjectMetricFactoryProps extends BaseMetricFactoryProps {
readonly project: IProject;
}

export class CodeBuildProjectMetricFactory {
export class CodeBuildProjectMetricFactory extends BaseMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly dimensionsMap: DimensionsMap;
protected readonly project: IProject;
Expand All @@ -16,6 +16,8 @@ export class CodeBuildProjectMetricFactory {
metricFactory: MetricFactory,
props: CodeBuildProjectMetricFactoryProps
) {
super(props);

this.metricFactory = metricFactory;
this.project = props.project;
this.dimensionsMap = {
Expand Down
Loading

0 comments on commit 8935e88

Please sign in to comment.