-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(block): Add cloudwatch publish operation #4027
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
044612d
feat(block): Add cloudwatch publish operation
f8cbbe7
fix(integrations): validate and fix cloudwatch, cloudformation, athen…
waleedlatif1 5aa5347
fix(cloudwatch): complete put_metric_data unit dropdown, add missing …
waleedlatif1 0aecc0f
fix(cloudwatch): fix DescribeAlarms returning only MetricAlarm when "…
waleedlatif1 66ffcd9
fix(cloudwatch): validate dimensions JSON at Zod schema level
waleedlatif1 1118e4f
fix(cloudwatch): reject non-numeric metricValue instead of silently p…
waleedlatif1 9a4d394
fix(cloudwatch): use Number.isFinite to also reject Infinity in block…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(block): Add cloudwatch publish operation
- Loading branch information
commit 044612def2edd78630130fe4ce131ebba33ba154
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
apps/sim/app/api/tools/cloudwatch/put-metric-data/route.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { | ||
| CloudWatchClient, | ||
| PutMetricDataCommand, | ||
| type StandardUnit, | ||
| } from '@aws-sdk/client-cloudwatch' | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { checkInternalAuth } from '@/lib/auth/hybrid' | ||
|
|
||
| const logger = createLogger('CloudWatchPutMetricData') | ||
|
|
||
| const VALID_UNITS = [ | ||
| 'Seconds', | ||
| 'Microseconds', | ||
| 'Milliseconds', | ||
| 'Bytes', | ||
| 'Kilobytes', | ||
| 'Megabytes', | ||
| 'Gigabytes', | ||
| 'Terabytes', | ||
| 'Bits', | ||
| 'Kilobits', | ||
| 'Megabits', | ||
| 'Gigabits', | ||
| 'Terabits', | ||
| 'Percent', | ||
| 'Count', | ||
| 'Bytes/Second', | ||
| 'Kilobytes/Second', | ||
| 'Megabytes/Second', | ||
| 'Gigabytes/Second', | ||
| 'Terabytes/Second', | ||
| 'Bits/Second', | ||
| 'Kilobits/Second', | ||
| 'Megabits/Second', | ||
| 'Gigabits/Second', | ||
| 'Terabits/Second', | ||
| 'Count/Second', | ||
| 'None', | ||
| ] as const | ||
|
|
||
| const PutMetricDataSchema = z.object({ | ||
| region: z.string().min(1, 'AWS region is required'), | ||
| accessKeyId: z.string().min(1, 'AWS access key ID is required'), | ||
| secretAccessKey: z.string().min(1, 'AWS secret access key is required'), | ||
| namespace: z.string().min(1, 'Namespace is required'), | ||
| metricName: z.string().min(1, 'Metric name is required'), | ||
| value: z.number({ coerce: true }), | ||
| unit: z.enum(VALID_UNITS).optional(), | ||
| dimensions: z.string().optional(), | ||
| }) | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const auth = await checkInternalAuth(request) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const body = await request.json() | ||
| const validatedData = PutMetricDataSchema.parse(body) | ||
|
|
||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| credentials: { | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }, | ||
| }) | ||
|
|
||
| const timestamp = new Date() | ||
|
|
||
| const dimensions: { Name: string; Value: string }[] = [] | ||
| if (validatedData.dimensions) { | ||
| const parsed = JSON.parse(validatedData.dimensions) | ||
| if (typeof parsed === 'object' && parsed !== null) { | ||
| for (const [name, value] of Object.entries(parsed)) { | ||
| dimensions.push({ Name: name, Value: String(value) }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const command = new PutMetricDataCommand({ | ||
| Namespace: validatedData.namespace, | ||
| MetricData: [ | ||
| { | ||
| MetricName: validatedData.metricName, | ||
| Value: validatedData.value, | ||
| Timestamp: timestamp, | ||
| ...(validatedData.unit && { Unit: validatedData.unit as StandardUnit }), | ||
| ...(dimensions.length > 0 && { Dimensions: dimensions }), | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| await client.send(command) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| success: true, | ||
| namespace: validatedData.namespace, | ||
| metricName: validatedData.metricName, | ||
| value: validatedData.value, | ||
| unit: validatedData.unit ?? 'None', | ||
| timestamp: timestamp.toISOString(), | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to publish CloudWatch metric' | ||
| logger.error('PutMetricData failed', { error: errorMessage }) | ||
| return NextResponse.json({ error: errorMessage }, { 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
Oops, something went wrong.
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.