Skip to content

Commit c155996

Browse files
committed
fix: prevent OTel timeout from killing dev server
- Add DISABLE_TELEMETRY env var to skip all OTel initialization - Reduce export timeout from 30s to 10s for faster failure - Wrap sdk.shutdown() with 5s timeout to prevent blocking exit - Change process.on to process.once for SIGTERM/SIGINT handlers to prevent duplicate execution during HMR reloads
1 parent 4eee650 commit c155996

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

‎apps/sim/instrumentation-node.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DEFAULT_TELEMETRY_CONFIG = {
3535
maxQueueSize: 2048,
3636
maxExportBatchSize: 512,
3737
scheduledDelayMillis: 5000,
38-
exportTimeoutMillis: 30000,
38+
exportTimeoutMillis: 10000,
3939
},
4040
}
4141

@@ -151,8 +151,12 @@ class MothershipOriginSpanProcessor implements SpanProcessor {
151151

152152
async function initializeOpenTelemetry() {
153153
try {
154-
if (env.NEXT_TELEMETRY_DISABLED === '1' || process.env.NEXT_TELEMETRY_DISABLED === '1') {
155-
logger.info('OpenTelemetry disabled via NEXT_TELEMETRY_DISABLED=1')
154+
if (
155+
process.env.DISABLE_TELEMETRY === '1' ||
156+
env.NEXT_TELEMETRY_DISABLED === '1' ||
157+
process.env.NEXT_TELEMETRY_DISABLED === '1'
158+
) {
159+
logger.info('OpenTelemetry disabled via env var')
156160
return
157161
}
158162

@@ -359,15 +363,19 @@ async function initializeOpenTelemetry() {
359363

360364
const shutdownOtel = async () => {
361365
try {
362-
await sdk.shutdown()
366+
const shutdownPromise = sdk.shutdown()
367+
const timeoutPromise = new Promise<void>((_, reject) =>
368+
setTimeout(() => reject(new Error('OTel shutdown timed out')), 5000)
369+
)
370+
await Promise.race([shutdownPromise, timeoutPromise])
363371
logger.info('OpenTelemetry SDK shut down successfully')
364372
} catch (err) {
365373
logger.error('Error shutting down OpenTelemetry SDK', err)
366374
}
367375
}
368376

369-
process.on('SIGTERM', shutdownOtel)
370-
process.on('SIGINT', shutdownOtel)
377+
process.once('SIGTERM', shutdownOtel)
378+
process.once('SIGINT', shutdownOtel)
371379

372380
logger.info('OpenTelemetry instrumentation initialized', {
373381
serviceName: telemetryConfig.serviceName,
@@ -398,8 +406,8 @@ export async function register() {
398406
}
399407
}
400408

401-
process.on('SIGTERM', shutdownPostHog)
402-
process.on('SIGINT', shutdownPostHog)
409+
process.once('SIGTERM', shutdownPostHog)
410+
process.once('SIGINT', shutdownPostHog)
403411

404412
const { startMemoryTelemetry } = await import('./lib/monitoring/memory-telemetry')
405413
startMemoryTelemetry()

0 commit comments

Comments
 (0)