diff --git a/src/services/trackingPlan.ts b/src/services/trackingPlan.ts index 64bb65a0f7..ea0502c7d4 100644 --- a/src/services/trackingPlan.ts +++ b/src/services/trackingPlan.ts @@ -1,33 +1,39 @@ import logger from '../logger'; import { RetryRequestError, RespStatusError, constructValidationErrors } from '../util/utils'; -import { getMetadata } from '../v0/util'; +import { getMetadata, getTrackingPlanMetadata } 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 validate(events, requestSize, reqParams) { + public static async validate(events, requestSize, reqParams) { const startTime = new Date(); const respList: any[] = []; const metaTags = events.length && events[0].metadata ? getMetadata(events[0].metadata) : {}; + const tpTags = + events.length && events[0].metadata ? getTrackingPlanMetadata(events[0].metadata) : {}; let ctxStatusCode = 200; for (let event of events) { - let toAdd: any; + let eventValidationResponse: any; let exceptionOccured = false; + let eventStartTime = new Date(); try { - event.request = { query: reqParams }; // FIXME: Do we need this update ? - const validatedEvent = eventValidator.handleValidation(event); - toAdd = { + event.request = { query: reqParams }; + const validatedEvent = await eventValidator.handleValidation(event); + eventValidationResponse = { output: event.message, metadata: event.metadata, - statusCode: validatedEvent['dropEvent'] ? HTTP_STATUS_CODES.BAD_REQUEST : HTTP_STATUS_CODES.OK, + statusCode: validatedEvent['dropEvent'] + ? HTTP_STATUS_CODES.BAD_REQUEST + : HTTP_STATUS_CODES.OK, validationErrors: validatedEvent['validationErrors'], error: JSON.stringify(constructValidationErrors(validatedEvent['validationErrors'])), - } + }; } catch (error) { + logger.debug(`Error occurred while validating: ${error}`); + exceptionOccured = true; // no need to process further if // we have error of retry request error @@ -36,33 +42,32 @@ export class TrackingPlanservice { break; } - toAdd = { + eventValidationResponse = { output: event.message, metadata: event.metadata, statusCode: error instanceof RespStatusError ? error.statusCode : HTTP_STATUS_CODES.OK, validationErrors: [], error: `Error occurred while validating: ${error}`, }; + } finally { + // finally on every event, we need to + // capture the information related to the validates event + stats.timing('tp_event_validation_latency', eventStartTime, { + ...metaTags, + ...tpTags, + status: eventValidationResponse.statusCode, + exception: exceptionOccured, + }); } - // 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); + respList.push(eventValidationResponse); } // capture overall function latency // with metadata tags - stats.timing('tp_request_latency', startTime, { + stats.histogram('tp_batch_validation_latency', startTime, { ...metaTags, - workspaceId: events[0]?.metadata?.workspaceId, - trackingPlanId: events[0]?.metadata?.trackingPlanId, + ...tpTags, }); return { body: respList, status: ctxStatusCode }; diff --git a/src/util/prometheus.js b/src/util/prometheus.js index 89e5424c0c..d406fadee0 100644 --- a/src/util/prometheus.js +++ b/src/util/prometheus.js @@ -596,8 +596,22 @@ class Prometheus { labelNames: ['sourceType', 'destinationType', 'k8_namespace'], }, { - name: 'tp_request_latency', - help: 'tp_request_latency', + name: 'tp_event_validation_latency', + help: 'Latency of validating tracking plan at event level', + type: 'histogram', + labelNames: [ + 'sourceType', + 'destinationType', + 'k8_namespace', + 'workspaceId', + 'trackingPlanId', + 'status', + 'exception', + ], + }, + { + name: 'tp_batch_validation_latency', + help: 'Latency of validating tracking plan at batch level', type: 'histogram', labelNames: [ 'sourceType', diff --git a/src/v0/util/index.js b/src/v0/util/index.js index c1debce088..32872cc5d9 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -1419,6 +1419,11 @@ function getStringValueOfJSON(json) { return output; } +const getTrackingPlanMetadata = (metadata) => ({ + trackingPlanId: metadata.trackingPlanId, + workspaceId: metadata.workspaceId, +}); + const getMetadata = (metadata) => ({ sourceType: metadata.sourceType, destinationType: metadata.destinationType, @@ -2267,6 +2272,7 @@ module.exports = { getMappingConfig, getMetadata, getTransformationMetadata, + getTrackingPlanMetadata, getParsedIP, getStringValueOfJSON, getSuccessRespEvents,