From b78edccd1931045c2967f6b33b2d8eda9a573d14 Mon Sep 17 00:00:00 2001 From: eternal-flame-AD Date: Mon, 18 Nov 2024 03:29:53 -0600 Subject: [PATCH] Fix permetheus query fingerprinting (#24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: eternal-flame-AD ## What ## Why ## Additional info (optional) ## Checklist - [ ] Read the [contribution guide](https://github.com/misskey-dev/misskey/blob/develop/CONTRIBUTING.md) - [ ] Test working in a local environment - [ ] (If needed) Add story of storybook - [ ] (If needed) Update CHANGELOG.md - [ ] (If possible) Add tests Reviewed-on: https://forge.yumechi.jp/yume/yumechi-no-kuni/pulls/24 Co-authored-by: eternal-flame-AD Co-committed-by: eternal-flame-AD --- packages/backend/src/postgres.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/postgres.ts b/packages/backend/src/postgres.ts index ed1fccddf0..b1cb25224b 100644 --- a/packages/backend/src/postgres.ts +++ b/packages/backend/src/postgres.ts @@ -95,7 +95,6 @@ const sqlLogger = dbLogger.createSubLogger('sql', 'gray'); type QueryTagCache = { join: string; from: string; - hash: string; }; function dedupConsecutive(arr: T[]): T[] { @@ -107,40 +106,34 @@ function simplifyIdentifiers(sql: string) { } function extractQueryTags(query: string): QueryTagCache { - const joins = query.matchAll(/(LEFT|RIGHT|INNER|OUTER) JOIN ([a-zA-Z0-9_"`.\s]+) ON/ig); - const froms = query.matchAll(/FROM ([a-zA-Z0-9_"`.]+)/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[\s\r\n]+([a-zA-Z0-9_"`.]+)/ig); 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('|'); - // 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 { join, from, - hash: hash.digest('hex'), }; } const mQueryCounter = metricCounter({ name: 'misskey_postgres_query_total', help: 'Total queries to postgres', - labelNames: ['join', 'from', 'hash'], + labelNames: ['join', 'from'], }); const mQueryErrorCounter = metricCounter({ name: 'misskey_postgres_query_error_total', help: 'Total errors in queries to postgres', - labelNames: ['join', 'from', 'hash'], + labelNames: ['join', 'from'], }); const mSlowQueryHisto = metricHistogram({ name: 'misskey_postgres_query_slow_duration_seconds', 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], });