Skip to content

Commit 8ee4010

Browse files
author
Eugene Cheung
authored
feat!: update more MetricFactory classes to extend base (#541)
This is a continuation of #534, but it includes breaking changes to some existing metric factories' constructor API contracts. Also includes some minor private method renaming for consistency and a basic facade test, pulled out from #533. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent c17e2b5 commit 8ee4010

File tree

13 files changed

+263
-47
lines changed

13 files changed

+263
-47
lines changed

.projen/tasks.json

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

.projenrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
1818
keywords: ["cloudwatch", "monitoring"],
1919

2020
defaultReleaseBranch: "main",
21-
majorVersion: 7,
21+
majorVersion: 8,
2222
stability: "experimental",
2323

2424
cdkVersion: CDK_VERSION,

API.md

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

lib/monitoring/aws-ec2/EC2MetricFactory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class EC2MetricFactory extends BaseMetricFactory<EC2MetricFactoryProps> {
157157
* CloudWatch when the instance is not allocated a full processor core.
158158
*/
159159
metricAverageCpuUtilisationPercent() {
160-
return this.createMetrics("CPUUtilization", MetricStatistic.AVERAGE);
160+
return this.metric("CPUUtilization", MetricStatistic.AVERAGE);
161161
}
162162

163163
/**
@@ -197,15 +197,15 @@ export class EC2MetricFactory extends BaseMetricFactory<EC2MetricFactoryProps> {
197197
* This metric identifies the volume of incoming network traffic to a single instance.
198198
*/
199199
metricAverageNetworkInRateBytes() {
200-
return this.createMetrics("NetworkIn", MetricStatistic.AVERAGE);
200+
return this.metric("NetworkIn", MetricStatistic.AVERAGE);
201201
}
202202

203203
/**
204204
* The number of bytes sent out on all network interfaces by the instance.
205205
* This metric identifies the volume of outgoing network traffic from a single instance.
206206
*/
207207
metricAverageNetworkOutRateBytes() {
208-
return this.createMetrics("NetworkOut", MetricStatistic.AVERAGE);
208+
return this.metric("NetworkOut", MetricStatistic.AVERAGE);
209209
}
210210

211211
private createDiskMetrics(metricName: string, statistic: MetricStatistic) {
@@ -235,7 +235,7 @@ export class EC2MetricFactory extends BaseMetricFactory<EC2MetricFactoryProps> {
235235
});
236236
}
237237

238-
private createMetrics(metricName: string, statistic: MetricStatistic) {
238+
private metric(metricName: string, statistic: MetricStatistic) {
239239
return this.strategy.createMetrics(
240240
this.metricFactory,
241241
metricName,

lib/monitoring/aws-kinesisanalytics/KinesisDataAnalyticsMetricFactory.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,92 +39,92 @@ export class KinesisDataAnalyticsMetricFactory extends BaseMetricFactory<Kinesis
3939
}
4040

4141
metricKPUsCount() {
42-
return this.generateMetric({
42+
return this.metric({
4343
name: "KPUs",
4444
description: "Kinesis Processing Units",
4545
});
4646
}
4747

4848
metricDowntimeMs() {
49-
return this.generateMetric({
49+
return this.metric({
5050
name: "downtime",
5151
description: "Downtime",
5252
});
5353
}
5454

5555
metricUptimeMs() {
56-
return this.generateMetric({
56+
return this.metric({
5757
name: "uptime",
5858
description: "Uptime",
5959
});
6060
}
6161

6262
metricFullRestartsCount() {
63-
return this.generateMetric({
63+
return this.metric({
6464
name: "fullRestarts",
6565
description: "Restarts",
6666
});
6767
}
6868

6969
metricNumberOfFailedCheckpointsCount() {
70-
return this.generateMetric({
70+
return this.metric({
7171
name: "numberOfFailedCheckpoints",
7272
description: "Failed Checkpoints",
7373
metricStatistic: MetricStatistic.SUM,
7474
});
7575
}
7676

7777
metricLastCheckpointDurationMs() {
78-
return this.generateMetric({
78+
return this.metric({
7979
name: "lastCheckpointDuration",
8080
description: "Last Checkpoint Duration",
8181
});
8282
}
8383

8484
metricLastCheckpointSizeBytes() {
85-
return this.generateMetric({
85+
return this.metric({
8686
name: "lastCheckpointSize",
8787
description: "Last Checkpoint Size",
8888
metricStatistic: MetricStatistic.SUM,
8989
});
9090
}
9191

9292
metricCpuUtilizationPercent() {
93-
return this.generateMetric({
93+
return this.metric({
9494
name: "cpuUtilization",
9595
description: "CPU Utilization",
9696
});
9797
}
9898

9999
metricHeapMemoryUtilizationPercent() {
100-
return this.generateMetric({
100+
return this.metric({
101101
name: "heapMemoryUtilization",
102102
description: "Heap Memory Utilization",
103103
});
104104
}
105105

106106
metricOldGenerationGCTimeMs() {
107-
return this.generateMetric({
107+
return this.metric({
108108
name: "oldGenerationGCTime",
109109
description: "GC Time",
110110
});
111111
}
112112

113113
metricOldGenerationGCCount() {
114-
return this.generateMetric({
114+
return this.metric({
115115
name: "oldGenerationGCCount",
116116
metricStatistic: MetricStatistic.N,
117117
description: "GC Count",
118118
});
119119
}
120120

121-
private generateMetric(metricsSpec: MetricsSpec) {
121+
private metric(metricsSpec: MetricsSpec) {
122122
return this.metricFactory.createMetric(
123123
metricsSpec.name,
124-
metricsSpec.metricStatistic || MetricStatistic.AVERAGE,
124+
metricsSpec.metricStatistic ?? MetricStatistic.AVERAGE,
125125
metricsSpec.description,
126126
this.dimensionsMap,
127-
undefined, // the hex color code of the metric on a graph
127+
undefined,
128128
"AWS/KinesisAnalytics",
129129
);
130130
}

lib/monitoring/aws-lambda/LambdaFunctionEnhancedMetricFactory.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
import { IFunction } from "aws-cdk-lib/aws-lambda";
22

3-
import { MetricFactory, MetricStatistic } from "../../common";
3+
import {
4+
BaseMetricFactory,
5+
BaseMetricFactoryProps,
6+
MetricFactory,
7+
MetricStatistic,
8+
} from "../../common";
49

510
const LambdaInsightsNamespace = "LambdaInsights";
611

7-
export class LambdaFunctionEnhancedMetricFactory {
8-
protected readonly metricFactory: MetricFactory;
12+
export interface LambdaFunctionEnhancedMetricFactoryProps
13+
extends BaseMetricFactoryProps {
14+
readonly lambdaFunction: IFunction;
15+
}
16+
17+
export class LambdaFunctionEnhancedMetricFactory extends BaseMetricFactory<LambdaFunctionEnhancedMetricFactoryProps> {
918
protected readonly lambdaFunction: IFunction;
1019

11-
constructor(metricFactory: MetricFactory, lambdaFunction: IFunction) {
12-
this.metricFactory = metricFactory;
13-
this.lambdaFunction = lambdaFunction;
20+
constructor(
21+
metricFactory: MetricFactory,
22+
props: LambdaFunctionEnhancedMetricFactoryProps,
23+
) {
24+
super(metricFactory, props);
25+
26+
this.lambdaFunction = props.lambdaFunction;
1427
}
1528

1629
enhancedMetricMaxCpuTotalTime() {

lib/monitoring/aws-lambda/LambdaFunctionMetricFactory.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ export class LambdaFunctionMetricFactory extends BaseMetricFactory<LambdaFunctio
166166
}
167167

168168
metricProvisionedConcurrencySpilloverRate() {
169-
const metric = this.metricProvisionedConcurrencySpilloverInvocations();
170169
return this.metricFactory.toRate(
171-
metric,
170+
this.metricProvisionedConcurrencySpilloverInvocations(),
172171
this.rateComputationMethod,
173172
false,
174173
"pcsi",

lib/monitoring/aws-lambda/LambdaFunctionMonitoring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class LambdaFunctionMonitoring extends Monitoring {
234234
if (props.lambdaInsightsEnabled) {
235235
this.enhancedMetricFactory = new LambdaFunctionEnhancedMetricFactory(
236236
scope.createMetricFactory(),
237-
props.lambdaFunction,
237+
props,
238238
);
239239
this.enhancedMonitoringMaxCpuTotalTimeMetric =
240240
this.enhancedMetricFactory.enhancedMetricMaxCpuTotalTime();

lib/monitoring/aws-secretsmanager/SecretsManagerMetricFactory.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Duration } from "aws-cdk-lib";
2-
import { MetricFactory, MetricStatistic } from "../../common";
2+
import {
3+
BaseMetricFactory,
4+
BaseMetricFactoryProps,
5+
MetricFactory,
6+
MetricStatistic,
7+
} from "../../common";
38

49
const CLASS = "None";
510
const DEFAULT_METRIC_PERIOD = Duration.hours(1);
@@ -9,11 +14,14 @@ const RESOURCE = "SecretCount";
914
const SERVICE = "Secrets Manager";
1015
const TYPE = "Resource";
1116

12-
export class SecretsManagerMetricFactory {
13-
protected readonly metricFactory: MetricFactory;
17+
export type SecretsManagerMetricFactoryProps = BaseMetricFactoryProps;
1418

15-
constructor(metricFactory: MetricFactory) {
16-
this.metricFactory = metricFactory;
19+
export class SecretsManagerMetricFactory extends BaseMetricFactory<SecretsManagerMetricFactoryProps> {
20+
constructor(
21+
metricFactory: MetricFactory,
22+
props: SecretsManagerMetricFactoryProps,
23+
) {
24+
super(metricFactory, props);
1725
}
1826

1927
metricSecretCount() {

lib/monitoring/aws-secretsmanager/SecretsManagerMonitoring.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class SecretsManagerMonitoring extends Monitoring {
6464

6565
const metricFactory = new SecretsManagerMetricFactory(
6666
scope.createMetricFactory(),
67+
props,
6768
);
6869
this.secretsCountMetric = metricFactory.metricSecretCount();
6970

0 commit comments

Comments
 (0)