-
Notifications
You must be signed in to change notification settings - Fork 114
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
* @param events | ||||||
* @param requestSize | ||||||
* @param reqParams | ||||||
* @returns | ||||||
*/ | ||||||
public static async validate(events, requestSize, reqParams) { | ||||||
const requestStartTime = new Date(); | ||||||
const respList: any[] = []; | ||||||
const validatedEvents: any[] = []; | ||||||
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) => { | ||||||
let errorReport; | ||||||
let exception = false; | ||||||
|
||||||
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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
failure['dropEvent'] ? | ||||||
errorReport = { | ||||||
statusCode: 400, | ||||||
validationErrors: failure['validationErrors'], | ||||||
error: JSON.stringify(constructValidationErrors(failure['validationErrors'])), | ||||||
violationType: failure['violationType'], | ||||||
}: | ||||||
errorReport = { | ||||||
statusCode: 200, | ||||||
validationErrors: failure['validationErrors'], | ||||||
error: JSON.stringify(constructValidationErrors(failure['validationErrors'])), | ||||||
}; | ||||||
|
||||||
} catch (error) { | ||||||
exception = true; | ||||||
|
||||||
const errMessage = `Error occurred while validating : ${error}`; | ||||||
logger.error(errMessage); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = { | ||||||
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, { | ||||||
...metaTags, | ||||||
workspaceId: event.metadata?.workspaceId, | ||||||
trackingPlanId: event.metadata?.trackingPlanId, | ||||||
status: errorReport?.status, | ||||||
violationType: errorReport.violationType ? errorReport.violationType : '', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||||||
output: event.message, | ||||||
meatdata: event.metadata, | ||||||
...errorReport, | ||||||
}); | ||||||
|
||||||
}); | ||||||
|
||||||
stats.counter('tp_events_count', events.length, { | ||||||
...metaTags, | ||||||
|
@@ -85,6 +99,6 @@ | |||||
trackingPlanId: events[0]?.metadata?.trackingPlanId, | ||||||
}); | ||||||
|
||||||
return { body: respList, status: ctxStatusCode }; | ||||||
return { body: validatedEvents, status: ctxStatusCode }; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.