Skip to content

Commit

Permalink
Add new fields from pg_stat_database
Browse files Browse the repository at this point in the history
  • Loading branch information
df7cb committed Jun 8, 2022
1 parent 79adac4 commit 01a9fee
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
11 changes: 10 additions & 1 deletion collector/models/pgStatDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// +metric=slice
type PgStatDatabase struct {
tableName struct{} `pg:"pg_stat_database"`
tableName struct{} `pg:"pg_stat_database,discard_unknown_columns"`
DatID int64 `pg:"datid" help:"OID of a database" metric:"database_id,type:label"`
DatName string `pg:"datname" help:"Name of this database" metric:"database,type:label"`
NumBackends int `pg:"numbackends" help:"Number of backends currently connected to this database" metric:"backends,type:gauge"`
Expand All @@ -23,7 +23,16 @@ type PgStatDatabase struct {
TempFiles int64 `pg:"temp_files" help:"Number of temporary files created by queries in this database" metric:"temp_files_total"`
TempBytes int64 `pg:"temp_bytes" help:"Total amount of data written to temporary files by queries in this database" metric:"temp_bytes_total"`
Deadlocks int64 `pg:"deadlocks" help:"Number of deadlocks detected in this database" metric:"deadlocks_total"`
checksumFailures int64 `pg:"checksum_failures" help:"Number of data page checksum failures detected in this database" metric:"checksum_failures_count"` // new in PG12
checksumLastFailure time.Time `pg:"checksum_last_failure" help:"Time at which the last data page checksum failure was detected in this database" metric:"checksum_last_failure"` // new in PG12
BlkReadTime Milliseconds `pg:"blk_read_time" help:"Time spent reading data file blocks by backends in this database" metric:"blk_read_seconds_total"`
BlkWriteTime Milliseconds `pg:"blk_write_time" help:"Time spent writing data file blocks by backends in this database" metric:"blk_write_seconds_total"`
sessionTime Milliseconds `pg:"session_time" help:"Time spent by database sessions in this database, in milliseconds" metric:"session_time_total"` // new in PG14
activeTime Milliseconds `pg:"active_time" help:"Time spent executing SQL statements in this database, in milliseconds" metric:"active_time_total"` // new in PG14
idleInTransactionTime Milliseconds `pg:"idle_in_transaction_time" help:"Time spent idling while in a transaction in this database, in milliseconds" metric:"idle_in_transaction_time_total"` // new in PG14
sessions int64 `pg:"sessions" help:"Total number of sessions established to this database" metric:"sessions_count"` // new in PG14
sessionsAbandoned int64 `pg:"sessions_abandoned" help:"Number of database sessions to this database that were terminated because connection to the client was lost" metric:"sessions_abandoned_count"` // new in PG14
sessionsFatal int64 `pg:"sessions_fatal" help:"Number of database sessions to this database that were terminated by fatal errors" metric:"sessions_fatal_count"` // new in PG14
sessionsKilled int64 `pg:"sessions_killed" help:"Number of database sessions to this database that were terminated by operator intervention" metric:"sessions_killed_count"` // new in PG14
StatsReset time.Time `pg:"stats_reset" help:"Time at which these statistics were last reset"`
}
77 changes: 77 additions & 0 deletions collector/models/pgStatDatabase_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
), prometheus.CounterValue, deadlocksTotal,
)

// checksum_failures_count (CounterValue)
checksumFailuresCount := float64(r.checksumFailures)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `checksum_failures_count`), `Number of data page checksum failures detected in this database`, nil, labels,
), prometheus.CounterValue, checksumFailuresCount,
)

// checksum_last_failure (CounterValue)
var checksumLastFailure float64
if r.checksumLastFailure.IsZero() {
checksumLastFailure = float64(0)
} else {
checksumLastFailure = float64(r.checksumLastFailure.Unix())
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `checksum_last_failure`), `Time at which the last data page checksum failure was detected in this database`, nil, labels,
), prometheus.CounterValue, checksumLastFailure,
)

// blk_read_seconds_total (CounterValue)
blkReadSecondsTotal := r.BlkReadTime.Seconds()
ch <- prometheus.MustNewConstMetric(
Expand All @@ -155,6 +176,62 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
), prometheus.CounterValue, blkWriteSecondsTotal,
)

// session_time_total (CounterValue)
sessionTimeTotal := r.sessionTime.Seconds()
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `session_time_total`), `Time spent by database sessions in this database, in milliseconds`, nil, labels,
), prometheus.CounterValue, sessionTimeTotal,
)

// active_time_total (CounterValue)
activeTimeTotal := r.activeTime.Seconds()
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `active_time_total`), `Time spent executing SQL statements in this database, in milliseconds`, nil, labels,
), prometheus.CounterValue, activeTimeTotal,
)

// idle_in_transaction_time_total (CounterValue)
idleInTransactionTimeTotal := r.idleInTransactionTime.Seconds()
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `idle_in_transaction_time_total`), `Time spent idling while in a transaction in this database, in milliseconds`, nil, labels,
), prometheus.CounterValue, idleInTransactionTimeTotal,
)

// sessions_count (CounterValue)
sessionsCount := float64(r.sessions)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `sessions_count`), `Total number of sessions established to this database`, nil, labels,
), prometheus.CounterValue, sessionsCount,
)

// sessions_abandoned_count (CounterValue)
sessionsAbandonedCount := float64(r.sessionsAbandoned)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `sessions_abandoned_count`), `Number of database sessions to this database that were terminated because connection to the client was lost`, nil, labels,
), prometheus.CounterValue, sessionsAbandonedCount,
)

// sessions_fatal_count (CounterValue)
sessionsFatalCount := float64(r.sessionsFatal)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `sessions_fatal_count`), `Number of database sessions to this database that were terminated by fatal errors`, nil, labels,
), prometheus.CounterValue, sessionsFatalCount,
)

// sessions_killed_count (CounterValue)
sessionsKilledCount := float64(r.sessionsKilled)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, `sessions_killed_count`), `Number of database sessions to this database that were terminated by operator intervention`, nil, labels,
), prometheus.CounterValue, sessionsKilledCount,
)

// stats_reset (CounterValue)
var statsReset float64
if r.StatsReset.IsZero() {
Expand Down

0 comments on commit 01a9fee

Please sign in to comment.