-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat(otel): added otel, persist settings to db, present user with the telemetry preferences & add privacy tab to settings #318
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
5 commits
Select commit
Hold shift + click to select a range
43e2881
feat(otel): added otel, persist settings to db, present user with the…
waleedlatif1 a52c4be
updated telemetry endpoint
waleedlatif1 4d83f96
add protected subdomains for chat deploy
waleedlatif1 a1137a7
removed unused dependencies
waleedlatif1 5b358ae
add execution telemetry logs for workflow-level and block-level logs,…
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
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
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
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,209 @@ | ||
| import { NextRequest, NextResponse } from 'next/server' | ||
| import { createLogger } from '@/lib/logs/console-logger' | ||
|
|
||
| const logger = createLogger('TelemetryAPI') | ||
|
|
||
| const ALLOWED_CATEGORIES = [ | ||
| 'page_view', | ||
| 'feature_usage', | ||
| 'performance', | ||
| 'error', | ||
| 'workflow', | ||
| 'consent', | ||
| ] | ||
|
|
||
| const DEFAULT_TIMEOUT = 5000 // 5 seconds timeout | ||
|
|
||
| /** | ||
| * Validates telemetry data to ensure it doesn't contain sensitive information | ||
| */ | ||
| function validateTelemetryData(data: any): boolean { | ||
| if (!data || typeof data !== 'object') { | ||
| return false | ||
| } | ||
|
|
||
| if (!data.category || !data.action) { | ||
| return false | ||
| } | ||
|
|
||
| if (!ALLOWED_CATEGORIES.includes(data.category)) { | ||
| return false | ||
| } | ||
|
|
||
| const jsonStr = JSON.stringify(data).toLowerCase() | ||
| const sensitivePatterns = [ | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| /password/, | ||
| /token/, | ||
| /secret/, | ||
| /key/, | ||
| /auth/, | ||
| /credential/, | ||
| /private/, | ||
| ] | ||
|
|
||
| return !sensitivePatterns.some(pattern => pattern.test(jsonStr)) | ||
| } | ||
|
|
||
| /** | ||
| * Safely converts a value to string, handling undefined and null values | ||
| */ | ||
| function safeStringValue(value: any): string { | ||
| if (value === undefined || value === null) { | ||
| return '' | ||
| } | ||
|
|
||
| try { | ||
| return String(value) | ||
| } catch (e) { | ||
| return '' | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a safe attribute object for OpenTelemetry | ||
| */ | ||
| function createSafeAttributes(data: Record<string, any>): Array<{key: string, value: {stringValue: string}}> { | ||
| if (!data || typeof data !== 'object') { | ||
| return [] | ||
| } | ||
|
|
||
| const attributes: Array<{key: string, value: {stringValue: string}}> = [] | ||
|
|
||
| Object.entries(data).forEach(([key, value]) => { | ||
| if (value !== undefined && value !== null && key) { | ||
| attributes.push({ | ||
| key, | ||
| value: { stringValue: safeStringValue(value) } | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| return attributes | ||
| } | ||
|
|
||
| /** | ||
| * Forwards telemetry data to OpenTelemetry collector | ||
| */ | ||
| async function forwardToCollector(data: any): Promise<boolean> { | ||
| if (!data || typeof data !== 'object') { | ||
| logger.error('Invalid telemetry data format') | ||
| return false | ||
| } | ||
|
|
||
| const endpoint = process.env.TELEMETRY_ENDPOINT || 'https://telemetry.simstudio.ai/v1/traces' | ||
| const timeout = parseInt(process.env.TELEMETRY_TIMEOUT || '') || DEFAULT_TIMEOUT | ||
|
|
||
| try { | ||
| const timestamp = Date.now() * 1000000 | ||
|
|
||
| const safeAttrs = createSafeAttributes(data) | ||
|
|
||
| const serviceAttrs = [ | ||
| { key: 'service.name', value: { stringValue: 'sim-studio' } }, | ||
| { key: 'service.version', value: { stringValue: process.env.NEXT_PUBLIC_APP_VERSION || '0.1.0' } }, | ||
| { key: 'deployment.environment', value: { stringValue: process.env.NODE_ENV || 'production' } } | ||
| ] | ||
|
|
||
| const spanName = data.category && data.action ? `${data.category}.${data.action}` : 'telemetry.event' | ||
|
|
||
| const payload = { | ||
| resourceSpans: [{ | ||
| resource: { | ||
| attributes: serviceAttrs | ||
| }, | ||
| instrumentationLibrarySpans: [{ | ||
| spans: [{ | ||
| name: spanName, | ||
| kind: 1, | ||
| startTimeUnixNano: timestamp, | ||
| endTimeUnixNano: timestamp + 1000000, | ||
| attributes: safeAttrs | ||
| }] | ||
| }] | ||
| }] | ||
| } | ||
|
|
||
| // Safe debug log of the payload structure without sensitive data | ||
| logger.debug('Preparing to send telemetry payload', { | ||
| endpoint, | ||
| hasAttributes: safeAttrs.length > 0, | ||
| attributeCount: safeAttrs.length | ||
| }) | ||
|
|
||
| // Create explicit AbortController for timeout | ||
| const controller = new AbortController() | ||
| const timeoutId = setTimeout(() => controller.abort(), timeout) | ||
|
|
||
| try { | ||
| const options = { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json' | ||
| }, | ||
| body: JSON.stringify(payload), | ||
| signal: controller.signal | ||
| } | ||
|
|
||
| const response = await fetch(endpoint, options) | ||
| clearTimeout(timeoutId) | ||
|
|
||
| if (!response.ok) { | ||
| logger.error('Telemetry collector returned error', { | ||
| status: response.status, | ||
| statusText: response.statusText | ||
| }) | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } catch (fetchError) { | ||
| clearTimeout(timeoutId) | ||
| if (fetchError instanceof Error && fetchError.name === 'AbortError') { | ||
| logger.error('Telemetry request timed out', { endpoint }) | ||
| } else { | ||
| logger.error('Failed to send telemetry to collector', fetchError) | ||
| } | ||
| return false | ||
| } | ||
| } catch (error) { | ||
| logger.error('Error preparing telemetry payload', error) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Endpoint that receives telemetry events and forwards them to OpenTelemetry collector | ||
| */ | ||
| export async function POST(req: NextRequest) { | ||
| try { | ||
| let eventData | ||
| try { | ||
| eventData = await req.json() | ||
| } catch (parseError) { | ||
| return NextResponse.json( | ||
| { error: 'Invalid JSON in request body' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| if (!validateTelemetryData(eventData)) { | ||
| return NextResponse.json( | ||
| { error: 'Invalid telemetry data format or contains sensitive information' }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const forwarded = await forwardToCollector(eventData) | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| forwarded | ||
| }) | ||
| } catch (error) { | ||
| logger.error('Error processing telemetry event', error) | ||
| return NextResponse.json( | ||
| { error: 'Failed to process telemetry event' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } | ||
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.