Fix permetheus query fingerprinting ()

Signed-off-by: eternal-flame-AD <yume@yumechi.jp>

<!-- ℹ お読みください / README
PRありがとうございます! PRを作成する前に、コントリビューションガイドをご確認ください:
Thank you for your PR! Before creating a PR, please check the contribution guide:
https://github.com/misskey-dev/misskey/blob/develop/CONTRIBUTING.md
-->

## What
<!-- このPRで何をしたのか? どう変わるのか? -->
<!-- What did you do with this PR? How will it change things? -->

## Why
<!-- なぜそうするのか? どういう意図なのか? 何が困っているのか? -->
<!-- Why do you do it? What are your intentions? What is the problem? -->

## Additional info (optional)
<!-- テスト観点など -->
<!-- Test perspective, etc -->

## 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: 
Co-authored-by: eternal-flame-AD <yume@yumechi.jp>
Co-committed-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
ゆめ 2024-11-18 03:29:53 -06:00 committed by ゆめ
parent b805239b39
commit b78edccd19

View file

@ -95,7 +95,6 @@ const sqlLogger = dbLogger.createSubLogger('sql', 'gray');
type QueryTagCache = {
join: string;
from: string;
hash: string;
};
function dedupConsecutive<T>(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],
});