Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | /** * OpenTelemetry Configuration * * Initializes distributed tracing and metrics collection using OpenTelemetry. * This should be imported and initialized at application startup. * * NOTE: This file is server-only due to Node.js dependencies. * Import directly from './otel' in server-side code only. */ import 'server-only'; import { NodeSDK } from '@opentelemetry/sdk-node'; import { logger } from '@/lib/logging'; import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'; import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; import { resourceFromAttributes } from '@opentelemetry/resources'; import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION, } from '@opentelemetry/semantic-conventions'; // Configuration constants const SERVICE_NAME = 'elite-events'; const SERVICE_VERSION = process.env.npm_package_version || '1.0.0'; const ENVIRONMENT = process.env.NODE_ENV || 'development'; const OTEL_ENABLED = process.env.OTEL_ENABLED === 'true'; const OTEL_ENDPOINT = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318'; const OTEL_API_KEY = process.env.OTEL_API_KEY; // Create resource with service information const resource = resourceFromAttributes({ [ATTR_SERVICE_NAME]: SERVICE_NAME, [ATTR_SERVICE_VERSION]: SERVICE_VERSION, 'deployment.environment': ENVIRONMENT, 'host.name': process.env.HOSTNAME || 'unknown', }); // Headers for authentication (if API key is provided) const headers: Record<string, string> = OTEL_API_KEY ? { Authorization: `Bearer ${OTEL_API_KEY}` } : {}; // Create trace exporter const traceExporter = new OTLPTraceExporter({ url: `${OTEL_ENDPOINT}/v1/traces`, headers, }); // Create metric exporter const metricExporter = new OTLPMetricExporter({ url: `${OTEL_ENDPOINT}/v1/metrics`, headers, }); // Create the SDK instance let sdk: NodeSDK | null = null; /** * Initialize OpenTelemetry SDK * * This should be called once at application startup, before any other code runs. */ export function initTelemetry(): void { if (!OTEL_ENABLED) { // OpenTelemetry disabled via configuration return; } if (sdk) { // OpenTelemetry already initialized return; } try { sdk = new NodeSDK({ resource, traceExporter, metricReader: new PeriodicExportingMetricReader({ exporter: metricExporter, exportIntervalMillis: 60000, // Export metrics every minute }), instrumentations: [ getNodeAutoInstrumentations({ // HTTP instrumentation configuration '@opentelemetry/instrumentation-http': { requestHook: (span, request) => { // Add custom attributes from request headers const requestId = (request as { headers?: Record<string, string | string[]> }).headers?.['x-request-id']; if (requestId) { span.setAttribute('http.request_id', String(requestId)); } }, ignoreIncomingRequestHook: (request) => { const url = request.url || ''; // Ignore health check endpoints if (/^\/api\/health/.test(url)) return true; // Ignore static assets if (/^\/_next\//.test(url)) return true; if (/^\/favicon\.ico/.test(url)) return true; return false; }, }, // Disable noisy file system instrumentation '@opentelemetry/instrumentation-fs': { enabled: false, }, // DNS instrumentation '@opentelemetry/instrumentation-dns': { enabled: false, // Usually not needed }, }), ], }); sdk.start(); // Graceful shutdown const shutdown = async () => { try { await sdk?.shutdown(); } catch { // Shutdown error - silently handled } }; process.on('SIGTERM', shutdown); process.on('SIGINT', shutdown); } catch (error) { logger.error('OpenTelemetry initialization failed', error instanceof Error ? error : new Error(String(error)), { category: 'SYSTEM' }); } } /** * Shutdown OpenTelemetry SDK * * Call this when the application is shutting down to ensure all data is flushed. */ export async function shutdownTelemetry(): Promise<void> { if (sdk) { await sdk.shutdown(); sdk = null; } } /** * Check if OpenTelemetry is enabled and initialized */ export function isTelemetryEnabled(): boolean { return OTEL_ENABLED && sdk !== null; } /** * Get telemetry configuration info (for debugging) */ export function getTelemetryInfo(): { enabled: boolean; serviceName: string; version: string; environment: string; endpoint: string; } { return { enabled: OTEL_ENABLED, serviceName: SERVICE_NAME, version: SERVICE_VERSION, environment: ENVIRONMENT, endpoint: OTEL_ENDPOINT, }; } |