Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(database monitor): metric for connection duration of 100 slowest #3781

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import prometheusClient from 'prom-client'
import { join } from 'lodash-es'
import type { MetricInitializer } from '@/observability/types.js'

export const init: MetricInitializer = (config) => {
const { labelNames, namePrefix, logger } = config
const currentConnections = new prometheusClient.Gauge({
name: join([namePrefix, 'db_connections'], '_'),
help: 'Age of database connections, by sql query, in milliseconds',
labelNames: ['query', 'region', ...labelNames]
})
return async (params) => {
const { dbClients, labels } = params
await Promise.all(
dbClients.map(async ({ client, regionKey }) => {
try {
const currentConnectionResults = await client.raw<{
rows: [{ datname: string; state: string; query: string; interval: string }]
}>(
`
SELECT datname, state, query, ROUND((EXTRACT(EPOCH FROM clock_timestamp()) - EXTRACT(EPOCH FROM query_start)) * 1000) AS interval
FROM pg_stat_activity
WHERE state <> 'idle'
AND query NOT LIKE '% FROM pg_stat_activity %'
AND query NOT LIKE 'START_REPLICATION SLOT %'
AND query NOT LIKE '<insufficient privilege>'
ORDER BY interval DESC
LIMIT 100;
`
)
currentConnections.reset()
for (const row of currentConnectionResults.rows) {
currentConnections.set(
{ ...labels, query: row.query, region: regionKey },
parseInt(row.interval)
)
}
} catch (err) {
logger.warn(
{ err, region: regionKey },
"Failed to collect current connections from region '{region}'."
)
}
})
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join } from 'lodash-es'
import { Counter, Histogram, Registry } from 'prom-client'
import prometheusClient from 'prom-client'
import { init as commits } from '@/observability/metrics/commits.js'
import { init as currentConnections } from '@/observability/metrics/currentConnections.js'
import { init as dbMaxLogicalReplicationWorkers } from '@/observability/metrics/dbMaxLogicalReplicationWorkers.js'
import { init as dbMaxReplicationSlots } from '@/observability/metrics/dbMaxReplicationSlots.js'
import { init as dbMaxSyncWorkersPerSubscription } from '@/observability/metrics/dbMaxSyncWorkersPerSubscription.js'
Expand Down Expand Up @@ -60,6 +61,7 @@ function initMonitoringMetrics(params: {

const metricsToInitialize = [
commits,
currentConnections,
dbMaxLogicalReplicationWorkers,
dbMaxReplicationSlots,
dbMaxSyncWorkersPerSubscription,
Expand Down