Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(cloudwatch): use PutAlarmMuteRule for mute/unmute with duration window#4621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { CloudWatchClient, DisableAlarmActionsCommand } from '@aws-sdk/client-cloudwatch' | ||
| import { CloudWatchClient, PutAlarmMuteRuleCommand } from '@aws-sdk/client-cloudwatch' | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| @@ -9,6 +9,26 @@ import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| const logger = createLogger('CloudWatchMuteAlarm') | ||
| function toAtExpression(date: Date): string { | ||
| const yyyy = date.getUTCFullYear() | ||
| const mm = String(date.getUTCMonth() + 1).padStart(2, '0') | ||
| const dd = String(date.getUTCDate()).padStart(2, '0') | ||
| const hh = String(date.getUTCHours()).padStart(2, '0') | ||
| const min = String(date.getUTCMinutes()).padStart(2, '0') | ||
| return `at(${yyyy}-${mm}-${dd}T${hh}:${min})` | ||
| } | ||
| function toIsoDuration(value: number, unit: 'minutes' | 'hours' | 'days'): string { | ||
| switch (unit) { | ||
| case 'minutes': | ||
| return `PT${value}M` | ||
| case 'hours': | ||
| return `PT${value}H` | ||
| case 'days': | ||
| return `P${value}D` | ||
| } | ||
| } | ||
| export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| const auth = await checkInternalAuth(request) | ||
| @@ -23,7 +43,14 @@ export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| if (!parsed.success) return parsed.response | ||
| const validatedData = parsed.data.body | ||
| logger.info(`Muting ${validatedData.alarmNames.length} CloudWatch alarm(s)`) | ||
| const startDate = | ||
| validatedData.startDate !== undefined ? new Date(validatedData.startDate * 1000) : new Date() | ||
| const expression = toAtExpression(startDate) | ||
| const duration = toIsoDuration(validatedData.durationValue, validatedData.durationUnit) | ||
| logger.info( | ||
| `Creating CloudWatch alarm mute rule "${validatedData.muteRuleName}" for ${validatedData.alarmNames.length} alarm(s) (${expression}, duration ${duration})` | ||
| ) | ||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| @@ -34,19 +61,30 @@ export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| }) | ||
| try { | ||
| const command = new DisableAlarmActionsCommand({ | ||
| AlarmNames: validatedData.alarmNames, | ||
| const command = new PutAlarmMuteRuleCommand({ | ||
| Name: validatedData.muteRuleName, | ||
| ...(validatedData.description && { Description: validatedData.description }), | ||
| Rule: { | ||
| Schedule: { | ||
| Expression: expression, | ||
| Duration: duration, | ||
| }, | ||
| }, | ||
| MuteTargets: { AlarmNames: validatedData.alarmNames }, | ||
| }) | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| await client.send(command) | ||
| logger.info(`Successfully muted ${validatedData.alarmNames.length} alarm(s)`) | ||
| logger.info(`Successfully created mute rule "${validatedData.muteRuleName}"`) | ||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| success: true, | ||
| muteRuleName: validatedData.muteRuleName, | ||
| alarmNames: validatedData.alarmNames, | ||
| expression, | ||
| duration, | ||
| }, | ||
| }) | ||
| } finally { | ||
| @@ -55,7 +93,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => { | ||
| } catch (error) { | ||
| logger.error('MuteAlarm failed', { error: toError(error).message }) | ||
| return NextResponse.json( | ||
| { error: `Failed to mute CloudWatch alarm: ${toError(error).message}` }, | ||
| { error: `Failed to create CloudWatch alarm mute rule: ${toError(error).message}` }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 46 additions & 14 deletions
60 apps/sim/lib/api/contracts/tools/aws/cloudwatch-mute-alarm.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10 apps/sim/lib/api/contracts/tools/aws/cloudwatch-unmute-alarm.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.