From b9176a54a6da25e6a98be4b0c79424a6bba0cddc Mon Sep 17 00:00:00 2001 From: Eugene Cheung <81188333+echeung-amzn@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:55:26 -0400 Subject: [PATCH] feat(logs): add addMaxIncomingLogsAlarm (#504) Closes #495 --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_ --- API.md | 11 +++++ .../aws-cloudwatch/LogMonitoring.ts | 13 +++++ .../aws-cloudwatch/LogMonitoring.test.ts | 7 ++- .../__snapshots__/LogMonitoring.test.ts.snap | 49 ++++++++++++++++++- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index f8f581c3..5c7630a7 100644 --- a/API.md +++ b/API.md @@ -25206,6 +25206,7 @@ const logMonitoringProps: LogMonitoringProps = { ... } | addToSummaryDashboard | boolean | Flag indicating if the widgets should be added to summary dashboard. | | useCreatedAlarms | IAlarmConsumer | Calls provided function to process all alarms created. | | logGroupName | string | Name of the log group to monitor. | +| addMaxIncomingLogsAlarm | {[ key: string ]: MaxUsageCountThreshold} | *No description.* | | addMinIncomingLogsAlarm | {[ key: string ]: MinUsageCountThreshold} | *No description.* | | limit | number | Maximum number of log messages to search for. | | pattern | string | Pattern to search for, e.g. "ERROR". | @@ -25323,6 +25324,16 @@ Name of the log group to monitor. --- +##### `addMaxIncomingLogsAlarm`Optional + +```typescript +public readonly addMaxIncomingLogsAlarm: {[ key: string ]: MaxUsageCountThreshold}; +``` + +- *Type:* {[ key: string ]: MaxUsageCountThreshold} + +--- + ##### `addMinIncomingLogsAlarm`Optional ```typescript diff --git a/lib/monitoring/aws-cloudwatch/LogMonitoring.ts b/lib/monitoring/aws-cloudwatch/LogMonitoring.ts index 9f9c679c..e37b1ffc 100644 --- a/lib/monitoring/aws-cloudwatch/LogMonitoring.ts +++ b/lib/monitoring/aws-cloudwatch/LogMonitoring.ts @@ -17,6 +17,7 @@ import { DefaultLogWidgetHeight, DefaultSummaryWidgetHeight, FullWidth, + MaxUsageCountThreshold, MetricWithAlarmSupport, MinUsageCountThreshold, Monitoring, @@ -55,6 +56,7 @@ export interface LogMonitoringProps readonly limit?: number; readonly addMinIncomingLogsAlarm?: Record; + readonly addMaxIncomingLogsAlarm?: Record; } /** @@ -116,6 +118,17 @@ export class LogMonitoring extends Monitoring { this.addAlarm(createdAlarm); } + for (const disambiguator in props.addMaxIncomingLogsAlarm) { + const alarmProps = props.addMaxIncomingLogsAlarm[disambiguator]; + const createdAlarm = this.usageAlarmFactory.addMaxCountAlarm( + this.incomingLogEventsMetric, + alarmProps, + disambiguator + ); + this.usageAnnotations.push(createdAlarm.annotation); + this.addAlarm(createdAlarm); + } + props.useCreatedAlarms?.consume(this.createdAlarms()); } diff --git a/test/monitoring/aws-cloudwatch/LogMonitoring.test.ts b/test/monitoring/aws-cloudwatch/LogMonitoring.test.ts index 309d8a12..e474656e 100644 --- a/test/monitoring/aws-cloudwatch/LogMonitoring.test.ts +++ b/test/monitoring/aws-cloudwatch/LogMonitoring.test.ts @@ -65,6 +65,11 @@ test("snapshot test: with alarms", () => { treatMissingDataOverride: TreatMissingData.BREACHING, }, }, + addMaxIncomingLogsAlarm: { + Critical: { + maxCount: 9001, + }, + }, useCreatedAlarms: { consume(alarms) { numAlarmsCreated = alarms.length; @@ -73,6 +78,6 @@ test("snapshot test: with alarms", () => { }); addMonitoringDashboardsToStack(stack, monitoring); - expect(numAlarmsCreated).toStrictEqual(1); + expect(numAlarmsCreated).toStrictEqual(2); expect(Template.fromStack(stack)).toMatchSnapshot(); }); diff --git a/test/monitoring/aws-cloudwatch/__snapshots__/LogMonitoring.test.ts.snap b/test/monitoring/aws-cloudwatch/__snapshots__/LogMonitoring.test.ts.snap index ee793f33..79130ea8 100644 --- a/test/monitoring/aws-cloudwatch/__snapshots__/LogMonitoring.test.ts.snap +++ b/test/monitoring/aws-cloudwatch/__snapshots__/LogMonitoring.test.ts.snap @@ -278,6 +278,17 @@ Object { "Arn", ], }, + "\\"]},\\"yAxis\\":{}}},{\\"type\\":\\"metric\\",\\"width\\":6,\\"height\\":4,\\"x\\":6,\\"y\\":0,\\"properties\\":{\\"view\\":\\"timeSeries\\",\\"region\\":\\"", + Object { + "Ref": "AWS::Region", + }, + "\\",\\"annotations\\":{\\"alarms\\":[\\"", + Object { + "Fn::GetAtt": Array [ + "ScopeTestDummyLogGroupMaxUsageCountCriticalD4E65812", + "Arn", + ], + }, "\\"]},\\"yAxis\\":{}}}]}", ], ], @@ -299,13 +310,47 @@ Object { Object { "Ref": "AWS::Region", }, - "\\",\\"metrics\\":[[\\"AWS/Logs\\",\\"IncomingLogEvents\\",\\"LogGroupName\\",\\"DummyLogGroup\\",{\\"label\\":\\"Logs\\",\\"stat\\":\\"SampleCount\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"Logs < 20 for 3 datapoints within 15 minutes\\",\\"value\\":20,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{\\"left\\":{\\"min\\":0,\\"label\\":\\"Count\\",\\"showUnits\\":false}}}}]}", + "\\",\\"metrics\\":[[\\"AWS/Logs\\",\\"IncomingLogEvents\\",\\"LogGroupName\\",\\"DummyLogGroup\\",{\\"label\\":\\"Logs\\",\\"stat\\":\\"SampleCount\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"Logs < 20 for 3 datapoints within 15 minutes\\",\\"value\\":20,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"Logs > 9001 for 3 datapoints within 15 minutes\\",\\"value\\":9001,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{\\"left\\":{\\"min\\":0,\\"label\\":\\"Count\\",\\"showUnits\\":false}}}}]}", ], ], }, }, "Type": "AWS::CloudWatch::Dashboard", }, + "ScopeTestDummyLogGroupMaxUsageCountCriticalD4E65812": Object { + "Properties": Object { + "ActionsEnabled": true, + "AlarmDescription": "The count is too high.", + "AlarmName": "Test-DummyLogGroup-Max-Usage-Count-Critical", + "ComparisonOperator": "GreaterThanThreshold", + "DatapointsToAlarm": 3, + "EvaluationPeriods": 3, + "Metrics": Array [ + Object { + "Id": "m1", + "Label": "Logs", + "MetricStat": Object { + "Metric": Object { + "Dimensions": Array [ + Object { + "Name": "LogGroupName", + "Value": "DummyLogGroup", + }, + ], + "MetricName": "IncomingLogEvents", + "Namespace": "AWS/Logs", + }, + "Period": 300, + "Stat": "SampleCount", + }, + "ReturnData": true, + }, + ], + "Threshold": 9001, + "TreatMissingData": "missing", + }, + "Type": "AWS::CloudWatch::Alarm", + }, "ScopeTestDummyLogGroupUsageCountWarningAE5FF03C": Object { "Properties": Object { "ActionsEnabled": true, @@ -350,7 +395,7 @@ Object { Object { "Ref": "AWS::Region", }, - "\\",\\"metrics\\":[[\\"AWS/Logs\\",\\"IncomingLogEvents\\",\\"LogGroupName\\",\\"DummyLogGroup\\",{\\"label\\":\\"Logs\\",\\"stat\\":\\"SampleCount\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"Logs < 20 for 3 datapoints within 15 minutes\\",\\"value\\":20,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{\\"left\\":{\\"min\\":0,\\"label\\":\\"Count\\",\\"showUnits\\":false}}}}]}", + "\\",\\"metrics\\":[[\\"AWS/Logs\\",\\"IncomingLogEvents\\",\\"LogGroupName\\",\\"DummyLogGroup\\",{\\"label\\":\\"Logs\\",\\"stat\\":\\"SampleCount\\"}]],\\"annotations\\":{\\"horizontal\\":[{\\"label\\":\\"Logs < 20 for 3 datapoints within 15 minutes\\",\\"value\\":20,\\"yAxis\\":\\"left\\"},{\\"label\\":\\"Logs > 9001 for 3 datapoints within 15 minutes\\",\\"value\\":9001,\\"yAxis\\":\\"left\\"}]},\\"yAxis\\":{\\"left\\":{\\"min\\":0,\\"label\\":\\"Count\\",\\"showUnits\\":false}}}}]}", ], ], },