1- import { GraphWidget , IMetric , IWidget } from "aws-cdk-lib/aws-cloudwatch" ;
1+ import {
2+ GraphWidget ,
3+ HorizontalAnnotation ,
4+ IMetric ,
5+ IWidget ,
6+ } from "aws-cdk-lib/aws-cloudwatch" ;
27
38import { EC2MetricFactory , EC2MetricFactoryProps } from "./EC2MetricFactory" ;
49import {
510 BaseMonitoringProps ,
611 CountAxisFromZero ,
712 DefaultGraphWidgetHeight ,
813 DefaultSummaryWidgetHeight ,
14+ MetricWithAlarmSupport ,
915 Monitoring ,
1016 MonitoringScope ,
1117 PercentageAxisFromZeroToHundred ,
1218 QuarterWidth ,
1319 SizeAxisBytesFromZero ,
1420 ThirdWidth ,
1521} from "../../common" ;
22+ import {
23+ EC2AlarmFactory ,
24+ NetworkInThreshold ,
25+ NetworkOutThreshold ,
26+ } from "../../common/monitoring/alarms/EC2AlarmFactory" ;
1627import {
1728 MonitoringHeaderWidget ,
1829 MonitoringNamingStrategy ,
1930} from "../../dashboard" ;
2031
2132export interface EC2MonitoringOptions
2233 extends EC2MetricFactoryProps ,
23- BaseMonitoringProps { }
34+ BaseMonitoringProps {
35+ readonly addNetworkOutTotalBytesExceedThresholdAlarm ?: Record <
36+ string ,
37+ NetworkOutThreshold
38+ > ;
39+
40+ readonly addNetworkInTotalBytesExceedThresholdAlarm ?: Record <
41+ string ,
42+ NetworkInThreshold
43+ > ;
44+ }
2445
2546export interface EC2MonitoringProps extends EC2MonitoringOptions { }
2647
2748export class EC2Monitoring extends Monitoring {
2849 readonly family : string ;
2950 readonly title : string ;
3051
52+ readonly ec2AlarmFactory : EC2AlarmFactory ;
53+
3154 readonly cpuUtilisationMetrics : IMetric [ ] ;
3255 readonly diskReadBytesMetrics : IMetric [ ] ;
3356 readonly diskWriteBytesMetrics : IMetric [ ] ;
3457 readonly diskReadOpsMetrics : IMetric [ ] ;
3558 readonly diskWriteOpsMetrics : IMetric [ ] ;
36- readonly networkInMetrics : IMetric [ ] ;
37- readonly networkOutMetrics : IMetric [ ] ;
59+ readonly networkInMetrics : MetricWithAlarmSupport [ ] ;
60+ readonly networkOutMetrics : MetricWithAlarmSupport [ ] ;
61+
62+ readonly networkInSumMetrics : MetricWithAlarmSupport [ ] ;
63+ readonly networkOutSumMetrics : MetricWithAlarmSupport [ ] ;
64+
65+ readonly networkInSumLimitAnnotations : HorizontalAnnotation [ ] ;
66+ readonly networkOutSumLimitAnnotations : HorizontalAnnotation [ ] ;
3867
3968 constructor ( scope : MonitoringScope , props : EC2MonitoringProps ) {
4069 super ( scope , props ) ;
@@ -48,11 +77,28 @@ export class EC2Monitoring extends Monitoring {
4877 } ) ;
4978 this . family = props . autoScalingGroup ? "EC2 Auto Scaling Group" : "EC2" ;
5079 this . title = namingStrategy . resolveHumanReadableName ( ) ;
80+ this . networkOutSumLimitAnnotations = [ ] ;
81+ this . networkInSumLimitAnnotations = [ ] ;
5182
5283 const metricFactory = new EC2MetricFactory (
5384 scope . createMetricFactory ( ) ,
5485 props ,
5586 ) ;
87+
88+ // using different fallback alarm construct name
89+ // as alarms don't allow whitespace
90+ const fallbackAlarmConstructName = props . autoScalingGroup
91+ ? props . autoScalingGroup . autoScalingGroupName
92+ : "All-Instances" ;
93+ const namingAlarmStrategy = new MonitoringNamingStrategy ( {
94+ ...props ,
95+ fallbackConstructName : fallbackAlarmConstructName ,
96+ } ) ;
97+ const alarmFactory = this . createAlarmFactory (
98+ namingAlarmStrategy . resolveAlarmFriendlyName ( ) ,
99+ ) ;
100+ this . ec2AlarmFactory = new EC2AlarmFactory ( alarmFactory ) ;
101+
56102 this . cpuUtilisationMetrics =
57103 metricFactory . metricAverageCpuUtilisationPercent ( ) ;
58104 this . diskReadBytesMetrics = metricFactory . metricAverageDiskReadBytes ( ) ;
@@ -61,6 +107,47 @@ export class EC2Monitoring extends Monitoring {
61107 this . diskWriteOpsMetrics = metricFactory . metricAverageDiskWriteOps ( ) ;
62108 this . networkInMetrics = metricFactory . metricAverageNetworkInRateBytes ( ) ;
63109 this . networkOutMetrics = metricFactory . metricAverageNetworkOutRateBytes ( ) ;
110+
111+ this . networkInSumMetrics = metricFactory . metricSumNetworkInRateBytes ( ) ;
112+ this . networkOutSumMetrics = metricFactory . metricSumNetworkOutRateBytes ( ) ;
113+
114+ for ( const disambiguator in props . addNetworkInTotalBytesExceedThresholdAlarm ) {
115+ const alarmProps =
116+ props . addNetworkInTotalBytesExceedThresholdAlarm [ disambiguator ] ;
117+
118+ const createdAlarms = this . networkInMetrics . map ( ( metric ) => {
119+ const createdAlarm = this . ec2AlarmFactory . addNetworkInAlarm (
120+ metric ,
121+ alarmProps ,
122+ disambiguator ,
123+ ) ;
124+ this . addAlarm ( createdAlarm ) ;
125+ return createdAlarm ;
126+ } ) ;
127+
128+ if ( createdAlarms . length > 0 ) {
129+ this . networkInSumLimitAnnotations . push ( createdAlarms [ 0 ] . annotation ) ;
130+ }
131+ }
132+
133+ for ( const disambiguator in props . addNetworkOutTotalBytesExceedThresholdAlarm ) {
134+ const alarmProps =
135+ props . addNetworkOutTotalBytesExceedThresholdAlarm [ disambiguator ] ;
136+ const createdAlarms = this . networkOutSumMetrics . map ( ( metric ) => {
137+ const createdAlarm = this . ec2AlarmFactory . addNetworkOutAlarm (
138+ metric ,
139+ alarmProps ,
140+ disambiguator ,
141+ ) ;
142+ this . addAlarm ( createdAlarm ) ;
143+ return createdAlarm ;
144+ } ) ;
145+
146+ if ( createdAlarms . length > 0 ) {
147+ this . networkOutSumLimitAnnotations . push ( createdAlarms [ 0 ] . annotation ) ;
148+ }
149+ }
150+ props . useCreatedAlarms ?. consume ( this . createdAlarms ( ) ) ;
64151 }
65152
66153 summaryWidgets ( ) : IWidget [ ] {
@@ -135,6 +222,12 @@ export class EC2Monitoring extends Monitoring {
135222 title : "Network" ,
136223 left : [ ...this . networkInMetrics , ...this . networkOutMetrics ] ,
137224 leftYAxis : SizeAxisBytesFromZero ,
225+ right : [ ...this . networkInSumMetrics , ...this . networkOutSumMetrics ] ,
226+ rightYAxis : SizeAxisBytesFromZero ,
227+ rightAnnotations : [
228+ ...this . networkInSumLimitAnnotations ,
229+ ...this . networkOutSumLimitAnnotations ,
230+ ] ,
138231 } ) ;
139232 }
140233}
0 commit comments