-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): more metrics related to replications
- Loading branch information
1 parent
efe2416
commit 4261e26
Showing
7 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/monitor-deployment/src/observability/metrics/dbWorkers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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 dbWorkers = new prometheusClient.Gauge({ | ||
name: join([namePrefix, 'db_workers'], '_'), | ||
help: 'Number of database workers', | ||
labelNames: ['region', ...labelNames] | ||
}) | ||
return async (params) => { | ||
const { dbClients, labels } = params | ||
await Promise.all( | ||
dbClients.map(async ({ client, regionKey }) => { | ||
const connectionResults = await client.raw<{ | ||
rows: [{ worker_count: string }] | ||
}>(`SELECT COUNT(*) AS worker_count FROM pg_stat_activity;`) | ||
if (!connectionResults.rows.length) { | ||
logger.error( | ||
{ region: regionKey }, | ||
"No database workers found for region '{region}'. This is odd." | ||
) | ||
return | ||
} | ||
dbWorkers.set( | ||
{ ...labels, region: regionKey }, | ||
parseInt(connectionResults.rows[0].worker_count) | ||
) | ||
}) | ||
) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/monitor-deployment/src/observability/metrics/dbWorkersAwaitingLocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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 promMetric = new prometheusClient.Gauge({ | ||
name: join([namePrefix, 'db_workers_awaiting_locks'], '_'), | ||
help: 'Number of database workers awaiting locks', | ||
labelNames: ['region', ...labelNames] | ||
}) | ||
return async (params) => { | ||
const { dbClients, labels } = params | ||
await Promise.all( | ||
dbClients.map(async ({ client, regionKey }) => { | ||
const queryResults = await client.raw<{ | ||
rows: [{ count: string }] | ||
}>(`SELECT COUNT(*) FROM pg_stat_activity WHERE wait_event = 'Lock';`) | ||
if (!queryResults.rows.length) { | ||
logger.error( | ||
{ region: regionKey }, | ||
"No database workers found for region '{region}'. This is odd." | ||
) | ||
return | ||
} | ||
promMetric.set( | ||
{ ...labels, region: regionKey }, | ||
parseInt(queryResults.rows[0].count) | ||
) | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/monitor-deployment/src/observability/metrics/replicationSlotLag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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 promMetric = new prometheusClient.Gauge({ | ||
name: join([namePrefix, 'db_replication_slot_lag'], '_'), | ||
help: 'Lag of replication slots in bytes', | ||
labelNames: ['region', ...labelNames] | ||
}) | ||
return async (params) => { | ||
const { dbClients, labels } = params | ||
await Promise.all( | ||
dbClients.map(async ({ client, regionKey }) => { | ||
const queryResults = await client.raw<{ | ||
rows: [{ slot_lag_bytes: number }] | ||
}>(` | ||
SELECT pg_current_wal_lsn() - confirmed_flush_lsn AS slot_lag_bytes | ||
FROM pg_replication_slots; | ||
`) | ||
if (!queryResults.rows.length) { | ||
logger.error( | ||
{ region: regionKey }, | ||
"No database replication slots found for region '{region}'. This is odd." | ||
) | ||
return | ||
} | ||
promMetric.set( | ||
{ ...labels, region: regionKey }, | ||
queryResults.rows[0].slot_lag_bytes | ||
) | ||
}) | ||
) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/monitor-deployment/src/observability/metrics/replicationWorkerLag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 promMetric = new prometheusClient.Gauge({ | ||
name: join([namePrefix, 'db_replication_worker_lag'], '_'), | ||
help: 'Lag of replication workers, by type of lag', | ||
labelNames: ['region', 'lagtype', ...labelNames] | ||
}) | ||
return async (params) => { | ||
const { dbClients, labels } = params | ||
await Promise.all( | ||
dbClients.map(async ({ client, regionKey }) => { | ||
const queryResults = await client.raw<{ | ||
rows: [{ write_lag: number; flush_lag: number; replay_lag: number }] | ||
}>(` | ||
SELECT write_lsn - sent_lsn AS write_lag, | ||
flush_lsn - write_lsn AS flush_lag, | ||
replay_lsn - flush_lsn AS replay_lag | ||
FROM pg_stat_replication; | ||
`) | ||
if (!queryResults.rows.length) { | ||
logger.error( | ||
{ region: regionKey }, | ||
"No database workers found for region '{region}'. This is odd." | ||
) | ||
return | ||
} | ||
promMetric.set( | ||
{ ...labels, region: regionKey, lagtype: 'write' }, | ||
queryResults.rows[0].write_lag | ||
) | ||
promMetric.set( | ||
{ ...labels, region: regionKey, lagtype: 'flush' }, | ||
queryResults.rows[0].flush_lag | ||
) | ||
promMetric.set( | ||
{ ...labels, region: regionKey, lagtype: 'replay' }, | ||
queryResults.rows[0].replay_lag | ||
) | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters