|
| 1 | +const { NodeSDK } = require('@opentelemetry/sdk-node'); |
| 2 | +const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); |
| 3 | +const { Resource } = require('@opentelemetry/resources'); |
| 4 | +const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); |
| 5 | +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); |
| 6 | +const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base'); |
| 7 | +const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api'); |
| 8 | + |
| 9 | +function parseOtlpHeaders(headerValue) { |
| 10 | + if (!headerValue || typeof headerValue !== 'string') { |
| 11 | + return undefined; |
| 12 | + } |
| 13 | + |
| 14 | + return Object.fromEntries( |
| 15 | + headerValue |
| 16 | + .split(',') |
| 17 | + .map((pair) => pair.trim()) |
| 18 | + .filter(Boolean) |
| 19 | + .map((pair) => pair.split('=').map((value) => value.trim())) |
| 20 | + .filter((tuple) => tuple.length === 2) |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +function createTraceExporter() { |
| 25 | + const endpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT || process.env.OTEL_EXPORTER_OTLP_ENDPOINT; |
| 26 | + const headers = parseOtlpHeaders(process.env.OTEL_EXPORTER_OTLP_HEADERS); |
| 27 | + |
| 28 | + if (endpoint) { |
| 29 | + return new OTLPTraceExporter({ url: endpoint, headers }); |
| 30 | + } |
| 31 | + |
| 32 | + return new ConsoleSpanExporter(); |
| 33 | +} |
| 34 | + |
| 35 | +function getDiagLevel() { |
| 36 | + const level = (process.env.OTEL_DIAG_LEVEL || 'error').toUpperCase(); |
| 37 | + return DiagLogLevel[level] || DiagLogLevel.ERROR; |
| 38 | +} |
| 39 | + |
| 40 | +function initTracing(options = {}) { |
| 41 | + if (process.env.OTEL_DISABLED === 'true' || global.__substreamOtelInitialized) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + diag.setLogger(new DiagConsoleLogger(), getDiagLevel()); |
| 46 | + |
| 47 | + const serviceName = process.env.OTEL_SERVICE_NAME || options.serviceName || 'substream-protocol-backend'; |
| 48 | + const serviceVersion = process.env.OTEL_SERVICE_VERSION || options.serviceVersion || '1.0.0'; |
| 49 | + |
| 50 | + const sdk = new NodeSDK({ |
| 51 | + resource: new Resource({ |
| 52 | + [SemanticResourceAttributes.SERVICE_NAME]: serviceName, |
| 53 | + [SemanticResourceAttributes.SERVICE_VERSION]: serviceVersion, |
| 54 | + [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.NODE_ENV || 'development', |
| 55 | + }), |
| 56 | + traceExporter: createTraceExporter(), |
| 57 | + instrumentations: [ |
| 58 | + getNodeAutoInstrumentations({ |
| 59 | + '@opentelemetry/instrumentation-http': { |
| 60 | + ignoreIncomingPaths: process.env.OTEL_IGNORE_PATHS ? process.env.OTEL_IGNORE_PATHS.split(',') : ['/health', '/metrics'], |
| 61 | + }, |
| 62 | + '@opentelemetry/instrumentation-express': { |
| 63 | + requestHook: (span, info) => { |
| 64 | + if (span && info) { |
| 65 | + span.setAttribute('http.route', info.route || info.request?.url || '/'); |
| 66 | + } |
| 67 | + }, |
| 68 | + }, |
| 69 | + }), |
| 70 | + ], |
| 71 | + }); |
| 72 | + |
| 73 | + sdk.start() |
| 74 | + .then(() => { |
| 75 | + console.log('[Tracing] OpenTelemetry initialized', { |
| 76 | + serviceName, |
| 77 | + serviceVersion, |
| 78 | + }); |
| 79 | + }) |
| 80 | + .catch((error) => { |
| 81 | + console.error('[Tracing] Failed to initialize OpenTelemetry', error); |
| 82 | + }); |
| 83 | + |
| 84 | + const shutdown = async () => { |
| 85 | + try { |
| 86 | + await sdk.shutdown(); |
| 87 | + console.log('[Tracing] OpenTelemetry shutdown complete'); |
| 88 | + } catch (error) { |
| 89 | + console.error('[Tracing] OpenTelemetry shutdown failed', error); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + process.on('SIGTERM', shutdown); |
| 94 | + process.on('SIGINT', shutdown); |
| 95 | + |
| 96 | + global.__substreamOtelInitialized = true; |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + initTracing, |
| 101 | +}; |
0 commit comments