Skip to content

Commit be76e86

Browse files
authored
fix(elb): allow inverting load balancer task count metric statistics (#387)
# Summary To monitor network load balancer metrics, track the maximum `HealthyHostCount` and the minimum `UnHealthyHostCount` as per the guidelines at <https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-cloudwatch-metrics.html#metric-statistics>. Fixes #392 # Tests ``` yarn build ``` The `package:js` build step has succeeded. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent b4f2ddf commit be76e86

File tree

10 files changed

+42012
-5432
lines changed

10 files changed

+42012
-5432
lines changed

API.md

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

lib/monitoring/aws-ecs-patterns/Ec2ServiceMonitoring.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ interface BaseLoadBalancedEc2ServiceMonitoringProps
102102
string,
103103
MinProcessedBytesThreshold
104104
>;
105+
106+
/**
107+
* Invert the statistics of `HealthyHostCount` and `UnHealthyHostCount`.
108+
*
109+
* When `invertLoadBalancerTaskCountMetricsStatistics` is set to false, the minimum of `HealthyHostCount` and the maximum of `UnHealthyHostCount` are monitored.
110+
* When `invertLoadBalancerTaskCountMetricsStatistics` is set to true, the maximum of `HealthyHostCount` and the minimum of `UnHealthyHostCount` are monitored.
111+
*
112+
* `invertLoadBalancerTaskCountMetricsStatistics` is recommended to set to true as per the guidelines at
113+
https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-cloudwatch-metrics.html#metric-statistics
114+
*
115+
* @default false
116+
*/
117+
readonly invertLoadBalancerTaskCountMetricsStatistics?: boolean;
105118
}
106119

107120
/**
@@ -187,7 +200,8 @@ export class Ec2ServiceMonitoring extends Monitoring {
187200
this.loadBalancerMetricFactory = createLoadBalancerMetricFactory(
188201
this.metricFactory,
189202
props.loadBalancer!,
190-
props.targetGroup!
203+
props.targetGroup!,
204+
props.invertLoadBalancerTaskCountMetricsStatistics
191205
);
192206
this.healthyTaskCountMetric =
193207
this.loadBalancerMetricFactory.metricHealthyTaskCount();

lib/monitoring/aws-ecs-patterns/FargateServiceMonitoring.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ interface BaseLoadBalancedFargateServiceMonitoringProps
102102
string,
103103
MinProcessedBytesThreshold
104104
>;
105+
106+
/**
107+
* Invert the statistics of `HealthyHostCount` and `UnHealthyHostCount`.
108+
*
109+
* When `invertLoadBalancerTaskCountMetricsStatistics` is set to false, the minimum of `HealthyHostCount` and the maximum of `UnHealthyHostCount` are monitored.
110+
* When `invertLoadBalancerTaskCountMetricsStatistics` is set to true, the maximum of `HealthyHostCount` and the minimum of `UnHealthyHostCount` are monitored.
111+
*
112+
* `invertLoadBalancerTaskCountMetricsStatistics` is recommended to set to true as per the guidelines at
113+
https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-cloudwatch-metrics.html#metric-statistics
114+
*
115+
* @default false
116+
*/
117+
readonly invertLoadBalancerTaskCountMetricsStatistics?: boolean;
105118
}
106119

107120
/**
@@ -190,7 +203,8 @@ export class FargateServiceMonitoring extends Monitoring {
190203
this.loadBalancerMetricFactory = createLoadBalancerMetricFactory(
191204
this.metricFactory,
192205
props.loadBalancer!,
193-
props.targetGroup!
206+
props.targetGroup!,
207+
props.invertLoadBalancerTaskCountMetricsStatistics
194208
);
195209
this.healthyTaskCountMetric =
196210
this.loadBalancerMetricFactory.metricHealthyTaskCount();

lib/monitoring/aws-loadbalancing/ApplicationLoadBalancerMetricFactory.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
IApplicationTargetGroup,
44
} from "aws-cdk-lib/aws-elasticloadbalancingv2";
55

6-
import { ILoadBalancerMetricFactory } from "./LoadBalancerMetricFactory";
6+
import {
7+
ILoadBalancerMetricFactory,
8+
BaseLoadBalancerMetricFactoryProps,
9+
} from "./LoadBalancerMetricFactory";
710
import {
811
HealthyMetricColor,
912
MetricFactory,
@@ -14,7 +17,8 @@ import {
1417
/**
1518
* Props to create ApplicationLoadBalancerMetricFactory.
1619
*/
17-
export interface ApplicationLoadBalancerMetricFactoryProps {
20+
export interface ApplicationLoadBalancerMetricFactoryProps
21+
extends BaseLoadBalancerMetricFactoryProps {
1822
readonly applicationLoadBalancer: IApplicationLoadBalancer;
1923
readonly applicationTargetGroup: IApplicationTargetGroup;
2024
}
@@ -28,6 +32,7 @@ export class ApplicationLoadBalancerMetricFactory
2832
protected readonly metricFactory: MetricFactory;
2933
protected readonly applicationLoadBalancer: IApplicationLoadBalancer;
3034
protected readonly applicationTargetGroup: IApplicationTargetGroup;
35+
protected readonly invertStatisticsOfTaskCountEnabled: boolean;
3136

3237
constructor(
3338
metricFactory: MetricFactory,
@@ -36,14 +41,18 @@ export class ApplicationLoadBalancerMetricFactory
3641
this.metricFactory = metricFactory;
3742
this.applicationLoadBalancer = props.applicationLoadBalancer;
3843
this.applicationTargetGroup = props.applicationTargetGroup;
44+
this.invertStatisticsOfTaskCountEnabled =
45+
props.invertStatisticsOfTaskCountEnabled ?? false;
3946
}
4047

4148
metricHealthyTaskCount() {
4249
return this.metricFactory.adaptMetric(
4350
this.applicationTargetGroup.metrics.healthyHostCount({
4451
label: "Healthy Tasks",
4552
color: HealthyMetricColor,
46-
statistic: MetricStatistic.MIN,
53+
statistic: this.invertStatisticsOfTaskCountEnabled
54+
? MetricStatistic.MAX
55+
: MetricStatistic.MIN,
4756
})
4857
);
4958
}
@@ -53,7 +62,9 @@ export class ApplicationLoadBalancerMetricFactory
5362
this.applicationTargetGroup.metrics.unhealthyHostCount({
5463
label: "Unhealthy Tasks",
5564
color: UnhealthyMetricColor,
56-
statistic: MetricStatistic.MAX,
65+
statistic: this.invertStatisticsOfTaskCountEnabled
66+
? MetricStatistic.MIN
67+
: MetricStatistic.MAX,
5768
})
5869
);
5970
}

lib/monitoring/aws-loadbalancing/LoadBalancerMetricFactory.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function isNetworkTargetGroup(
4646
export function createLoadBalancerMetricFactory(
4747
metricFactory: MetricFactory,
4848
loadBalancer: INetworkLoadBalancer | IApplicationLoadBalancer,
49-
targetGroup: INetworkTargetGroup | IApplicationTargetGroup
49+
targetGroup: INetworkTargetGroup | IApplicationTargetGroup,
50+
invertStatisticsOfTaskCountEnabled?: boolean
5051
): ILoadBalancerMetricFactory {
5152
if (
5253
isNetworkLoadBalancer(loadBalancer) &&
@@ -55,6 +56,7 @@ export function createLoadBalancerMetricFactory(
5556
return new NetworkLoadBalancerMetricFactory(metricFactory, {
5657
networkLoadBalancer: loadBalancer,
5758
networkTargetGroup: targetGroup,
59+
invertStatisticsOfTaskCountEnabled: invertStatisticsOfTaskCountEnabled,
5860
});
5961
} else if (
6062
isApplicationLoadBalancer(loadBalancer) &&
@@ -63,6 +65,7 @@ export function createLoadBalancerMetricFactory(
6365
return new ApplicationLoadBalancerMetricFactory(metricFactory, {
6466
applicationLoadBalancer: loadBalancer,
6567
applicationTargetGroup: targetGroup,
68+
invertStatisticsOfTaskCountEnabled: invertStatisticsOfTaskCountEnabled,
6669
});
6770
} else {
6871
throw new Error(
@@ -71,6 +74,24 @@ export function createLoadBalancerMetricFactory(
7174
}
7275
}
7376

77+
/**
78+
* Base of Monitoring props for load-balancer metric factories.
79+
*/
80+
export interface BaseLoadBalancerMetricFactoryProps {
81+
/**
82+
* Invert the statistics of `HealthyHostCount` and `UnHealthyHostCount`.
83+
*
84+
* When `invertStatisticsOfTaskCountEnabled` is set to false, the minimum of `HealthyHostCount` and the maximum of `UnHealthyHostCount` are monitored.
85+
* When `invertStatisticsOfTaskCountEnabled` is set to true, the maximum of `HealthyHostCount` and the minimum of `UnHealthyHostCount` are monitored.
86+
*
87+
* `invertStatisticsOfTaskCountEnabled` is recommended to set to true as per the guidelines at
88+
https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-cloudwatch-metrics.html#metric-statistics
89+
*
90+
* @default false
91+
*/
92+
readonly invertStatisticsOfTaskCountEnabled?: boolean;
93+
}
94+
7495
/**
7596
* Common interface for load-balancer based service metric factories.
7697
*/

lib/monitoring/aws-loadbalancing/NetworkLoadBalancerMetricFactory.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
INetworkTargetGroup,
44
} from "aws-cdk-lib/aws-elasticloadbalancingv2";
55

6-
import { ILoadBalancerMetricFactory } from "./LoadBalancerMetricFactory";
6+
import {
7+
ILoadBalancerMetricFactory,
8+
BaseLoadBalancerMetricFactoryProps,
9+
} from "./LoadBalancerMetricFactory";
710
import {
811
HealthyMetricColor,
912
MetricFactory,
@@ -14,7 +17,8 @@ import {
1417
/**
1518
* Props to create NetworkLoadBalancerMetricFactory.
1619
*/
17-
export interface NetworkLoadBalancerMetricFactoryProps {
20+
export interface NetworkLoadBalancerMetricFactoryProps
21+
extends BaseLoadBalancerMetricFactoryProps {
1822
readonly networkLoadBalancer: INetworkLoadBalancer;
1923
readonly networkTargetGroup: INetworkTargetGroup;
2024
}
@@ -28,6 +32,7 @@ export class NetworkLoadBalancerMetricFactory
2832
protected readonly metricFactory: MetricFactory;
2933
protected readonly networkLoadBalancer: INetworkLoadBalancer;
3034
protected readonly networkTargetGroup: INetworkTargetGroup;
35+
protected readonly invertStatisticsOfTaskCountEnabled: boolean;
3136

3237
constructor(
3338
metricFactory: MetricFactory,
@@ -36,14 +41,18 @@ export class NetworkLoadBalancerMetricFactory
3641
this.metricFactory = metricFactory;
3742
this.networkLoadBalancer = props.networkLoadBalancer;
3843
this.networkTargetGroup = props.networkTargetGroup;
44+
this.invertStatisticsOfTaskCountEnabled =
45+
props.invertStatisticsOfTaskCountEnabled ?? false;
3946
}
4047

4148
metricHealthyTaskCount() {
4249
return this.metricFactory.adaptMetric(
4350
this.networkTargetGroup.metrics.healthyHostCount({
4451
label: "Healthy Tasks",
4552
color: HealthyMetricColor,
46-
statistic: MetricStatistic.MIN,
53+
statistic: this.invertStatisticsOfTaskCountEnabled
54+
? MetricStatistic.MAX
55+
: MetricStatistic.MIN,
4756
})
4857
);
4958
}
@@ -53,7 +62,9 @@ export class NetworkLoadBalancerMetricFactory
5362
this.networkTargetGroup.metrics.unHealthyHostCount({
5463
label: "Unhealthy Tasks",
5564
color: UnhealthyMetricColor,
56-
statistic: MetricStatistic.MAX,
65+
statistic: this.invertStatisticsOfTaskCountEnabled
66+
? MetricStatistic.MIN
67+
: MetricStatistic.MAX,
5768
})
5869
);
5970
}

0 commit comments

Comments
 (0)