mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor(KyselyLogger): use startTimer instead of observe
This commit is contained in:
parent
d580cac763
commit
ac9bdfde62
1 changed files with 4 additions and 14 deletions
|
|
@ -1,28 +1,18 @@
|
||||||
import { Stickynotes } from '@soapbox/stickynotes';
|
import { Stickynotes } from '@soapbox/stickynotes';
|
||||||
import { Logger } from 'kysely';
|
import { Logger } from 'kysely';
|
||||||
import { dbQueryTime } from '@/metrics.ts';
|
import { dbQueryTimeHistogram } from '@/metrics.ts';
|
||||||
|
|
||||||
export const prometheusParams = {
|
|
||||||
threshold: 10000,
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Log the SQL for queries. */
|
/** Log the SQL for queries. */
|
||||||
export const KyselyLogger: Logger = (event) => {
|
export const KyselyLogger: Logger = (event) => {
|
||||||
if (event.level === 'query') {
|
if (event.level === 'query') {
|
||||||
const console = new Stickynotes('ditto:sql');
|
const console = new Stickynotes('ditto:sql');
|
||||||
|
|
||||||
|
const timer = dbQueryTimeHistogram.startTimer();
|
||||||
|
|
||||||
const { query, queryDurationMillis } = event;
|
const { query, queryDurationMillis } = event;
|
||||||
const { sql, parameters } = query;
|
const { sql, parameters } = query;
|
||||||
|
|
||||||
if (queryDurationMillis > prometheusParams.threshold) {
|
timer();
|
||||||
const labels = {
|
|
||||||
sql,
|
|
||||||
parameters: JSON.stringify(
|
|
||||||
parameters.filter((param: any) => ['string', 'number'].includes(typeof param)) as (string | number)[],
|
|
||||||
),
|
|
||||||
};
|
|
||||||
dbQueryTime.observe(labels, queryDurationMillis);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug(
|
console.debug(
|
||||||
sql,
|
sql,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue