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): added stats during various stages of event schema validation #2870

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion src/util/eventValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const NodeCache = require('node-cache');
const hash = require('object-hash');
const logger = require('../logger');
const trackingPlan = require('./trackingPlan');
const stats = require('./stats');

const SECONDS_IN_DAY = 60 * 60 * 24 * 1;
const eventSchemaCache = new NodeCache();
const ajv19Cache = new NodeCache({ useClones: false, stdTTL: SECONDS_IN_DAY });
const ajv4Cache = new NodeCache({ useClones: false, stdTTL: SECONDS_IN_DAY });
const { isEmptyObject } = require('../v0/util');
const { isEmptyObject, getMetadata } = require('../v0/util');

const defaultOptions = {
strictRequired: true,
Expand Down Expand Up @@ -181,13 +182,24 @@ async function validate(event) {
}
}

let start = new Date();
let validateEvent = eventSchemaCache.get(schemaHash);
if (!validateEvent) {
validateEvent = ajv.compile(eventSchema);
eventSchemaCache.set(schemaHash, validateEvent);
}
stats.timing(`tp_ajv_caching_latency`, start, {
eventName: event.event || event.type,
...getMetadata(event.metadata)
});

start = new Date();
const valid = validateEvent(event.message);
stats.timing(`tp_ajv_validation_latency`, start, {
eventName: event.event || event.type,
...getMetadata(event.metadata)
});

if (valid) {
return [];
}
Expand Down Expand Up @@ -355,7 +367,13 @@ async function handleValidation(event) {
};
}

let start = new Date();
const validationErrors = await validate(event);
stats.timing(`tp_validation_latency`, start, {
eventName: event.event || event.type,
...getMetadata(event.metadata)
});

if (validationErrors.length === 0) {
return {
dropEvent,
Expand Down
33 changes: 33 additions & 0 deletions src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,39 @@ class Prometheus {
'transformationId',
'workspaceId'
],
},
{
name: 'tp_validation_latency',
help: 'tp_validation_latency',
type: 'histogram',
labelNames: [
'eventName',
'sourceType',
'destinationType',
'k8_namespace',
],
},
{
name: 'tp_ajv_validation_latency',
help: 'tp_ajv_validation_latency',
type: 'histogram',
labelNames: [
'eventName',
'sourceType',
'destinationType',
'k8_namespace',
],
},
{
name: 'tp_ajv_caching_latency',
help: 'tp_ajv_caching_latency',
type: 'histogram',
labelNames: [
'eventName',
'sourceType',
'destinationType',
'k8_namespace',
],
}
];

Expand Down
Loading