From 31bff7ff9469b48eece71e1399a316ef5f1dd22a Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 22 Feb 2024 18:18:14 +0000 Subject: [PATCH] Refactor telemetry configuration and remove unused code --- .../BearerTokenAuthorization.test.ts | 7 +-- CommonServer/Utils/Telemetry.ts | 46 +++++++++---------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/CommonServer/Tests/Middleware/BearerTokenAuthorization.test.ts b/CommonServer/Tests/Middleware/BearerTokenAuthorization.test.ts index 77b15efd36f..656faef231f 100644 --- a/CommonServer/Tests/Middleware/BearerTokenAuthorization.test.ts +++ b/CommonServer/Tests/Middleware/BearerTokenAuthorization.test.ts @@ -28,7 +28,6 @@ describe('BearerTokenAuthorization', () => { it('calls next without arguments if token is valid', () => { const jsonObj: JSONObject = { test: 'test' }; const req: OneUptimeRequest = { - headers: { authorization: `Bearer ${JSONWebToken.signJsonPayload( jsonObj, @@ -47,7 +46,6 @@ describe('BearerTokenAuthorization', () => { }); it('calls next with exception if token is empty', () => { const req: OneUptimeRequest = { - headers: { authorization: '', }, @@ -65,7 +63,6 @@ describe('BearerTokenAuthorization', () => { }); it('calls next with exception if token is invalid', () => { const req: OneUptimeRequest = { - headers: { authorization: 'Bearer ', }, @@ -82,9 +79,7 @@ describe('BearerTokenAuthorization', () => { ); }); it('calls next with exception if token header is not present', () => { - const req: OneUptimeRequest = { - - } as OneUptimeRequest; + const req: OneUptimeRequest = {} as OneUptimeRequest; const res: ExpressResponse = {} as ExpressResponse; const next: jest.Mock = jest.fn(); void BearerTokenAuthorization.isAuthorizedBearerToken( diff --git a/CommonServer/Utils/Telemetry.ts b/CommonServer/Utils/Telemetry.ts index fae8f7c1249..5f35ff4b341 100644 --- a/CommonServer/Utils/Telemetry.ts +++ b/CommonServer/Utils/Telemetry.ts @@ -1,10 +1,7 @@ import * as opentelemetry from '@opentelemetry/sdk-node'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'; import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto'; -import { - ConsoleMetricExporter, - PeriodicExportingMetricReader, -} from '@opentelemetry/sdk-metrics'; +import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'; import Dictionary from 'Common/Types/Dictionary'; import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; @@ -17,10 +14,7 @@ import { SimpleLogRecordProcessor, } from '@opentelemetry/sdk-logs'; import URL from 'Common/Types/API/URL'; -import { - ConsoleSpanExporter, - SpanExporter, -} from '@opentelemetry/sdk-trace-node'; +import { SpanExporter } from '@opentelemetry/sdk-trace-node'; import { Resource } from '@opentelemetry/resources'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { Logger, logs } from '@opentelemetry/api-logs'; @@ -102,7 +96,8 @@ export default class OneUptimeTelemetry { let traceExporter: SpanExporter | undefined = undefined; - let metricReader: PeriodicExportingMetricReader | undefined = undefined; + let metricReader: PeriodicExportingMetricReader | undefined = + undefined; if (this.getOltpTracesEndpoint()) { traceExporter = new OTLPTraceExporter({ @@ -144,27 +139,30 @@ export default class OneUptimeTelemetry { this.logger = logs.getLogger('default'); - const nodeSdkConfiguration: Partial = { - idGenerator: new AWSXRayIdGenerator(), - instrumentations: [ - new HttpInstrumentation(), - new ExpressInstrumentation(), - ], - resource: this.getResource({ - serviceName: data.serviceName, - }), - logRecordProcessor: loggerProvider as any, - }; - - if(traceExporter) { + const nodeSdkConfiguration: Partial = + { + idGenerator: new AWSXRayIdGenerator(), + instrumentations: [ + new HttpInstrumentation(), + new ExpressInstrumentation(), + ], + resource: this.getResource({ + serviceName: data.serviceName, + }), + logRecordProcessor: loggerProvider as any, + }; + + if (traceExporter) { nodeSdkConfiguration.traceExporter = traceExporter; } - if(metricReader) { + if (metricReader) { nodeSdkConfiguration.metricReader = metricReader as any; } - const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK(nodeSdkConfiguration); + const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK( + nodeSdkConfiguration + ); process.on('SIGTERM', () => { sdk.shutdown().finally(() => {