Skip to content

Commit

Permalink
Revert "feat(multi-region): metrics for knex for all regional databas…
Browse files Browse the repository at this point in the history
…es (#3508)"

This reverts commit f51eb91.
  • Loading branch information
iainsproat authored Nov 21, 2024
1 parent f51eb91 commit e8c3450
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 286 deletions.
4 changes: 2 additions & 2 deletions packages/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import { GraphQLError } from 'graphql'
import { redactSensitiveVariables } from '@/logging/loggingHelper'
import { buildMocksConfig } from '@/modules/mocks'
import { defaultErrorHandler } from '@/modules/core/rest/defaultErrorHandler'
import { migrateDbToLatestFactory } from '@/db/migrations'
import { migrateDbToLatest } from '@/db/migrations'
import { statusCodePlugin } from '@/modules/core/graph/plugins/statusCode'
import { BaseError, ForbiddenError } from '@/modules/shared/errors'
import { loggingPlugin } from '@/modules/core/graph/plugins/logging'
Expand Down Expand Up @@ -364,7 +364,7 @@ export async function init() {

// Moves things along automatically on restart.
// Should perhaps be done manually?
await migrateDbToLatestFactory({ region: 'main', db: knex })()
await migrateDbToLatest(knex)()

app.use(cookieParser())
app.use(DetermineRequestIdMiddleware)
Expand Down
16 changes: 3 additions & 13 deletions packages/server/db/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { Knex } from 'knex'
import { logger } from '@/logging/logging'

export const migrateDbToLatestFactory =
(params: { db: Knex; region: string }) => async () => {
const { db, region } = params
try {
await db.migrate.latest()
} catch (err: unknown) {
logger.error(
{ err, region },
'Error migrating db to latest for region "{region}".'
)
}
}
export const migrateDbToLatest = (db: Knex) => async () => {
await db.migrate.latest()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MetricConfig = {
prefix?: string
labels?: Record<string, string>
buckets?: Record<string, number[]>
getDbClients: () => Promise<Record<string, Knex>>
knex: Knex
}

type HighFrequencyMonitor = {
Expand Down
23 changes: 9 additions & 14 deletions packages/server/logging/highFrequencyMetrics/knexConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MetricConfig = {
prefix?: string
labels?: Record<string, string>
buckets?: Record<BucketName, number[]>
getDbClients: () => Promise<Record<string, Knex>>
knex: Knex
}

export const knexConnections = (registry: Registry, config: MetricConfig): Metric => {
Expand All @@ -40,6 +40,7 @@ export const knexConnections = (registry: Registry, config: MetricConfig): Metri
const labels = config.labels ?? {}
const labelNames = Object.keys(labels)
const buckets = { ...DEFAULT_KNEX_TOTAL_BUCKETS, ...config.buckets }
const knex = config.knex

const knexConnectionsFree = new Histogram({
name: namePrefix + KNEX_CONNECTIONS_FREE,
Expand Down Expand Up @@ -91,20 +92,14 @@ export const knexConnections = (registry: Registry, config: MetricConfig): Metri

return {
collect: () => {
for (const [region, knex] of Object.entries(config.getDbClients())) {
const labelsAndRegion = { ...labels, region }
const connPool = knex.client.pool
const connPool = knex.client.pool

knexConnectionsFree.observe(labelsAndRegion, connPool.numFree())
knexConnectionsUsed.observe(labelsAndRegion, connPool.numUsed())
knexPendingAcquires.observe(labelsAndRegion, connPool.numPendingAcquires())
knexPendingCreates.observe(labelsAndRegion, connPool.numPendingCreates())
knexPendingValidations.observe(
labelsAndRegion,
connPool.numPendingValidations()
)
knexRemainingCapacity.observe(labelsAndRegion, numberOfFreeConnections(knex))
}
knexConnectionsFree.observe(labels, connPool.numFree())
knexConnectionsUsed.observe(labels, connPool.numUsed())
knexPendingAcquires.observe(labels, connPool.numPendingAcquires())
knexPendingCreates.observe(labels, connPool.numPendingCreates())
knexPendingValidations.observe(labels, connPool.numPendingValidations())
knexRemainingCapacity.observe(labels, numberOfFreeConnections(knex))
}
}
}
6 changes: 3 additions & 3 deletions packages/server/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import promBundle from 'express-prom-bundle'

import { initKnexPrometheusMetrics } from '@/logging/knexMonitoring'
import { initHighFrequencyMonitoring } from '@/logging/highFrequencyMetrics/highfrequencyMonitoring'
import knex from '@/db/knex'
import { highFrequencyMetricsCollectionPeriodMs } from '@/modules/shared/helpers/envHelper'
import { startupLogger as logger } from '@/logging/logging'
import type express from 'express'
import { getAllClients } from '@/modules/multiregion/dbSelector'

let prometheusInitialized = false

Expand All @@ -24,14 +24,14 @@ export default function (app: express.Express) {
register: prometheusClient.register,
collectionPeriodMilliseconds: highFrequencyMetricsCollectionPeriodMs(),
config: {
getDbClients: getAllClients
knex
}
})
highfrequencyMonitoring.start()

initKnexPrometheusMetrics({
register: prometheusClient.register,
getAllDbClients: getAllClients,
db: knex,
logger
})
const expressMetricsMiddleware = promBundle({
Expand Down
Loading

0 comments on commit e8c3450

Please sign in to comment.