Skip to content

Commit 307eee7

Browse files
committed
consolidated telemetry events
1 parent fba0b0f commit 307eee7

File tree

31 files changed

+842
-378
lines changed

31 files changed

+842
-378
lines changed

apps/sim/app/api/chat/route.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ export async function POST(request: NextRequest) {
212212

213213
logger.info(`Chat "${title}" deployed successfully at ${chatUrl}`)
214214

215+
try {
216+
const { PlatformEvents } = await import('@/lib/core/telemetry')
217+
PlatformEvents.chatDeployed({
218+
chatId: id,
219+
workflowId,
220+
authType,
221+
hasOutputConfigs: outputConfigs.length > 0,
222+
})
223+
} catch (_e) {
224+
// Silently fail
225+
}
226+
215227
return createSuccessResponse({
216228
id,
217229
chatUrl,

apps/sim/app/api/knowledge/[id]/documents/route.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,14 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
198198
`[${requestId}] Starting controlled async processing of ${createdDocuments.length} documents`
199199
)
200200

201-
// Track bulk document upload
202201
try {
203-
const { trackPlatformEvent } = await import('@/lib/core/telemetry')
204-
trackPlatformEvent('platform.knowledge_base.documents_uploaded', {
205-
'knowledge_base.id': knowledgeBaseId,
206-
'documents.count': createdDocuments.length,
207-
'documents.upload_type': 'bulk',
208-
'processing.chunk_size': validatedData.processingOptions.chunkSize,
209-
'processing.recipe': validatedData.processingOptions.recipe,
202+
const { PlatformEvents } = await import('@/lib/core/telemetry')
203+
PlatformEvents.knowledgeBaseDocumentsUploaded({
204+
knowledgeBaseId,
205+
documentsCount: createdDocuments.length,
206+
uploadType: 'bulk',
207+
chunkSize: validatedData.processingOptions.chunkSize,
208+
recipe: validatedData.processingOptions.recipe,
210209
})
211210
} catch (_e) {
212211
// Silently fail
@@ -262,15 +261,14 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
262261
userId
263262
)
264263

265-
// Track single document upload
266264
try {
267-
const { trackPlatformEvent } = await import('@/lib/core/telemetry')
268-
trackPlatformEvent('platform.knowledge_base.documents_uploaded', {
269-
'knowledge_base.id': knowledgeBaseId,
270-
'documents.count': 1,
271-
'documents.upload_type': 'single',
272-
'document.mime_type': validatedData.mimeType,
273-
'document.file_size': validatedData.fileSize,
265+
const { PlatformEvents } = await import('@/lib/core/telemetry')
266+
PlatformEvents.knowledgeBaseDocumentsUploaded({
267+
knowledgeBaseId,
268+
documentsCount: 1,
269+
uploadType: 'single',
270+
mimeType: validatedData.mimeType,
271+
fileSize: validatedData.fileSize,
274272
})
275273
} catch (_e) {
276274
// Silently fail

apps/sim/app/api/knowledge/[id]/route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createLogger } from '@sim/logger'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { z } from 'zod'
44
import { getSession } from '@/lib/auth'
5+
import { PlatformEvents } from '@/lib/core/telemetry'
56
import { generateRequestId } from '@/lib/core/utils/request'
67
import {
78
deleteKnowledgeBase,
@@ -183,6 +184,14 @@ export async function DELETE(
183184

184185
await deleteKnowledgeBase(id, requestId)
185186

187+
try {
188+
PlatformEvents.knowledgeBaseDeleted({
189+
knowledgeBaseId: id,
190+
})
191+
} catch {
192+
// Telemetry should not fail the operation
193+
}
194+
186195
logger.info(`[${requestId}] Knowledge base deleted: ${id} for user ${session.user.id}`)
187196

188197
return NextResponse.json({

apps/sim/app/api/knowledge/route.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createLogger } from '@sim/logger'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { z } from 'zod'
44
import { getSession } from '@/lib/auth'
5+
import { PlatformEvents } from '@/lib/core/telemetry'
56
import { generateRequestId } from '@/lib/core/utils/request'
67
import { createKnowledgeBase, getKnowledgeBases } from '@/lib/knowledge/service'
78

@@ -94,6 +95,16 @@ export async function POST(req: NextRequest) {
9495

9596
const newKnowledgeBase = await createKnowledgeBase(createData, requestId)
9697

98+
try {
99+
PlatformEvents.knowledgeBaseCreated({
100+
knowledgeBaseId: newKnowledgeBase.id,
101+
name: validatedData.name,
102+
workspaceId: validatedData.workspaceId,
103+
})
104+
} catch {
105+
// Telemetry should not fail the operation
106+
}
107+
97108
logger.info(
98109
`[${requestId}] Knowledge base created: ${newKnowledgeBase.id} for user ${session.user.id}`
99110
)

apps/sim/app/api/knowledge/search/route.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { type NextRequest, NextResponse } from 'next/server'
33
import { z } from 'zod'
4+
import { PlatformEvents } from '@/lib/core/telemetry'
45
import { generateRequestId } from '@/lib/core/utils/request'
56
import { ALL_TAG_SLOTS } from '@/lib/knowledge/constants'
67
import { getDocumentTagDefinitions } from '@/lib/knowledge/tags/service'
@@ -294,6 +295,16 @@ export async function POST(request: NextRequest) {
294295
const documentIds = results.map((result) => result.documentId)
295296
const documentNameMap = await getDocumentNamesByIds(documentIds)
296297

298+
try {
299+
PlatformEvents.knowledgeBaseSearched({
300+
knowledgeBaseId: accessibleKbIds[0],
301+
resultsCount: results.length,
302+
workspaceId: workspaceId || undefined,
303+
})
304+
} catch {
305+
// Telemetry should not fail the operation
306+
}
307+
297308
return NextResponse.json({
298309
success: true,
299310
data: {

apps/sim/app/api/mcp/servers/route.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ export const POST = withMcpAuth('write')(
140140
)
141141

142142
try {
143-
const { trackPlatformEvent } = await import('@/lib/core/telemetry')
144-
trackPlatformEvent('platform.mcp.server_added', {
145-
'mcp.server_id': serverId,
146-
'mcp.server_name': body.name,
147-
'mcp.transport': body.transport,
148-
'workspace.id': workspaceId,
143+
const { PlatformEvents } = await import('@/lib/core/telemetry')
144+
PlatformEvents.mcpServerAdded({
145+
serverId,
146+
serverName: body.name,
147+
transport: body.transport,
148+
workspaceId,
149149
})
150150
} catch (_e) {
151151
// Silently fail

apps/sim/app/api/mcp/tools/execute/route.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ export const POST = withMcpAuth('read')(
194194
logger.info(`[${requestId}] Successfully executed tool ${toolName} on server ${serverId}`)
195195

196196
try {
197-
const { trackPlatformEvent } = await import('@/lib/core/telemetry')
198-
trackPlatformEvent('platform.mcp.tool_executed', {
199-
'mcp.server_id': serverId,
200-
'mcp.tool_name': toolName,
201-
'mcp.execution_status': 'success',
202-
'workspace.id': workspaceId,
197+
const { PlatformEvents } = await import('@/lib/core/telemetry')
198+
PlatformEvents.mcpToolExecuted({
199+
serverId,
200+
toolName,
201+
status: 'success',
202+
workspaceId,
203203
})
204204
} catch {
205205
// Telemetry failure is non-critical

apps/sim/app/api/templates/[id]/use/route.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,15 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
168168
`[${requestId}] Successfully used template: ${id}, created workflow: ${newWorkflowId}`
169169
)
170170

171-
// Track template usage
172171
try {
173-
const { trackPlatformEvent } = await import('@/lib/core/telemetry')
172+
const { PlatformEvents } = await import('@/lib/core/telemetry')
174173
const templateState = templateData.state as any
175-
trackPlatformEvent('platform.template.used', {
176-
'template.id': id,
177-
'template.name': templateData.name,
178-
'workflow.created_id': newWorkflowId,
179-
'workflow.blocks_count': templateState?.blocks
180-
? Object.keys(templateState.blocks).length
181-
: 0,
182-
'workspace.id': workspaceId,
174+
PlatformEvents.templateUsed({
175+
templateId: id,
176+
templateName: templateData.name,
177+
newWorkflowId,
178+
blocksCount: templateState?.blocks ? Object.keys(templateState.blocks).length : 0,
179+
workspaceId,
183180
})
184181
} catch (_e) {
185182
// Silently fail

apps/sim/app/api/webhooks/[id]/route.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { and, eq } from 'drizzle-orm'
55
import { type NextRequest, NextResponse } from 'next/server'
66
import { getSession } from '@/lib/auth'
77
import { validateInteger } from '@/lib/core/security/input-validation'
8+
import { PlatformEvents } from '@/lib/core/telemetry'
89
import { generateRequestId } from '@/lib/core/utils/request'
910
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
1011

@@ -314,6 +315,17 @@ export async function DELETE(
314315
await db.delete(webhook).where(eq(webhook.id, wId))
315316
}
316317

318+
try {
319+
for (const wId of idsToDelete) {
320+
PlatformEvents.webhookDeleted({
321+
webhookId: wId,
322+
workflowId: webhookData.workflow.id,
323+
})
324+
}
325+
} catch {
326+
// Telemetry should not fail the operation
327+
}
328+
317329
logger.info(
318330
`[${requestId}] Successfully deleted ${idsToDelete.length} webhooks for credential set`,
319331
{
@@ -325,6 +337,16 @@ export async function DELETE(
325337
} else {
326338
await cleanupExternalWebhook(foundWebhook, webhookData.workflow, requestId)
327339
await db.delete(webhook).where(eq(webhook.id, id))
340+
341+
try {
342+
PlatformEvents.webhookDeleted({
343+
webhookId: id,
344+
workflowId: webhookData.workflow.id,
345+
})
346+
} catch {
347+
// Telemetry should not fail the operation
348+
}
349+
328350
logger.info(`[${requestId}] Successfully deleted webhook: ${id}`)
329351
}
330352

apps/sim/app/api/webhooks/route.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { and, desc, eq } from 'drizzle-orm'
55
import { nanoid } from 'nanoid'
66
import { type NextRequest, NextResponse } from 'next/server'
77
import { getSession } from '@/lib/auth'
8+
import { PlatformEvents } from '@/lib/core/telemetry'
89
import { generateRequestId } from '@/lib/core/utils/request'
910
import { getBaseUrl } from '@/lib/core/utils/urls'
1011
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
@@ -790,6 +791,19 @@ export async function POST(request: NextRequest) {
790791
}
791792
// --- End Grain specific logic ---
792793

794+
if (!targetWebhookId && savedWebhook) {
795+
try {
796+
PlatformEvents.webhookCreated({
797+
webhookId: savedWebhook.id,
798+
workflowId: workflowId,
799+
provider: provider || 'generic',
800+
workspaceId: workflowRecord.workspaceId || undefined,
801+
})
802+
} catch {
803+
// Telemetry should not fail the operation
804+
}
805+
}
806+
793807
const status = targetWebhookId ? 200 : 201
794808
return NextResponse.json({ webhook: savedWebhook }, { status })
795809
} catch (error: any) {

0 commit comments

Comments
 (0)