Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dm): add tracking plan stat per event level #2673

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 7 additions & 119 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@
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
104 changes: 59 additions & 45 deletions src/services/trackingPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,85 @@
import stats from '../util/stats';

export default class TrackingPlanservice {
public static async validateTrackingPlan(events, requestSize, reqParams) {
/**
* validate takes in events as argument and iterates over the events
* validating them using the tracking
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* validating them using the tracking
* validating them using the tracking plan

* @param events
* @param requestSize
* @param reqParams
* @returns
*/
public static async validate(events, requestSize, reqParams) {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L16

Added line #L16 was not covered by tests
const requestStartTime = new Date();
const respList: any[] = [];
const validatedEvents: any[] = [];

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L18

Added line #L18 was not covered by tests
const metaTags = 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();

events.forEach(async (event) => {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L23

Added line #L23 was not covered by tests
let errorReport;
let exception = false;

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L25

Added line #L25 was not covered by tests

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,
});
}
const failure = await eventValidator.handleValidation(parsedEvent);

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L30

Added line #L30 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const failure = await eventValidator.handleValidation(parsedEvent);
const validatedEvent = await eventValidator.handleValidation(parsedEvent);


failure['dropEvent'] ?
errorReport = {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L33

Added line #L33 was not covered by tests
statusCode: 400,
validationErrors: failure['validationErrors'],
error: JSON.stringify(constructValidationErrors(failure['validationErrors'])),
violationType: failure['violationType'],
}:
errorReport = {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L39

Added line #L39 was not covered by tests
statusCode: 200,
validationErrors: failure['validationErrors'],
error: JSON.stringify(constructValidationErrors(failure['validationErrors'])),
};

} catch (error) {
exception = 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

const errMessage = `Error occurred while validating : ${error}`;
logger.error(errMessage);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add event name in logger to pin point where error happened


let status = 200;
if (error instanceof RetryRequestError) {
ctxStatusCode = error.statusCode;
}
if (error instanceof RespStatusError) {
status = error.statusCode;
}
respList.push({
output: event.message,
metadata: event.metadata,

errorReport = {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L59

Added line #L59 was not covered by tests
statusCode: status,
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,
});
}
}
}

// stat which is fired per tp event validation level
// containing labels which allows us to identify the information
// at each stage.
stats.counter('tp_event_validation', 1, {

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L69

Added line #L69 was not covered by tests
...metaTags,
workspaceId: event.metadata?.workspaceId,
trackingPlanId: event.metadata?.trackingPlanId,
status: errorReport?.status,
violationType: errorReport.violationType ? errorReport.violationType : '',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one event can have multiple violations. We can stop capturing this info

exception: exception,
});

// push validated event into final return which contains error report
// for the validation and output along with metadata.
validatedEvents.push({

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L80

Added line #L80 was not covered by tests
output: event.message,
meatdata: event.metadata,
...errorReport,
});

});

stats.counter('tp_events_count', events.length, {
...metaTags,
Expand All @@ -85,6 +99,6 @@
trackingPlanId: events[0]?.metadata?.trackingPlanId,
});

return { body: respList, status: ctxStatusCode };
return { body: validatedEvents, status: ctxStatusCode };

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

View check run for this annotation

Codecov / codecov/patch

src/services/trackingPlan.ts#L102

Added line #L102 was not covered by tests
}
}
Loading