This repository has been archived by the owner on Jan 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfargate-service-metric-factory.ts
133 lines (120 loc) · 4.95 KB
/
fargate-service-metric-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { FargateService } from 'aws-cdk-lib/aws-ecs';
import {
DEFAULT_METRIC_PERIOD, Metric, MetricOptions, MetricStatistic,
} from '../../common/metrics';
import { MetricFactory } from '../metric-factory';
import { ResourceMetricProps } from '../resource-metric';
const EcsContainerInsightsNamespace = 'ECS/ContainerInsights';
const COLOR_NOT_SPECIFIED = undefined;
/**
* Network Load Balanced Fargate Service Metric Factory Props.
* Defines the metrics for the resource against which monitoring can be done.
*
*/
export interface FargateServiceMetricFactoryProps extends ResourceMetricProps {
/**
* Fargate service instance. Must pass this or fargateServiceCore.
*/
readonly fargateServiceCore: FargateService;
/**
* Metric Options for the Cpu Maximum utilization
*
* Refer: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.FargateService.html#metricwbrcpuwbrutilizationprops
*/
readonly cpuUtilizationPercentMetricOptions?: MetricOptions;
/**
* Metric Options for the Memory Maximum utilization.
*
* Refer : https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.FargateService.html#metricwbrmemorywbrutilizationprops
*/
readonly memoryUtilizationPercentMetricOptions?: MetricOptions;
/**
* Metric Options for the Healthy Hosts Count.
*/
readonly healthyHostsMetricOptions?: MetricOptions;
/**
* Metric Options for Disk Utilization provided by ContainerInsights
*/
readonly diskUtilizationMetricOptions?: MetricOptions;
/**
* The metric statistic for the running tasks on which metric has to be formed.
*/
readonly runningTasksMetricStatistic ?: MetricStatistic;
}
export class FargateServiceMetricFactory {
private readonly props: FargateServiceMetricFactoryProps;
protected readonly metricFactory: MetricFactory;
private readonly fargateServiceCore: FargateService;
constructor(metricFactory: MetricFactory, props: FargateServiceMetricFactoryProps) {
this.props = props;
this.metricFactory = metricFactory;
this.fargateServiceCore = props.fargateServiceCore;
}
metricClusterCpuUtilizationInPercent() : Metric {
return this.fargateServiceCore.metricCpuUtilization({
label: 'Cluster CPU Utilization',
period: this.props.cpuUtilizationPercentMetricOptions?.period ?? DEFAULT_METRIC_PERIOD,
});
}
metricClusterMemoryUtilizationInPercent(): Metric {
return this.fargateServiceCore.metricMemoryUtilization({
label: 'Cluster Memory Utilization',
period: this.props.memoryUtilizationPercentMetricOptions?.period ?? DEFAULT_METRIC_PERIOD,
});
}
metricRunningTaskCount(): Metric {
return this.metricFactory.createMetric(
'RunningTaskCount',
this.props.runningTasksMetricStatistic ?? MetricStatistic.AVERAGE,
'Running Tasks',
{
ServiceName: this.fargateServiceCore.serviceName,
ClusterName: this.fargateServiceCore.cluster.clusterName,
},
COLOR_NOT_SPECIFIED,
EcsContainerInsightsNamespace,
);
}
metricDiskUtilization(): Metric {
const ephemeralStorareReserved = this.metricFactory
.createMetric(
'EphemeralStorageReserved',
MetricStatistic.SUM,
'Ephemeral GiB Reserved',
{
ClusterName: this.fargateServiceCore.cluster.clusterName,
ServiceName: this.fargateServiceCore.serviceName,
},
COLOR_NOT_SPECIFIED,
EcsContainerInsightsNamespace,
this.props.diskUtilizationMetricOptions?.period ?? DEFAULT_METRIC_PERIOD,
);
const ephemeralStorageUtilized = this.metricFactory
.createMetric(
'EphemeralStorageUtilized',
MetricStatistic.SUM,
'Ephemeral GiB Utilized',
{
ClusterName: this.fargateServiceCore.cluster.clusterName,
ServiceName: this.fargateServiceCore.serviceName,
},
COLOR_NOT_SPECIFIED,
EcsContainerInsightsNamespace,
this.props.diskUtilizationMetricOptions?.period ?? DEFAULT_METRIC_PERIOD,
);
return this.metricFactory.createMetricMath(
'storageUtilized * 100 / storageReserved',
{
storageReserved: ephemeralStorareReserved,
storageUtilized: ephemeralStorageUtilized,
},
'DiskSpaceUtilization',
COLOR_NOT_SPECIFIED,
this.props.diskUtilizationMetricOptions?.period ?? DEFAULT_METRIC_PERIOD,
);
}
// JSII forbids 'get*' method names because they conflict with Java autogenerated code
provideFargateServiceCore(): FargateService {
return this.fargateServiceCore;
}
}