Skip to content

Commit

Permalink
feat: add BaseMetricFactory (#534)
Browse files Browse the repository at this point in the history
So we can later use it for #533.

---

_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 Jun 25, 2024
1 parent 061d16f commit f8778a1
Show file tree
Hide file tree
Showing 40 changed files with 368 additions and 159 deletions.
44 changes: 44 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/common/metric/BaseMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MetricFactory } from "./MetricFactory";

export interface BaseMetricFactoryProps {
// TODO: this will eventually include other common things like account/region
}

export abstract class BaseMetricFactory<
PropsType extends BaseMetricFactoryProps
> {
protected readonly metricFactory: MetricFactory;

constructor(metricFactory: MetricFactory, _props: PropsType) {
this.metricFactory = metricFactory;
}
}
3 changes: 2 additions & 1 deletion lib/common/metric/MetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "aws-cdk-lib/aws-cloudwatch";

import { AnomalyDetectionMathExpression } from "./AnomalyDetectionMathExpression";
import { BaseMetricFactoryProps } from "./BaseMetricFactory";
import { MetricStatistic } from "./MetricStatistic";
import { MetricWithAlarmSupport } from "./MetricWithAlarmSupport";
import { RateComputationMethod } from "./RateComputationMethod";
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 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
11 changes: 7 additions & 4 deletions lib/monitoring/aws-acm/CertificateManagerMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ 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 {
protected readonly metricFactory: MetricFactory;
export class CertificateManagerMetricFactory extends BaseMetricFactory<CertificateManagerMetricFactoryProps> {
protected readonly dimensionsMap: DimensionsMap;

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

this.dimensionsMap = {
CertificateArn: props.certificate.certificateArn,
};
Expand Down
10 changes: 6 additions & 4 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,8 +41,7 @@ export interface ApiGatewayMetricFactoryProps {
readonly rateComputationMethod?: RateComputationMethod;
}

export class ApiGatewayMetricFactory {
protected readonly metricFactory: MetricFactory;
export class ApiGatewayMetricFactory extends BaseMetricFactory<ApiGatewayMetricFactoryProps> {
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;
protected readonly dimensionsMap: DimensionsMap;
Expand All @@ -49,7 +50,8 @@ export class ApiGatewayMetricFactory {
metricFactory: MetricFactory,
props: ApiGatewayMetricFactoryProps
) {
this.metricFactory = metricFactory;
super(metricFactory, props);

this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
Expand Down
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,8 @@ import {

const ApiGatewayNamespace = "AWS/ApiGateway";

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

export class ApiGatewayV2HttpApiMetricFactory {
protected readonly metricFactory: MetricFactory;
export class ApiGatewayV2HttpApiMetricFactory extends BaseMetricFactory<ApiGatewayV2HttpApiMetricFactoryProps> {
protected readonly fillTpsWithZeroes: boolean;
protected readonly rateComputationMethod: RateComputationMethod;
protected readonly dimensionsMap: DimensionsMap;
Expand All @@ -46,7 +48,8 @@ export class ApiGatewayV2HttpApiMetricFactory {
metricFactory: MetricFactory,
props: ApiGatewayV2HttpApiMetricFactoryProps
) {
this.metricFactory = metricFactory;
super(metricFactory, props);

this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
Expand Down
10 changes: 6 additions & 4 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,14 +28,14 @@ export interface AppSyncMetricFactoryProps {
readonly rateComputationMethod?: RateComputationMethod;
}

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

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

this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
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,8 @@ 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,8 +42,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 {
private readonly metricFactory: MetricFactory;
export class CloudFrontDistributionMetricFactory extends BaseMetricFactory<CloudFrontDistributionMetricFactoryProps> {
private readonly fillTpsWithZeroes: boolean;
private readonly rateComputationMethod: RateComputationMethod;
private readonly dimensionsMap: DimensionsMap;
Expand All @@ -49,7 +51,8 @@ export class CloudFrontDistributionMetricFactory {
metricFactory: MetricFactory,
props: CloudFrontDistributionMetricFactoryProps
) {
this.metricFactory = metricFactory;
super(metricFactory, props);

this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
this.rateComputationMethod =
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
Expand Down
16 changes: 11 additions & 5 deletions lib/monitoring/aws-cloudwatch/CloudWatchLogsMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
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 {
private readonly metricFactory: MetricFactory;
export class CloudWatchLogsMetricFactory extends BaseMetricFactory<CloudWatchLogsMetricFactoryProps> {
private readonly dimensionsMap: DimensionsMap;

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

this.dimensionsMap = {
LogGroupName: props.logGroupName,
};
Expand Down
16 changes: 11 additions & 5 deletions lib/monitoring/aws-codebuild/CodeBuildProjectMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
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 {
protected readonly metricFactory: MetricFactory;
export class CodeBuildProjectMetricFactory extends BaseMetricFactory<CodeBuildProjectMetricFactoryProps> {
protected readonly dimensionsMap: DimensionsMap;
protected readonly project: IProject;

constructor(
metricFactory: MetricFactory,
props: CodeBuildProjectMetricFactoryProps
) {
this.metricFactory = metricFactory;
super(metricFactory, props);

this.project = props.project;
this.dimensionsMap = {
ProjectName: props.project.projectName,
Expand Down
Loading

0 comments on commit f8778a1

Please sign in to comment.