From 5dc625c34b18e7e087f54bc0a1c7a62834212045 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 22 Feb 2024 16:54:34 +0000 Subject: [PATCH] Add OpenTelemetry HTTP and Express instrumentations --- CommonServer/Utils/Telemetry.ts | 22 +++++++++++++++++----- Ingestor/API/OTelIngest.ts | 2 ++ config.example.env | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CommonServer/Utils/Telemetry.ts b/CommonServer/Utils/Telemetry.ts index 3d89933315..155191cef7 100644 --- a/CommonServer/Utils/Telemetry.ts +++ b/CommonServer/Utils/Telemetry.ts @@ -7,7 +7,8 @@ import { } from '@opentelemetry/sdk-metrics'; import Dictionary from 'Common/Types/Dictionary'; import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray'; -import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; +import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; +import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'; import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http'; import { BatchLogRecordProcessor, @@ -23,6 +24,7 @@ import { import { Resource } from '@opentelemetry/resources'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { Logger, logs } from '@opentelemetry/api-logs'; +import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base'; export default class OneUptimeTelemetry { public static sdk: opentelemetry.NodeSDK | null = null; @@ -93,7 +95,13 @@ export default class OneUptimeTelemetry { } public static init(data: { serviceName: string }): opentelemetry.NodeSDK { + if (!this.sdk) { + + const headers: Dictionary = this.getHeaders(); + + console.log(headers); + let traceExporter: SpanExporter = new ConsoleSpanExporter(); let metricReader: PeriodicExportingMetricReader = @@ -104,7 +112,7 @@ export default class OneUptimeTelemetry { if (this.getOltpTracesEndpoint()) { traceExporter = new OTLPTraceExporter({ url: this.getOltpTracesEndpoint()!.toString(), - headers: this.getHeaders(), + headers: headers, }); } @@ -112,7 +120,7 @@ export default class OneUptimeTelemetry { metricReader = new PeriodicExportingMetricReader({ exporter: new OTLPMetricExporter({ url: this.getOltpMetricsEndpoint()!.toString(), - headers: this.getHeaders(), + headers: headers, }), }); } @@ -122,7 +130,8 @@ export default class OneUptimeTelemetry { if (this.getOltpLogsEndpoint()) { const logExporter: OTLPLogExporter = new OTLPLogExporter({ url: this.getOltpLogsEndpoint()!.toString(), - headers: this.getHeaders(), + headers: headers, + compression: CompressionAlgorithm.GZIP, }); loggerProvider.addLogRecordProcessor( @@ -143,7 +152,10 @@ export default class OneUptimeTelemetry { traceExporter: traceExporter, metricReader: metricReader as any, logRecordProcessor: loggerProvider as any, - instrumentations: [getNodeAutoInstrumentations()], + instrumentations: [ + new HttpInstrumentation(), + new ExpressInstrumentation() + ], resource: this.getResource({ serviceName: data.serviceName, }), diff --git a/Ingestor/API/OTelIngest.ts b/Ingestor/API/OTelIngest.ts index 67ac716729..35d1a1ff84 100644 --- a/Ingestor/API/OTelIngest.ts +++ b/Ingestor/API/OTelIngest.ts @@ -63,6 +63,8 @@ class OpenTelemetryRequestMiddleware { try { let productType: ProductType; + debugger; + const isProtobuf: boolean = req.body instanceof Uint8Array; if (req.url.includes('/otlp/v1/traces')) { diff --git a/config.example.env b/config.example.env index 55f124d1d0..43287bf472 100644 --- a/config.example.env +++ b/config.example.env @@ -170,7 +170,7 @@ BILLING_PRIVATE_KEY= # Use this when you want to disable incident creation. DISABLE_AUTOMATIC_INCIDENT_CREATION=false -# You can set the env var to http://otel-collector:4317 if you want instrumentation to be sent to otel collector. +# You can set the env var to http://otel-collector:4318 if you want instrumentation to be sent to otel collector. OTEL_EXPORTER_OTLP_ENDPOINT= # You can set the env var to "x-oneuptime-service-token="