From 30d9cbe9a3f3c3e22dbbfeb0b12eb8d68f9b3da0 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 20 Feb 2024 13:01:58 +0000 Subject: [PATCH] Refactor OpenTelemetry configuration --- CommonServer/Utils/OpenTelemetry.ts | 56 +++++++++++++++-------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/CommonServer/Utils/OpenTelemetry.ts b/CommonServer/Utils/OpenTelemetry.ts index acff00c082..6fcd49820c 100644 --- a/CommonServer/Utils/OpenTelemetry.ts +++ b/CommonServer/Utils/OpenTelemetry.ts @@ -6,41 +6,45 @@ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; import logger from './Logger'; import Dictionary from 'Common/Types/Dictionary'; -let sdk: opentelemetry.NodeSDK | null = null; +let sdk: opentelemetry.NodeSDK | null = null; if ( - process.env['OTEL_EXPORTER_OTLP_ENDPOINT'] && - process.env['OTEL_EXPORTER_OTLP_HEADERS'] + process.env['OTEL_EXPORTER_OTLP_ENDPOINT'] && + process.env['OTEL_EXPORTER_OTLP_HEADERS'] ) { - const headersStrings: Array = - process.env['OTEL_EXPORTER_OTLP_HEADERS'].split(';'); + const headersStrings: Array = + process.env['OTEL_EXPORTER_OTLP_HEADERS'].split(';'); - const headers: Dictionary = {}; + const headers: Dictionary = {}; - for (const headerString of headersStrings) { - const header: Array = headerString.split('='); - if (header.length === 2) { - headers[header[0]!.toString()] = header[1]!.toString(); - } + for (const headerString of headersStrings) { + const header: Array = headerString.split('='); + if (header.length === 2) { + headers[header[0]!.toString()] = header[1]!.toString(); } + } - sdk = new opentelemetry.NodeSDK({ - traceExporter: new OTLPTraceExporter({ - url: process.env['OTEL_EXPORTER_OTLP_ENDPOINT'], - headers: headers, - }), - metricReader: new PeriodicExportingMetricReader({ - exporter: new OTLPMetricExporter({ - url: process.env['OTEL_EXPORTER_OTLP_ENDPOINT'], - headers: headers, - }), - }) as any, - instrumentations: [getNodeAutoInstrumentations()], - }); + const otlpEndpoint = process.env['OTEL_EXPORTER_OTLP_ENDPOINT']; - sdk.start(); + logger.info(otlpEndpoint); - logger.info('Instrumentation initialized'); + sdk = new opentelemetry.NodeSDK({ + traceExporter: new OTLPTraceExporter({ + url: otlpEndpoint + '/v1/traces', + headers: headers, + }), + metricReader: new PeriodicExportingMetricReader({ + exporter: new OTLPMetricExporter({ + url: otlpEndpoint + '/v1/metrics', + headers: headers, + }), + }) as any, + instrumentations: [getNodeAutoInstrumentations()], + }); + + sdk.start(); + + logger.info('Instrumentation initialized'); } export default sdk;