From 899422812dcc65cd85dd04e1c25b1ef3c86c28de Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 2 Feb 2024 11:05:34 +0000 Subject: [PATCH] Refactor OTelIngest and TelemetryIngest modules --- Ingestor/API/OTelIngest.ts | 24 +++++++++++++----------- Ingestor/Middleware/TelemetryIngest.ts | 13 +++++++++---- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Ingestor/API/OTelIngest.ts b/Ingestor/API/OTelIngest.ts index 8a019e9d20..6ed33953a8 100644 --- a/Ingestor/API/OTelIngest.ts +++ b/Ingestor/API/OTelIngest.ts @@ -22,7 +22,9 @@ import LogService from 'CommonServer/Services/LogService'; import { JSONArray, JSONObject } from 'Common/Types/JSON'; import OTelIngestService from '../Service/OTelIngest'; import { ProductType } from 'Model/Models/UsageBilling'; -import TelemetryIngest, { TelemetryRequest } from '../Middleware/TelemetryIngest'; +import TelemetryIngest, { + TelemetryRequest, +} from '../Middleware/TelemetryIngest'; // Load proto file for OTel @@ -46,7 +48,6 @@ const MetricsData: protobuf.Type = MetricsProto.lookupType('MetricsData'); const router: ExpressRouter = Express.getRouter(); - /** * * Otel Middleware @@ -54,8 +55,12 @@ const router: ExpressRouter = Express.getRouter(); */ class OpenTelemetryRequestMiddleware { - public static async getProductType (req: ExpressRequest, _res: ExpressResponse, next: NextFunction) { - try{ + public static async getProductType( + req: ExpressRequest, + _res: ExpressResponse, + next: NextFunction + ): Promise { + try { let productType: ProductType; if (req.baseUrl === '/otlp/v1/traces') { @@ -72,18 +77,15 @@ class OpenTelemetryRequestMiddleware { } (req as TelemetryRequest).productType = productType; - - }catch(err){ + } catch (err) { return next(err); } } } - - router.post( '/otlp/v1/traces', - OpenTelemetryRequestMiddleware.getProductType, + OpenTelemetryRequestMiddleware.getProductType, TelemetryIngest.isAuthorizedServiceMiddleware, async ( req: ExpressRequest, @@ -155,7 +157,7 @@ router.post( router.post( '/otlp/v1/metrics', - OpenTelemetryRequestMiddleware.getProductType, + OpenTelemetryRequestMiddleware.getProductType, TelemetryIngest.isAuthorizedServiceMiddleware, async ( req: ExpressRequest, @@ -397,7 +399,7 @@ router.post( router.post( '/otlp/v1/logs', - OpenTelemetryRequestMiddleware.getProductType, + OpenTelemetryRequestMiddleware.getProductType, TelemetryIngest.isAuthorizedServiceMiddleware, async ( req: ExpressRequest, diff --git a/Ingestor/Middleware/TelemetryIngest.ts b/Ingestor/Middleware/TelemetryIngest.ts index cbecb73fd5..bb2d2610d7 100644 --- a/Ingestor/Middleware/TelemetryIngest.ts +++ b/Ingestor/Middleware/TelemetryIngest.ts @@ -1,4 +1,8 @@ -import { ExpressRequest, ExpressResponse, NextFunction } from 'CommonServer/Utils/Express'; +import { + ExpressRequest, + ExpressResponse, + NextFunction, +} from 'CommonServer/Utils/Express'; import { ProbeExpressRequest } from '../Types/Request'; import BadRequestException from 'Common/Types/Exception/BadRequestException'; import GlobalCache from 'CommonServer/Infrastructure/GlobalCache'; @@ -13,7 +17,7 @@ import logger from 'CommonServer/Utils/Logger'; export interface TelemetryRequest extends ExpressRequest { serviceId: ObjectID; // Service ID projectId: ObjectID; // Project ID - productType: ProductType; // what is the product type of the request - logs, metrics or traces. + productType: ProductType; // what is the product type of the request - logs, metrics or traces. } export default class TelemetryIngest { @@ -85,7 +89,8 @@ export default class TelemetryIngest { ); (req as TelemetryRequest).serviceId = service.id as ObjectID; - (req as TelemetryRequest).projectId = service.projectId as ObjectID; + (req as TelemetryRequest).projectId = + service.projectId as ObjectID; } (req as TelemetryRequest).serviceId = ObjectID.fromString( @@ -110,4 +115,4 @@ export default class TelemetryIngest { return next(err); } } -} \ No newline at end of file +}