Fix peometheus query fingerprinting #24

Merged
yume merged 1 commit from develop into master 2024-11-18 03:29:54 -06:00

View file

@ -95,7 +95,6 @@ const sqlLogger = dbLogger.createSubLogger('sql', 'gray');
type QueryTagCache = { type QueryTagCache = {
join: string; join: string;
from: string; from: string;
hash: string;
}; };
function dedupConsecutive<T>(arr: T[]): T[] { function dedupConsecutive<T>(arr: T[]): T[] {
@ -107,40 +106,34 @@ function simplifyIdentifiers(sql: string) {
} }
function extractQueryTags(query: string): QueryTagCache { function extractQueryTags(query: string): QueryTagCache {
const joins = query.matchAll(/(LEFT|RIGHT|INNER|OUTER) JOIN ([a-zA-Z0-9_"`.\s]+) ON/ig); const joins = query.matchAll(/(LEFT|RIGHT|INNER|OUTER)[\s\S]+JOIN[\s\r\n]+([a-zA-Z0-9_"`.]+)/ig);
const froms = query.matchAll(/FROM ([a-zA-Z0-9_"`.]+)/ig); const froms = query.matchAll(/FROM[\s\r\n]+([a-zA-Z0-9_"`.]+)/ig);
const join = Array.from(joins).map(j => `${j[1]}:${simplifyIdentifiers(j[2])}`).join('|'); const join = Array.from(joins).map(j => `${j[1]}:${simplifyIdentifiers(j[2])}`).join('|');
const from = dedupConsecutive(Array.from(froms).map(f => simplifyIdentifiers(f[1]))).join('|'); const from = dedupConsecutive(Array.from(froms).map(f => simplifyIdentifiers(f[1]))).join('|');
// this is not for security just to reduce metrics size just for confirmation in local environment
// all the code is public there is nothing secret in the query itself
const hash = createHash('md5');
hash.update(query);
return { return {
join, join,
from, from,
hash: hash.digest('hex'),
}; };
} }
const mQueryCounter = metricCounter({ const mQueryCounter = metricCounter({
name: 'misskey_postgres_query_total', name: 'misskey_postgres_query_total',
help: 'Total queries to postgres', help: 'Total queries to postgres',
labelNames: ['join', 'from', 'hash'], labelNames: ['join', 'from'],
}); });
const mQueryErrorCounter = metricCounter({ const mQueryErrorCounter = metricCounter({
name: 'misskey_postgres_query_error_total', name: 'misskey_postgres_query_error_total',
help: 'Total errors in queries to postgres', help: 'Total errors in queries to postgres',
labelNames: ['join', 'from', 'hash'], labelNames: ['join', 'from'],
}); });
const mSlowQueryHisto = metricHistogram({ const mSlowQueryHisto = metricHistogram({
name: 'misskey_postgres_query_slow_duration_seconds', name: 'misskey_postgres_query_slow_duration_seconds',
help: 'Duration of slow queries to postgres', help: 'Duration of slow queries to postgres',
labelNames: ['join', 'from', 'hash'], labelNames: ['join', 'from'],
buckets: [0.1, 0.5, 1, 2, 5, 10, 30, 300], buckets: [0.1, 0.5, 1, 2, 5, 10, 30, 300],
}); });