From 7c7c584b78a1dda68b900f04b3fdb302538eef13 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Wed, 26 Jun 2024 01:30:16 +0530 Subject: [PATCH] basic (and incredibly stupid but potentially genius) db_query_time histogram --- src/controllers/nostr/relay.ts | 12 +++++++++++- src/db/KyselyLogger.ts | 15 +++++++++++++++ src/metrics.ts | 7 ++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 4e624e9b..63c52236 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -14,6 +14,7 @@ import { relayConnectionsGauge, relayEventCounter, relayMessageCounter } from '@ import * as pipeline from '@/pipeline.ts'; import { RelayError } from '@/RelayError.ts'; import { Storages } from '@/storages.ts'; +import { prometheusParams } from "@/db/KyselyLogger.ts"; /** Limit of initial events returned for a subscription. */ const FILTER_LIMIT = 100; @@ -52,7 +53,16 @@ function connectStream(socket: WebSocket) { handleReq(msg); return; 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; case 'CLOSE': handleClose(msg); diff --git a/src/db/KyselyLogger.ts b/src/db/KyselyLogger.ts index e39cbd08..46e73e96 100644 --- a/src/db/KyselyLogger.ts +++ b/src/db/KyselyLogger.ts @@ -1,5 +1,10 @@ import { Stickynotes } from '@soapbox/stickynotes'; import { Logger } from 'kysely'; +import { dbQueryTime } from '@/metrics.ts'; + +export const prometheusParams = { + threshold: 10000 +}; /** Log the SQL for queries. */ export const KyselyLogger: Logger = (event) => { @@ -9,6 +14,16 @@ export const KyselyLogger: Logger = (event) => { const { query, queryDurationMillis } = event; 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( sql, JSON.stringify(parameters), diff --git a/src/metrics.ts b/src/metrics.ts index 68ddfcef..7daa6600 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,4 +1,4 @@ -import { Counter, Gauge } from 'prom-client'; +import { Counter, Histogram, Gauge } from 'prom-client'; export const httpRequestCounter = new Counter({ name: 'http_requests_total', @@ -67,3 +67,8 @@ export const dbAvailableConnectionsGauge = new Gauge({ name: 'db_available_connections', help: 'Number of available connections in the database pool', }); + +export const dbQueryTime = new Histogram({ + name: "db_query_time", + help: "Time taken per kysely query" +}) \ No newline at end of file