Refactor OTelIngest and TelemetryIngest modules

This commit is contained in:
Simon Larsen 2024-02-02 11:05:34 +00:00
parent e8e735d8d2
commit 899422812d
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
2 changed files with 22 additions and 15 deletions

View File

@ -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<void> {
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,

View File

@ -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);
}
}
}
}