Skip to content

Commit

Permalink
chore: added base support for raising event per tracking plan event
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyubabbar committed Mar 12, 2024
1 parent 01d460c commit dbd574d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/controllers/trackingPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = await TrackingPlanservice.validate(events, requestSize, reqParams);

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

View check run for this annotation

Codecov / codecov/patch

src/controllers/trackingPlan.ts#L10

Added line #L10 was not covered by tests
ctx.body = response.body;
ControllerUtility.postProcess(ctx, response.status);
return ctx;
Expand Down
113 changes: 56 additions & 57 deletions src/services/trackingPlan.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,87 @@
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 async validateTrackingPlan(events, requestSize, reqParams) {
const requestStartTime = new Date();
public static async validate(events, requestSize, reqParams) {
const startTime = Date.now();

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[0].metadata ? getMetadata(events[0].metadata) : {};
const metaTags = events.length && events[0].metadata ? getMetadata(events[0].metadata) : {};
const tpTags: any =
events.length && events[0].metadata ? getTrackingPlanMetadata(events[0].metadata) : {};
let ctxStatusCode = 200;

for (let i = 0; i < events.length; i++) {
let eventValidationResponse: any;
let exceptionOccured = false;
const eventStartTime = Date.now();

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
const event = events[i];
const eventStartTime = new Date();

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,
});
}
} catch (error) {
const errMessage = `Error occurred while validating : ${error}`;
logger.error(errMessage);
let status = 200;
event.request = { query: reqParams };
const validatedEvent = await eventValidator.handleValidation(event);
eventValidationResponse = {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L24-L26

Added lines #L24 - L26 were not covered by tests
output: event.message,
metadata: event.metadata,
statusCode: validatedEvent['dropEvent']
? HTTP_STATUS_CODES.BAD_REQUEST
: HTTP_STATUS_CODES.OK,

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L30-L31

Added lines #L30 - L31 were not covered by tests
validationErrors: validatedEvent['validationErrors'],
error: JSON.stringify(constructValidationErrors(validatedEvent['validationErrors'])),
};
} catch (error: any) {
logger.debug(

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L36

Added line #L36 was not covered by tests
`Error occurred while validating event`,
'event',
`${event.message?.event}::${event.message?.type}`,
'trackingPlan',
`${tpTags?.trackingPlanId}`,
'error',
error.message,
);

exceptionOccured = true;

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L46

Added line #L46 was not covered by tests
// no need to process further if
// we have error of retry request error
if (error instanceof RetryRequestError) {
ctxStatusCode = error.statusCode;
break;

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L51

Added line #L51 was not covered by tests
}
if (error instanceof RespStatusError) {
status = error.statusCode;
}
respList.push({

eventValidationResponse = {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L54

Added line #L54 was not covered by tests
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,
});
error: `Error occurred while validating: ${error}`,
};
} finally {
stats.timing('tp_event_latency', eventStartTime, {
// 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 64 in src/services/trackingPlan.ts

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L64

Added line #L64 was not covered by tests
...metaTags,
...tpTags,
status: eventValidationResponse.statusCode,
exception: exceptionOccured,
});
}
}

stats.counter('tp_events_count', events.length, {
...metaTags,
});
respList.push(eventValidationResponse);

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L72

Added line #L72 was not covered by tests
}

stats.histogram('tp_request_size', requestSize, {
stats.histogram('tp_batch_size', requestSize, {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L75

Added line #L75 was not covered by tests
...metaTags,
...tpTags,
});

stats.timing('tp_request_latency', requestStartTime, {
// capture overall function latency
// with metadata tags
stats.histogram('tp_batch_validation_latency', (Date.now() - startTime) / 1000, {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L82

Added line #L82 was not covered by tests
...metaTags,
workspaceId: events[0]?.metadata?.workspaceId,
trackingPlanId: events[0]?.metadata?.trackingPlanId,
...tpTags,
});

return { body: respList, status: ctxStatusCode };
Expand Down
30 changes: 25 additions & 5 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,34 @@ class Prometheus {
labelNames: ['method', 'route', 'code'],
},
{
name: 'tp_request_size',
help: 'tp_request_size',
name: 'tp_batch_size',
help: 'Size of batch of events for tracking plan validation',
type: 'histogram',
labelNames: ['sourceType', 'destinationType', 'k8_namespace'],
labelNames: [
'sourceType',
'destinationType',
'k8_namespace',
'workspaceId',
'trackingPlanId',
],
},
{
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_request_latency',
help: 'tp_request_latency',
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 dbd574d

Please sign in to comment.