Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Mar 11, 2024
1 parent 646608d commit 8ab4269
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
51 changes: 28 additions & 23 deletions src/services/trackingPlan.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 };
Expand Down
18 changes: 16 additions & 2 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2267,6 +2272,7 @@ module.exports = {
getMappingConfig,
getMetadata,
getTransformationMetadata,
getTrackingPlanMetadata,
getParsedIP,
getStringValueOfJSON,
getSuccessRespEvents,
Expand Down

0 comments on commit 8ab4269

Please sign in to comment.