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 9931404 commit 55834b0
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();

Check warning on line 10 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L9-L10

Added lines #L9 - L10 were not covered by tests
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) {

Check warning on line 17 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L17

Added line #L17 was not covered by tests
let toAdd: any;
let eventValidationResponse: any;
let exceptionOccured = false;
let eventStartTime = new Date();

Check warning on line 20 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L19-L20

Added lines #L19 - L20 were not covered by tests

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 = {

Check warning on line 25 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L23-L25

Added lines #L23 - L25 were not covered by tests
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,

Check warning on line 30 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L29-L30

Added lines #L29 - L30 were not covered by tests
validationErrors: validatedEvent['validationErrors'],
error: JSON.stringify(constructValidationErrors(validatedEvent['validationErrors'])),
}
};
} catch (error) {
logger.debug(`Error occurred while validating: ${error}`);

Check warning on line 35 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L35

Added line #L35 was not covered by tests

exceptionOccured = true;

Check warning on line 37 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L37

Added line #L37 was not covered by tests
// no need to process further if
// we have error of retry request error
Expand All @@ -36,33 +42,32 @@ export class TrackingPlanservice {
break;

Check warning on line 42 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L42

Added line #L42 was not covered by tests
}

toAdd = {
eventValidationResponse = {

Check warning on line 45 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L45

Added line #L45 was not covered by tests
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, {

Check warning on line 55 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L55

Added line #L55 was not covered by tests
...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);

Check warning on line 63 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L63

Added line #L63 was not covered by tests
}

// capture overall function latency
// with metadata tags
stats.timing('tp_request_latency', startTime, {
stats.histogram('tp_batch_validation_latency', startTime, {

Check warning on line 68 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L68

Added line #L68 was not covered by tests
...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 @@ -588,8 +588,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 @@ -1426,6 +1426,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 @@ -2184,6 +2189,7 @@ module.exports = {
getMappingConfig,
getMetadata,
getTransformationMetadata,
getTrackingPlanMetadata,
getParsedIP,
getStringValueOfJSON,
getSuccessRespEvents,
Expand Down

0 comments on commit 55834b0

Please sign in to comment.