From b5deefe31906a3b0ce09c034c6450babc79ec0c7 Mon Sep 17 00:00:00 2001 From: Abhimanyu Babbar Date: Wed, 22 Nov 2023 18:49:53 +0530 Subject: [PATCH] chore: added base support for raising event per tracking plan event --- src/controllers/trackingPlan.ts | 2 +- src/services/trackingPlan.ts | 100 +++++++++++++------------------- 2 files changed, 41 insertions(+), 61 deletions(-) diff --git a/src/controllers/trackingPlan.ts b/src/controllers/trackingPlan.ts index 74e47e0ec9..f96be6a5f7 100644 --- a/src/controllers/trackingPlan.ts +++ b/src/controllers/trackingPlan.ts @@ -7,7 +7,7 @@ export class TrackingPlanController { const events = ctx.request.body; const requestSize = Number(ctx.request.get('content-length')); const reqParams = ctx.request.query; - const response = await TrackingPlanservice.validateTrackingPlan(events, requestSize, reqParams); + const response = TrackingPlanservice.validate(events, requestSize, reqParams); ctx.body = response.body; ControllerUtility.postProcess(ctx, response.status); return ctx; diff --git a/src/services/trackingPlan.ts b/src/services/trackingPlan.ts index 2e68df55e9..89544cd9c8 100644 --- a/src/services/trackingPlan.ts +++ b/src/services/trackingPlan.ts @@ -3,83 +3,63 @@ import { RetryRequestError, RespStatusError, constructValidationErrors } from '. import { getMetadata } from '../v0/util'; import eventValidator from '../util/eventValidation'; import stats from '../util/stats'; +import { HTTP_STATUS_CODES } from '../v0/util/constant'; export class TrackingPlanservice { - public static async validateTrackingPlan(events, requestSize, reqParams) { - const requestStartTime = new Date(); + + public static validate(events, requestSize, reqParams) { + const startTime = new Date(); const respList: any[] = []; - const metaTags = events[0].metadata ? getMetadata(events[0].metadata) : {}; + const metaTags = events.length && events[0].metadata ? getMetadata(events[0].metadata) : {}; let ctxStatusCode = 200; - for (let i = 0; i < events.length; i++) { - const event = events[i]; - const eventStartTime = new Date(); + for(let event of events) { + let toAdd; + let exceptionOccured = false; + try { - const parsedEvent = event; - parsedEvent.request = { query: reqParams }; - const hv = await eventValidator.handleValidation(parsedEvent); - if (hv['dropEvent']) { - respList.push({ - output: event.message, - metadata: event.metadata, - statusCode: 400, - validationErrors: hv['validationErrors'], - error: JSON.stringify(constructValidationErrors(hv['validationErrors'])), - }); - stats.counter('tp_violation_type', 1, { - violationType: hv['violationType'], - ...metaTags, - }); - } else { - respList.push({ - output: event.message, - metadata: event.metadata, - statusCode: 200, - validationErrors: hv['validationErrors'], - error: JSON.stringify(constructValidationErrors(hv['validationErrors'])), - }); - stats.counter('tp_propagated_events', 1, { - ...metaTags, - }); + event.request = { query: reqParams }; // FIXME: Do we need this update ? + const validatedEvent = eventValidator.handleValidation(event); + toAdd = { + output: event.message, + metadata: event.metadata, + statusCode: validatedEvent['dropEvent'] ? HTTP_STATUS_CODES.BAD_REQUEST : HTTP_STATUS_CODES.OK, + validationErrors: validatedEvent['validationErrors'], + error: JSON.stringify(constructValidationErrors(validatedEvent['validationErrors'])), } } catch (error) { - const errMessage = `Error occurred while validating : ${error}`; - logger.error(errMessage); - let status = 200; + exceptionOccured = true; + // no need to process further if + // we have error of retry request error if (error instanceof RetryRequestError) { ctxStatusCode = error.statusCode; + break; } - if (error instanceof RespStatusError) { - status = error.statusCode; - } - respList.push({ + + toAdd = { output: event.message, metadata: event.metadata, - statusCode: status, + statusCode: error instanceof RespStatusError ? error.statusCode : HTTP_STATUS_CODES.OK, validationErrors: [], - error: errMessage, - }); - stats.counter('tp_errors', 1, { - ...metaTags, - workspaceId: event.metadata?.workspaceId, - trackingPlanId: event.metadata?.trackingPlanId, - }); - } finally { - stats.timing('tp_event_latency', eventStartTime, { - ...metaTags, - }); + error: `Error occurred while validating: ${error}`, + }; } - } - stats.counter('tp_events_count', events.length, { - ...metaTags, - }); - - stats.histogram('tp_request_size', requestSize, { - ...metaTags, - }); + // finally on every event, we need to + // capture the information related to the validates event + stats.counter('tp_event_validation', 1, { + ...metaTags, + workspaceId: event.metadata.workspaceId, + trackingPlanId: event.metadata.trackingPlanId, + status: toAdd.statusCode, + exception: exceptionOccured, + }) + respList.push(toAdd); + } - stats.timing('tp_request_latency', requestStartTime, { + // capture overall function latency + // with metadata tags + stats.timing('tp_request_latency', startTime, { ...metaTags, workspaceId: events[0]?.metadata?.workspaceId, trackingPlanId: events[0]?.metadata?.trackingPlanId,