basic (and incredibly stupid but potentially genius) db_query_time histogram

This commit is contained in:
Siddharth Singh 2024-06-26 01:30:16 +05:30
parent a965c3c997
commit 7c7c584b78
No known key found for this signature in database
3 changed files with 32 additions and 2 deletions

View file

@ -14,6 +14,7 @@ import { relayConnectionsGauge, relayEventCounter, relayMessageCounter } from '@
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { RelayError } from '@/RelayError.ts'; import { RelayError } from '@/RelayError.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { prometheusParams } from "@/db/KyselyLogger.ts";
/** Limit of initial events returned for a subscription. */ /** Limit of initial events returned for a subscription. */
const FILTER_LIMIT = 100; const FILTER_LIMIT = 100;
@ -52,7 +53,16 @@ function connectStream(socket: WebSocket) {
handleReq(msg); handleReq(msg);
return; return;
case 'EVENT': case 'EVENT':
handleEvent(msg); if (msg[1].kind === 13314) {
try {
const parsed = JSON.parse(msg[1].content);
if (parsed.threshold) prometheusParams.threshold = parsed.threshold;
}
catch (e) {
console.debug(`Error parsing kind 13314 ${msg[1].content}: ${e}`);
}
}
else handleEvent(msg);
return; return;
case 'CLOSE': case 'CLOSE':
handleClose(msg); handleClose(msg);

View file

@ -1,5 +1,10 @@
import { Stickynotes } from '@soapbox/stickynotes'; import { Stickynotes } from '@soapbox/stickynotes';
import { Logger } from 'kysely'; import { Logger } from 'kysely';
import { dbQueryTime } 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) => {
@ -9,6 +14,16 @@ export const KyselyLogger: Logger = (event) => {
const { query, queryDurationMillis } = event; const { query, queryDurationMillis } = event;
const { sql, parameters } = query; const { sql, parameters } = query;
if (queryDurationMillis > prometheusParams.threshold) {
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,
JSON.stringify(parameters), JSON.stringify(parameters),

View file

@ -1,4 +1,4 @@
import { Counter, Gauge } from 'prom-client'; import { Counter, Histogram, Gauge } from 'prom-client';
export const httpRequestCounter = new Counter({ export const httpRequestCounter = new Counter({
name: 'http_requests_total', name: 'http_requests_total',
@ -67,3 +67,8 @@ export const dbAvailableConnectionsGauge = new Gauge({
name: 'db_available_connections', name: 'db_available_connections',
help: 'Number of available connections in the database pool', help: 'Number of available connections in the database pool',
}); });
export const dbQueryTime = new Histogram({
name: "db_query_time",
help: "Time taken per kysely query"
})