1- import { GraphWidget , IWidget } from "aws-cdk-lib/aws-cloudwatch" ;
1+ import {
2+ GraphWidget ,
3+ HorizontalAnnotation ,
4+ IWidget ,
5+ } from "aws-cdk-lib/aws-cloudwatch" ;
26import {
37 WafV2MetricFactory ,
48 WafV2MetricFactoryProps ,
59} from "./WafV2MetricFactory" ;
610import {
11+ AlarmFactory ,
712 BaseMonitoringProps ,
813 CountAxisFromZero ,
914 DefaultGraphWidgetHeight ,
1015 DefaultSummaryWidgetHeight ,
16+ ErrorAlarmFactory ,
17+ ErrorCountThreshold ,
18+ ErrorRateThreshold ,
19+ ErrorType ,
1120 MetricWithAlarmSupport ,
1221 Monitoring ,
1322 MonitoringScope ,
@@ -23,7 +32,10 @@ export interface WafV2MonitoringOptions extends BaseMonitoringProps {}
2332
2433export interface WafV2MonitoringProps
2534 extends WafV2MetricFactoryProps ,
26- WafV2MonitoringOptions { }
35+ WafV2MonitoringOptions {
36+ readonly addBlockedRequestsCountAlarm ?: Record < string , ErrorCountThreshold > ;
37+ readonly addBlockedRequestsRateAlarm ?: Record < string , ErrorRateThreshold > ;
38+ }
2739
2840/**
2941 * Monitoring for AWS Web Application Firewall.
@@ -33,6 +45,12 @@ export interface WafV2MonitoringProps
3345export class WafV2Monitoring extends Monitoring {
3446 readonly humanReadableName : string ;
3547
48+ readonly alarmFactory : AlarmFactory ;
49+ readonly errorAlarmFactory : ErrorAlarmFactory ;
50+
51+ readonly errorCountAnnotations : HorizontalAnnotation [ ] ;
52+ readonly errorRateAnnotations : HorizontalAnnotation [ ] ;
53+
3654 readonly allowedRequestsMetric : MetricWithAlarmSupport ;
3755 readonly blockedRequestsMetric : MetricWithAlarmSupport ;
3856 readonly blockedRequestsRateMetric : MetricWithAlarmSupport ;
@@ -46,6 +64,15 @@ export class WafV2Monitoring extends Monitoring {
4664 } ) ;
4765 this . humanReadableName = namingStrategy . resolveHumanReadableName ( ) ;
4866
67+ this . alarmFactory = this . createAlarmFactory (
68+ namingStrategy . resolveAlarmFriendlyName ( )
69+ ) ;
70+
71+ this . errorAlarmFactory = new ErrorAlarmFactory ( this . alarmFactory ) ;
72+
73+ this . errorCountAnnotations = [ ] ;
74+ this . errorRateAnnotations = [ ] ;
75+
4976 const metricFactory = new WafV2MetricFactory (
5077 scope . createMetricFactory ( ) ,
5178 props
@@ -54,6 +81,31 @@ export class WafV2Monitoring extends Monitoring {
5481 this . allowedRequestsMetric = metricFactory . metricAllowedRequests ( ) ;
5582 this . blockedRequestsMetric = metricFactory . metricBlockedRequests ( ) ;
5683 this . blockedRequestsRateMetric = metricFactory . metricBlockedRequestsRate ( ) ;
84+
85+ for ( const disambiguator in props . addBlockedRequestsCountAlarm ) {
86+ const alarmProps = props . addBlockedRequestsCountAlarm [ disambiguator ] ;
87+ const createdAlarm = this . errorAlarmFactory . addErrorCountAlarm (
88+ this . blockedRequestsMetric ,
89+ ErrorType . BLOCKED ,
90+ alarmProps ,
91+ disambiguator
92+ ) ;
93+ this . errorCountAnnotations . push ( createdAlarm . annotation ) ;
94+ this . addAlarm ( createdAlarm ) ;
95+ }
96+ for ( const disambiguator in props . addBlockedRequestsRateAlarm ) {
97+ const alarmProps = props . addBlockedRequestsRateAlarm [ disambiguator ] ;
98+ const createdAlarm = this . errorAlarmFactory . addErrorRateAlarm (
99+ this . blockedRequestsRateMetric ,
100+ ErrorType . BLOCKED ,
101+ alarmProps ,
102+ disambiguator
103+ ) ;
104+ this . errorRateAnnotations . push ( createdAlarm . annotation ) ;
105+ this . addAlarm ( createdAlarm ) ;
106+ }
107+
108+ props . useCreatedAlarms ?. consume ( this . createdAlarms ( ) ) ;
57109 }
58110
59111 summaryWidgets ( ) : IWidget [ ] {
@@ -103,6 +155,7 @@ export class WafV2Monitoring extends Monitoring {
103155 height,
104156 title : "Blocked Requests" ,
105157 left : [ this . blockedRequestsMetric ] ,
158+ leftAnnotations : this . errorCountAnnotations ,
106159 leftYAxis : CountAxisFromZero ,
107160 } ) ;
108161 }
@@ -113,6 +166,7 @@ export class WafV2Monitoring extends Monitoring {
113166 height,
114167 title : "Blocked Requests (rate)" ,
115168 left : [ this . blockedRequestsRateMetric ] ,
169+ leftAnnotations : this . errorRateAnnotations ,
116170 leftYAxis : RateAxisFromZero ,
117171 } ) ;
118172 }
0 commit comments