From d6b9882c64bbd74272e9b99e75a50ecfbe962abf Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Fri, 18 Oct 2024 11:31:26 -0400 Subject: [PATCH] Use pagination for alarm history --- src/alarm-slack-notifications/builder.mjs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/alarm-slack-notifications/builder.mjs b/src/alarm-slack-notifications/builder.mjs index bd9cd54..59b0caf 100644 --- a/src/alarm-slack-notifications/builder.mjs +++ b/src/alarm-slack-notifications/builder.mjs @@ -5,8 +5,8 @@ import { STSClient, AssumeRoleCommand } from "@aws-sdk/client-sts"; import { CloudWatchClient, - DescribeAlarmHistoryCommand, DescribeAlarmsCommand, + paginateDescribeAlarmHistory, } from "@aws-sdk/client-cloudwatch"; import { ConfiguredRetryStrategy } from "@aws-sdk/util-retry"; import { alarmConsoleUrl } from "./urls.mjs"; @@ -123,18 +123,28 @@ export async function blocks(event) { AlarmNames: [event.detail.alarmName], }), ); + // Fetch all state transitions from the last 24 hours + const history = { AlarmHistoryItems: [] }; const historyStart = new Date(); historyStart.setUTCHours(-24); - const history = await cloudwatch.send( - new DescribeAlarmHistoryCommand({ + const paginator = paginateDescribeAlarmHistory( + { + client: cloudwatch, + }, + { AlarmName: event.detail.alarmName, HistoryItemType: "StateUpdate", StartDate: historyStart, EndDate: new Date(), - MaxRecords: 100, - }), + }, ); + + // eslint-disable-next-line no-restricted-syntax, no-await-in-loop + for await (const page of paginator) { + history.AlarmHistoryItems.push(...page.AlarmHistoryItems); + } + // Linked title block blox.push({ type: "section",