Skip to content

Commit

Permalink
Merge branch 'db-query-time-prom-metric' into 'main'
Browse files Browse the repository at this point in the history
basic (and incredibly stupid but potentially genius) db_query_time histogram

See merge request soapbox-pub/ditto!400
  • Loading branch information
alexgleason committed Jun 27, 2024
2 parents a965c3c + 9cdb8ec commit eba0605
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/db/KyselyLogger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Stickynotes } from '@soapbox/stickynotes';
import { Logger } from 'kysely';
import { dbQueryTimeHistogram } from '@/metrics.ts';

/** Log the SQL for queries. */
export const KyselyLogger: Logger = (event) => {
Expand All @@ -9,6 +10,8 @@ export const KyselyLogger: Logger = (event) => {
const { query, queryDurationMillis } = event;
const { sql, parameters } = query;

dbQueryTimeHistogram.observe(queryDurationMillis);

console.debug(
sql,
JSON.stringify(parameters),
Expand Down
7 changes: 6 additions & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Counter, Gauge } from 'prom-client';
import { Counter, Gauge, Histogram } from 'prom-client';

export const httpRequestCounter = new Counter({
name: 'http_requests_total',
Expand Down Expand Up @@ -67,3 +67,8 @@ export const dbAvailableConnectionsGauge = new Gauge({
name: 'db_available_connections',
help: 'Number of available connections in the database pool',
});

export const dbQueryTimeHistogram = new Histogram({
name: 'db_query_duration_seconds',
help: 'Duration of database queries',
});

0 comments on commit eba0605

Please sign in to comment.