Skip to content

Commit 430abf2

Browse files
authored
feat: extend metric factories with custom rate computation method (#121)
Related to #50 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 5316f56 commit 430abf2

File tree

9 files changed

+314
-40
lines changed

9 files changed

+314
-40
lines changed

API.md

Lines changed: 193 additions & 21 deletions
Large diffs are not rendered by default.

lib/monitoring/aws-apigateway/ApiGatewayMetricFactory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ export class ApiGatewayMetricFactory {
6161
};
6262
}
6363

64+
/**
65+
* @deprecated use metricInvocationRate
66+
*/
6467
metricTps() {
65-
// TODO: rename to metricInvocationRate and use rateComputationMethod
6668
return this.metricFactory.toRate(
6769
this.metricInvocationCount(),
6870
RateComputationMethod.PER_SECOND,
@@ -72,6 +74,16 @@ export class ApiGatewayMetricFactory {
7274
);
7375
}
7476

77+
metricInvocationRate() {
78+
return this.metricFactory.toRate(
79+
this.metricInvocationCount(),
80+
this.rateComputationMethod,
81+
false,
82+
"requests",
83+
this.fillTpsWithZeroes
84+
);
85+
}
86+
7587
metricInvocationCount() {
7688
return this.metricFactory.createMetric(
7789
"Count",

lib/monitoring/aws-apigatewayv2/ApiGatewayV2HttpApiMetricFactory.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ export class ApiGatewayV2HttpApiMetricFactory {
5858
};
5959
}
6060

61+
/**
62+
* @deprecated use metricInvocationRate
63+
*/
6164
metricTps() {
62-
// TODO: rename to metricInvocationRate and use rateComputationMethod
6365
return this.metricFactory.toRate(
6466
this.metricInvocationCount(),
6567
RateComputationMethod.PER_SECOND,
@@ -68,6 +70,15 @@ export class ApiGatewayV2HttpApiMetricFactory {
6870
);
6971
}
7072

73+
metricInvocationRate() {
74+
return this.metricFactory.toRate(
75+
this.metricInvocationCount(),
76+
this.rateComputationMethod,
77+
false,
78+
"requests"
79+
);
80+
}
81+
7182
metricInvocationCount() {
7283
return this.metricFactory.createMetric(
7384
"Count",

lib/monitoring/aws-appsync/AppSyncMetricFactory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ export class AppSyncMetricFactory {
4242
};
4343
}
4444

45+
/**
46+
* @deprecated use metricRequestRate
47+
*/
4548
metricTps() {
46-
// TODO: rename to metricInvocationRate and use rateComputationMethod
4749
return this.metricFactory.toRate(
4850
this.metricRequestCount(),
4951
RateComputationMethod.PER_SECOND,
@@ -53,6 +55,16 @@ export class AppSyncMetricFactory {
5355
);
5456
}
5557

58+
metricRequestRate() {
59+
return this.metricFactory.toRate(
60+
this.metricRequestCount(),
61+
this.rateComputationMethod,
62+
true,
63+
"requests",
64+
this.fillTpsWithZeroes
65+
);
66+
}
67+
5668
metricRequestCount() {
5769
return this.metricFactory.createMetric(
5870
"Latency",

lib/monitoring/aws-cloudfront/CloudFrontDistributionMetricFactory.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export interface CloudFrontDistributionMetricFactoryProps {
1717
* @default true
1818
*/
1919
readonly fillTpsWithZeroes?: boolean;
20+
/**
21+
* @default average
22+
*/
23+
readonly rateComputationMethod?: RateComputationMethod;
2024
}
2125

2226
/**
@@ -26,6 +30,7 @@ export interface CloudFrontDistributionMetricFactoryProps {
2630
export class CloudFrontDistributionMetricFactory {
2731
private readonly metricFactory: MetricFactory;
2832
private readonly fillTpsWithZeroes: boolean;
33+
private readonly rateComputationMethod: RateComputationMethod;
2934
private readonly dimensionsMap: DimensionsMap;
3035

3136
constructor(
@@ -34,6 +39,8 @@ export class CloudFrontDistributionMetricFactory {
3439
) {
3540
this.metricFactory = metricFactory;
3641
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
42+
this.rateComputationMethod =
43+
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
3744
this.dimensionsMap = {
3845
DistributionId: props.distribution.distributionId,
3946
Region: CloudFrontGlobalRegion,
@@ -53,8 +60,20 @@ export class CloudFrontDistributionMetricFactory {
5360
.with({ region: CloudFrontDefaultMetricRegion });
5461
}
5562

63+
metricRequestRate() {
64+
return this.metricFactory.toRate(
65+
this.metricRequestCount(),
66+
this.rateComputationMethod,
67+
false,
68+
"requests",
69+
this.fillTpsWithZeroes
70+
);
71+
}
72+
73+
/**
74+
* @deprecated use metricRequestRate
75+
*/
5676
metricRequestTps() {
57-
// TODO: rename to metricInvocationRate and use rateComputationMethod
5877
return this.metricFactory.toRate(
5978
this.metricRequestCount(),
6079
RateComputationMethod.PER_SECOND,

lib/monitoring/aws-glue/GlueJobMetricFactory.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@ import {
88

99
const GlueNamespace = "Glue";
1010

11+
export interface GlueJobMetricFactoryProps {
12+
readonly jobName: string;
13+
/**
14+
* @default average
15+
*/
16+
readonly rateComputationMethod?: RateComputationMethod;
17+
}
18+
1119
export class GlueJobMetricFactory {
1220
protected readonly metricFactory: MetricFactory;
21+
protected readonly rateComputationMethod: RateComputationMethod;
1322
protected readonly dimensionsMap: DimensionsMap;
1423

15-
constructor(metricFactory: MetricFactory, jobName: string) {
24+
constructor(metricFactory: MetricFactory, props: GlueJobMetricFactoryProps) {
1625
this.metricFactory = metricFactory;
26+
this.rateComputationMethod =
27+
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
1728
this.dimensionsMap = {
1829
Type: "gauge",
1930
JobRunId: "ALL",
20-
JobName: jobName,
31+
JobName: props.jobName,
2132
};
2233
}
2334

@@ -112,7 +123,7 @@ export class GlueJobMetricFactory {
112123
metricFailedTasksRate() {
113124
return this.metricFactory.toRate(
114125
this.metricFailedTasksSum(),
115-
RateComputationMethod.AVERAGE,
126+
this.rateComputationMethod,
116127
true,
117128
"killed",
118129
false
@@ -133,7 +144,7 @@ export class GlueJobMetricFactory {
133144
metricKilledTasksRate() {
134145
return this.metricFactory.toRate(
135146
this.metricKilledTasksSum(),
136-
RateComputationMethod.AVERAGE,
147+
this.rateComputationMethod,
137148
true,
138149
"killed",
139150
false

lib/monitoring/aws-glue/GlueJobMonitoring.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ import {
2828
MonitoringHeaderWidget,
2929
MonitoringNamingStrategy,
3030
} from "../../dashboard";
31-
import { GlueJobMetricFactory } from "./GlueJobMetricFactory";
31+
import {
32+
GlueJobMetricFactory,
33+
GlueJobMetricFactoryProps,
34+
} from "./GlueJobMetricFactory";
3235

33-
export interface GlueJobMonitoringOptions extends BaseMonitoringProps {
36+
export interface GlueJobMonitoringOptions
37+
extends GlueJobMetricFactoryProps,
38+
BaseMonitoringProps {
3439
readonly addFailedTaskCountAlarm?: Record<string, ErrorCountThreshold>;
3540
readonly addFailedTaskRateAlarm?: Record<string, ErrorRateThreshold>;
3641
readonly addKilledTaskCountAlarm?: Record<string, ErrorCountThreshold>;
3742
readonly addKilledTaskRateAlarm?: Record<string, ErrorRateThreshold>;
3843
}
3944

40-
export interface GlueJobMonitoringProps extends GlueJobMonitoringOptions {
41-
readonly jobName: string;
42-
}
45+
export interface GlueJobMonitoringProps extends GlueJobMonitoringOptions {}
4346

4447
export class GlueJobMonitoring extends Monitoring {
4548
protected readonly title: string;
@@ -74,7 +77,7 @@ export class GlueJobMonitoring extends Monitoring {
7477

7578
const metricFactory = new GlueJobMetricFactory(
7679
scope.createMetricFactory(),
77-
props.jobName
80+
props
7881
);
7982

8083
this.alarmFactory = this.createAlarmFactory(

lib/monitoring/aws-lambda/LambdaFunctionMetricFactory.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ export class LambdaFunctionMetricFactory {
4444
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
4545
}
4646

47+
/**
48+
* @deprecated use metricInvocationRate
49+
*/
4750
metricTps() {
48-
// TODO: rename to metricInvocationRate and use rateComputationMethod
4951
return this.metricFactory.toRate(
5052
this.metricInvocationCount(),
5153
RateComputationMethod.PER_SECOND,
@@ -55,6 +57,16 @@ export class LambdaFunctionMetricFactory {
5557
);
5658
}
5759

60+
metricInvocationRate() {
61+
return this.metricFactory.toRate(
62+
this.metricInvocationCount(),
63+
this.rateComputationMethod,
64+
false,
65+
"requests",
66+
this.fillTpsWithZeroes
67+
);
68+
}
69+
5870
metricInvocationCount() {
5971
return this.metricFactory.adaptMetric(
6072
this.lambdaFunction.metricInvocations({

lib/monitoring/aws-opensearch/OpenSearchClusterMetricFactory.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ export interface OpenSearchClusterMetricFactoryProps {
1616
* @default true
1717
*/
1818
readonly fillTpsWithZeroes?: boolean;
19+
/**
20+
* @default average
21+
*/
22+
readonly rateComputationMethod?: RateComputationMethod;
1923
}
2024

2125
export class OpenSearchClusterMetricFactory {
2226
protected readonly metricFactory: MetricFactory;
2327
protected readonly domainMetrics: OpenSearchBackportedMetrics;
2428
protected readonly fillTpsWithZeroes: boolean;
29+
protected readonly rateComputationMethod: RateComputationMethod;
2530

2631
constructor(
2732
metricFactory: MetricFactory,
@@ -30,15 +35,32 @@ export class OpenSearchClusterMetricFactory {
3035
this.metricFactory = metricFactory;
3136
this.domainMetrics = new OpenSearchBackportedMetrics(props.domain);
3237
this.fillTpsWithZeroes = props.fillTpsWithZeroes ?? true;
38+
this.rateComputationMethod =
39+
props.rateComputationMethod ?? RateComputationMethod.AVERAGE;
3340
}
3441

35-
metricTps() {
36-
// TODO: rename to metricInvocationRate and use rateComputationMethod
37-
const requests = this.domainMetrics.metric("SearchRate", {
42+
metricSearchCount() {
43+
return this.domainMetrics.metric("SearchRate", {
3844
statistic: Statistic.SUM,
3945
});
46+
}
47+
48+
metricSearchRate() {
49+
return this.metricFactory.toRate(
50+
this.metricSearchCount(),
51+
this.rateComputationMethod,
52+
false,
53+
"requests",
54+
this.fillTpsWithZeroes
55+
);
56+
}
57+
58+
/**
59+
* @deprecated use metricSearchRate
60+
*/
61+
metricTps() {
4062
return this.metricFactory.toRate(
41-
requests,
63+
this.metricSearchCount(),
4264
RateComputationMethod.PER_SECOND,
4365
false,
4466
"requests",

0 commit comments

Comments
 (0)