-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(block): Add cloudwatch block #3911
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eaca4fb
feat(block): add cloudwatch integration
c5b2f64
Merge branch 'staging' into feat/cloudwatch-block
1ec6f83
Fix bun lock
4e75c47
Add logger, use execution timeout
b043a87
Switch metric dimensions to map style input
925f8b0
Fix attribute names for dimension map
68d9174
Fix import styling
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
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
apps/sim/app/api/tools/cloudwatch/describe-alarms/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,96 @@ | ||
| import { | ||
| type AlarmType, | ||
| CloudWatchClient, | ||
| DescribeAlarmsCommand, | ||
| type StateValue, | ||
| } 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('CloudWatchDescribeAlarms') | ||
|
|
||
| const DescribeAlarmsSchema = 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'), | ||
| alarmNamePrefix: z.string().optional(), | ||
| stateValue: z.preprocess( | ||
| (v) => (v === '' ? undefined : v), | ||
| z.enum(['OK', 'ALARM', 'INSUFFICIENT_DATA']).optional() | ||
| ), | ||
| alarmType: z.preprocess( | ||
| (v) => (v === '' ? undefined : v), | ||
| z.enum(['MetricAlarm', 'CompositeAlarm']).optional() | ||
| ), | ||
| limit: z.preprocess( | ||
| (v) => (v === '' || v === undefined || v === null ? undefined : v), | ||
| z.number({ coerce: true }).int().positive().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 = DescribeAlarmsSchema.parse(body) | ||
|
|
||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| credentials: { | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }, | ||
| }) | ||
|
|
||
| const command = new DescribeAlarmsCommand({ | ||
| ...(validatedData.alarmNamePrefix && { AlarmNamePrefix: validatedData.alarmNamePrefix }), | ||
| ...(validatedData.stateValue && { StateValue: validatedData.stateValue as StateValue }), | ||
| ...(validatedData.alarmType && { AlarmTypes: [validatedData.alarmType as AlarmType] }), | ||
| ...(validatedData.limit !== undefined && { MaxRecords: validatedData.limit }), | ||
| }) | ||
|
|
||
| const response = await client.send(command) | ||
|
|
||
| const metricAlarms = (response.MetricAlarms ?? []).map((a) => ({ | ||
| alarmName: a.AlarmName ?? '', | ||
| alarmArn: a.AlarmArn ?? '', | ||
| stateValue: a.StateValue ?? 'UNKNOWN', | ||
| stateReason: a.StateReason ?? '', | ||
| metricName: a.MetricName, | ||
| namespace: a.Namespace, | ||
| comparisonOperator: a.ComparisonOperator, | ||
| threshold: a.Threshold, | ||
| evaluationPeriods: a.EvaluationPeriods, | ||
| stateUpdatedTimestamp: a.StateUpdatedTimestamp?.getTime(), | ||
| })) | ||
|
|
||
| const compositeAlarms = (response.CompositeAlarms ?? []).map((a) => ({ | ||
| alarmName: a.AlarmName ?? '', | ||
| alarmArn: a.AlarmArn ?? '', | ||
| stateValue: a.StateValue ?? 'UNKNOWN', | ||
| stateReason: a.StateReason ?? '', | ||
| metricName: undefined, | ||
| namespace: undefined, | ||
| comparisonOperator: undefined, | ||
| threshold: undefined, | ||
| evaluationPeriods: undefined, | ||
| stateUpdatedTimestamp: a.StateUpdatedTimestamp?.getTime(), | ||
| })) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { alarms: [...metricAlarms, ...compositeAlarms] }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to describe CloudWatch alarms' | ||
| logger.error('DescribeAlarms failed', { error: errorMessage }) | ||
| return NextResponse.json({ error: errorMessage }, { status: 500 }) | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
apps/sim/app/api/tools/cloudwatch/describe-log-groups/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,62 @@ | ||
| import { DescribeLogGroupsCommand } from '@aws-sdk/client-cloudwatch-logs' | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' | ||
| import { createCloudWatchLogsClient } from '@/app/api/tools/cloudwatch/utils' | ||
|
|
||
| const logger = createLogger('CloudWatchDescribeLogGroups') | ||
|
|
||
| const DescribeLogGroupsSchema = 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'), | ||
| prefix: z.string().optional(), | ||
| limit: z.preprocess( | ||
| (v) => (v === '' || v === undefined || v === null ? undefined : v), | ||
| z.number({ coerce: true }).int().positive().optional() | ||
| ), | ||
| }) | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const body = await request.json() | ||
| const validatedData = DescribeLogGroupsSchema.parse(body) | ||
|
|
||
| const client = createCloudWatchLogsClient({ | ||
| region: validatedData.region, | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }) | ||
TheodoreSpeaks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const command = new DescribeLogGroupsCommand({ | ||
| ...(validatedData.prefix && { logGroupNamePrefix: validatedData.prefix }), | ||
| ...(validatedData.limit !== undefined && { limit: validatedData.limit }), | ||
| }) | ||
|
|
||
| const response = await client.send(command) | ||
|
|
||
| const logGroups = (response.logGroups ?? []).map((lg) => ({ | ||
| logGroupName: lg.logGroupName ?? '', | ||
| arn: lg.arn ?? '', | ||
| storedBytes: lg.storedBytes ?? 0, | ||
| retentionInDays: lg.retentionInDays, | ||
| creationTime: lg.creationTime, | ||
| })) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { logGroups }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to describe CloudWatch log groups' | ||
| logger.error('DescribeLogGroups failed', { error: errorMessage }) | ||
| return NextResponse.json({ error: errorMessage }, { status: 500 }) | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
apps/sim/app/api/tools/cloudwatch/describe-log-streams/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,53 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' | ||
|
|
||
| const logger = createLogger('CloudWatchDescribeLogStreams') | ||
|
|
||
| import { createCloudWatchLogsClient, describeLogStreams } from '@/app/api/tools/cloudwatch/utils' | ||
|
|
||
| const DescribeLogStreamsSchema = 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'), | ||
| logGroupName: z.string().min(1, 'Log group name is required'), | ||
| prefix: z.string().optional(), | ||
| limit: z.preprocess( | ||
| (v) => (v === '' || v === undefined || v === null ? undefined : v), | ||
| z.number({ coerce: true }).int().positive().optional() | ||
| ), | ||
| }) | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| const auth = await checkSessionOrInternalAuth(request) | ||
| if (!auth.success || !auth.userId) { | ||
| return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const body = await request.json() | ||
| const validatedData = DescribeLogStreamsSchema.parse(body) | ||
|
|
||
| const client = createCloudWatchLogsClient({ | ||
| region: validatedData.region, | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }) | ||
|
|
||
| const result = await describeLogStreams(client, validatedData.logGroupName, { | ||
| prefix: validatedData.prefix, | ||
| limit: validatedData.limit, | ||
| }) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { logStreams: result.logStreams }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to describe CloudWatch log streams' | ||
| logger.error('DescribeLogStreams 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 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('CloudWatchGetLogEvents') | ||
|
|
||
| import { createCloudWatchLogsClient, getLogEvents } from '@/app/api/tools/cloudwatch/utils' | ||
|
|
||
| const GetLogEventsSchema = 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'), | ||
| logGroupName: z.string().min(1, 'Log group name is required'), | ||
| logStreamName: z.string().min(1, 'Log stream name is required'), | ||
| startTime: z.number({ coerce: true }).int().optional(), | ||
| endTime: z.number({ coerce: true }).int().optional(), | ||
| limit: z.preprocess( | ||
| (v) => (v === '' || v === undefined || v === null ? undefined : v), | ||
| z.number({ coerce: true }).int().positive().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 = GetLogEventsSchema.parse(body) | ||
|
|
||
| const client = createCloudWatchLogsClient({ | ||
| region: validatedData.region, | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }) | ||
|
|
||
| const result = await getLogEvents( | ||
| client, | ||
| validatedData.logGroupName, | ||
| validatedData.logStreamName, | ||
| { | ||
| startTime: validatedData.startTime, | ||
| endTime: validatedData.endTime, | ||
| limit: validatedData.limit, | ||
| } | ||
| ) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { events: result.events }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to get CloudWatch log events' | ||
| logger.error('GetLogEvents failed', { error: errorMessage }) | ||
| return NextResponse.json({ error: errorMessage }, { status: 500 }) | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
apps/sim/app/api/tools/cloudwatch/get-metric-statistics/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,97 @@ | ||
| import { CloudWatchClient, GetMetricStatisticsCommand } 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('CloudWatchGetMetricStatistics') | ||
|
|
||
| const GetMetricStatisticsSchema = 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'), | ||
| startTime: z.number({ coerce: true }).int(), | ||
| endTime: z.number({ coerce: true }).int(), | ||
| period: z.number({ coerce: true }).int().min(1), | ||
| statistics: z.array(z.enum(['Average', 'Sum', 'Minimum', 'Maximum', 'SampleCount'])).min(1), | ||
| 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 = GetMetricStatisticsSchema.parse(body) | ||
|
|
||
| const client = new CloudWatchClient({ | ||
| region: validatedData.region, | ||
| credentials: { | ||
| accessKeyId: validatedData.accessKeyId, | ||
| secretAccessKey: validatedData.secretAccessKey, | ||
| }, | ||
| }) | ||
|
|
||
| let parsedDimensions: { Name: string; Value: string }[] | undefined | ||
| if (validatedData.dimensions) { | ||
| try { | ||
| const dims = JSON.parse(validatedData.dimensions) | ||
| if (Array.isArray(dims)) { | ||
| parsedDimensions = dims.map((d: Record<string, string>) => ({ | ||
| Name: d.name, | ||
| Value: d.value, | ||
| })) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (typeof dims === 'object') { | ||
| parsedDimensions = Object.entries(dims).map(([name, value]) => ({ | ||
| Name: name, | ||
| Value: String(value), | ||
| })) | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } catch { | ||
| throw new Error('Invalid dimensions JSON') | ||
| } | ||
| } | ||
|
|
||
| const command = new GetMetricStatisticsCommand({ | ||
| Namespace: validatedData.namespace, | ||
| MetricName: validatedData.metricName, | ||
| StartTime: new Date(validatedData.startTime * 1000), | ||
| EndTime: new Date(validatedData.endTime * 1000), | ||
| Period: validatedData.period, | ||
| Statistics: validatedData.statistics, | ||
| ...(parsedDimensions && { Dimensions: parsedDimensions }), | ||
| }) | ||
|
|
||
| const response = await client.send(command) | ||
|
|
||
| const datapoints = (response.Datapoints ?? []) | ||
| .sort((a, b) => (a.Timestamp?.getTime() ?? 0) - (b.Timestamp?.getTime() ?? 0)) | ||
| .map((dp) => ({ | ||
| timestamp: dp.Timestamp ? Math.floor(dp.Timestamp.getTime() / 1000) : 0, | ||
| average: dp.Average, | ||
| sum: dp.Sum, | ||
| minimum: dp.Minimum, | ||
| maximum: dp.Maximum, | ||
| sampleCount: dp.SampleCount, | ||
| unit: dp.Unit, | ||
| })) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| output: { | ||
| label: response.Label ?? validatedData.metricName, | ||
| datapoints, | ||
| }, | ||
| }) | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Failed to get CloudWatch metric statistics' | ||
| logger.error('GetMetricStatistics failed', { error: errorMessage }) | ||
| return NextResponse.json({ error: errorMessage }, { status: 500 }) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.