Skip to content

Commit cd1ae8c

Browse files
df7cbtbe
authored andcommitted
WIP: Mark new PG14 columns nullable
TODO: SessionTime, ActiveTime, and IdleInTransactionTime do not work on PG14; ChecksumLastFailure needs a proper datatype.
1 parent 36246b5 commit cd1ae8c

File tree

4 files changed

+112
-106
lines changed

4 files changed

+112
-106
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
bin/
33
pg-exporter
44
pid
5+
regression.diffs
6+
regression.out
57
results/

collector/models/pgStatDatabase.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"database/sql"
45
"time"
56

67
"github.com/uptrace/bun"
@@ -9,32 +10,32 @@ import (
910
// +metric=slice
1011
type PgStatDatabase struct {
1112
bun.BaseModel `bun:"pg_stat_database"`
12-
DatID int64 `bun:"datid" help:"OID of a database" metric:"database_id,type:label"`
13-
DatName string `bun:"datname" help:"Name of this database" metric:"database,type:label"`
14-
NumBackends int `bun:"numbackends" help:"Number of backends currently connected to this database" metric:"backends,type:gauge"`
15-
XactCommit int64 `bun:"xact_commit" help:"Number of transactions in this database that have been committed" metric:"xact_commited_total"`
16-
XactRollback int64 `bun:"xact_rollback" help:"Number of transactions in this database that have been rolled back" metric:"xact_rolledback_total"`
17-
BlksRead int64 `bun:"blks_read" help:"Number of disk blocks read in this database" metric:"blocks_read_total"`
18-
BlksHit int64 `bun:"blks_hit" help:"Number of times disk blocks were found already in the buffer cache, so that a read was not necessary" metric:"blocks_hit_total"`
19-
TupReturned int64 `bun:"tup_returned" help:"Number of rows returned by queries in this database" metric:"rows_returned_total"`
20-
TupFetched int64 `bun:"tup_fetched" help:"Number of rows fetched by queries in this database" metric:"rows_fetched_total"`
21-
TupInserted int64 `bun:"tup_inserted" help:"Number of rows inserted by queries in this database" metric:"rows_inserted_total"`
22-
TupUpdated int64 `bun:"tup_updated" help:"Number of rows updated by queries in this database" metric:"rows_updated_total"`
23-
TupDeleted int64 `bun:"tup_deleted" help:"Number of rows deleted by queries in this database" metric:"rows_deleted_total"`
24-
Conflicts int64 `bun:"conflicts" help:"Number of queries canceled due to conflicts with recovery in this database" metric:"conflicts_total"`
25-
TempFiles int64 `bun:"temp_files" help:"Number of temporary files created by queries in this database" metric:"temp_files_total"`
26-
TempBytes int64 `bun:"temp_bytes" help:"Total amount of data written to temporary files by queries in this database" metric:"temp_bytes_total"`
27-
Deadlocks int64 `bun:"deadlocks" help:"Number of deadlocks detected in this database" metric:"deadlocks_total"`
28-
ChecksumFailures int64 `bun:"checksum_failures" help:"Number of data page checksum failures detected in this database" metric:"checksum_failures_count"` // new in PG12
29-
ChecksumLastFailure time.Time `bun:"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
30-
BlkReadTime Milliseconds `bun:"blk_read_time" help:"Time spent reading data file blocks by backends in this database" metric:"blk_read_seconds_total"`
31-
BlkWriteTime Milliseconds `bun:"blk_write_time" help:"Time spent writing data file blocks by backends in this database" metric:"blk_write_seconds_total"`
32-
SessionTime Milliseconds `bun:"session_time" help:"Time spent by database sessions in this database, in milliseconds" metric:"session_time_total"` // new in PG14
33-
ActiveTime Milliseconds `bun:"active_time" help:"Time spent executing SQL statements in this database, in milliseconds" metric:"active_time_total"` // new in PG14
34-
IdleInTransactionTime Milliseconds `bun:"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
35-
Sessions int64 `bun:"sessions" help:"Total number of sessions established to this database" metric:"sessions_count"` // new in PG14
36-
SessionsAbandoned int64 `bun:"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
37-
SessionsFatal int64 `bun:"sessions_fatal" help:"Number of database sessions to this database that were terminated by fatal errors" metric:"sessions_fatal_count"` // new in PG14
38-
SessionsKilled int64 `bun:"sessions_killed" help:"Number of database sessions to this database that were terminated by operator intervention" metric:"sessions_killed_count"` // new in PG14
39-
StatsReset time.Time `bun:"stats_reset" help:"Time at which these statistics were last reset"`
13+
DatID int64 `bun:"datid" help:"OID of a database" metric:"database_id,type:label"`
14+
DatName string `bun:"datname" help:"Name of this database" metric:"database,type:label"`
15+
NumBackends int `bun:"numbackends" help:"Number of backends currently connected to this database" metric:"backends,type:gauge"`
16+
XactCommit int64 `bun:"xact_commit" help:"Number of transactions in this database that have been committed" metric:"xact_commited_total"`
17+
XactRollback int64 `bun:"xact_rollback" help:"Number of transactions in this database that have been rolled back" metric:"xact_rolledback_total"`
18+
BlksRead int64 `bun:"blks_read" help:"Number of disk blocks read in this database" metric:"blocks_read_total"`
19+
BlksHit int64 `bun:"blks_hit" help:"Number of times disk blocks were found already in the buffer cache, so that a read was not necessary" metric:"blocks_hit_total"`
20+
TupReturned int64 `bun:"tup_returned" help:"Number of rows returned by queries in this database" metric:"rows_returned_total"`
21+
TupFetched int64 `bun:"tup_fetched" help:"Number of rows fetched by queries in this database" metric:"rows_fetched_total"`
22+
TupInserted int64 `bun:"tup_inserted" help:"Number of rows inserted by queries in this database" metric:"rows_inserted_total"`
23+
TupUpdated int64 `bun:"tup_updated" help:"Number of rows updated by queries in this database" metric:"rows_updated_total"`
24+
TupDeleted int64 `bun:"tup_deleted" help:"Number of rows deleted by queries in this database" metric:"rows_deleted_total"`
25+
Conflicts int64 `bun:"conflicts" help:"Number of queries canceled due to conflicts with recovery in this database" metric:"conflicts_total"`
26+
TempFiles int64 `bun:"temp_files" help:"Number of temporary files created by queries in this database" metric:"temp_files_total"`
27+
TempBytes int64 `bun:"temp_bytes" help:"Total amount of data written to temporary files by queries in this database" metric:"temp_bytes_total"`
28+
Deadlocks int64 `bun:"deadlocks" help:"Number of deadlocks detected in this database" metric:"deadlocks_total"`
29+
ChecksumFailures sql.NullInt64 `bun:"checksum_failures" help:"Number of data page checksum failures detected in this database" metric:"checksum_failures_count"` // new in PG12
30+
ChecksumLastFailure sql.NullTime `bun:"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
31+
BlkReadTime Milliseconds `bun:"blk_read_time" help:"Time spent reading data file blocks by backends in this database" metric:"blk_read_seconds_total"`
32+
BlkWriteTime Milliseconds `bun:"blk_write_time" help:"Time spent writing data file blocks by backends in this database" metric:"blk_write_seconds_total"`
33+
SessionTime NullMilliseconds `bun:"session_time" help:"Time spent by database sessions in this database, in milliseconds" metric:"session_time_total"` // new in PG14
34+
ActiveTime NullMilliseconds `bun:"active_time" help:"Time spent executing SQL statements in this database, in milliseconds" metric:"active_time_total"` // new in PG14
35+
IdleInTransactionTime NullMilliseconds `bun:"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
36+
Sessions sql.NullInt64 `bun:"sessions" help:"Total number of sessions established to this database" metric:"sessions_count"` // new in PG14
37+
SessionsAbandoned sql.NullInt64 `bun:"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
38+
SessionsFatal sql.NullInt64 `bun:"sessions_fatal" help:"Number of database sessions to this database that were terminated by fatal errors" metric:"sessions_fatal_count"` // new in PG14
39+
SessionsKilled sql.NullInt64 `bun:"sessions_killed" help:"Number of database sessions to this database that were terminated by operator intervention" metric:"sessions_killed_count"` // new in PG14
40+
StatsReset time.Time `bun:"stats_reset" help:"Time at which these statistics were last reset"`
4041
}

collector/models/pgStatDatabase_gen.go

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,6 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
139139
), prometheus.CounterValue, deadlocksTotal,
140140
)
141141

142-
// checksum_failures_count (CounterValue)
143-
checksumFailuresCount := float64(r.ChecksumFailures)
144-
ch <- prometheus.MustNewConstMetric(
145-
prometheus.NewDesc(
146-
prometheus.BuildFQName(namespace, subsystem, `checksum_failures_count`), `Number of data page checksum failures detected in this database`, nil, labels,
147-
), prometheus.CounterValue, checksumFailuresCount,
148-
)
149-
150-
// checksum_last_failure (CounterValue)
151-
var checksumLastFailure float64
152-
if r.ChecksumLastFailure.IsZero() {
153-
checksumLastFailure = float64(0)
154-
} else {
155-
checksumLastFailure = float64(r.ChecksumLastFailure.Unix())
156-
}
157-
ch <- prometheus.MustNewConstMetric(
158-
prometheus.NewDesc(
159-
prometheus.BuildFQName(namespace, subsystem, `checksum_last_failure`), `Time at which the last data page checksum failure was detected in this database`, nil, labels,
160-
), prometheus.CounterValue, checksumLastFailure,
161-
)
162-
163142
// blk_read_seconds_total (CounterValue)
164143
blkReadSecondsTotal := r.BlkReadTime.Seconds()
165144
ch <- prometheus.MustNewConstMetric(
@@ -176,62 +155,6 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
176155
), prometheus.CounterValue, blkWriteSecondsTotal,
177156
)
178157

179-
// session_time_total (CounterValue)
180-
sessionTimeTotal := r.SessionTime.Seconds()
181-
ch <- prometheus.MustNewConstMetric(
182-
prometheus.NewDesc(
183-
prometheus.BuildFQName(namespace, subsystem, `session_time_total`), `Time spent by database sessions in this database, in milliseconds`, nil, labels,
184-
), prometheus.CounterValue, sessionTimeTotal,
185-
)
186-
187-
// active_time_total (CounterValue)
188-
activeTimeTotal := r.ActiveTime.Seconds()
189-
ch <- prometheus.MustNewConstMetric(
190-
prometheus.NewDesc(
191-
prometheus.BuildFQName(namespace, subsystem, `active_time_total`), `Time spent executing SQL statements in this database, in milliseconds`, nil, labels,
192-
), prometheus.CounterValue, activeTimeTotal,
193-
)
194-
195-
// idle_in_transaction_time_total (CounterValue)
196-
idleInTransactionTimeTotal := r.IdleInTransactionTime.Seconds()
197-
ch <- prometheus.MustNewConstMetric(
198-
prometheus.NewDesc(
199-
prometheus.BuildFQName(namespace, subsystem, `idle_in_transaction_time_total`), `Time spent idling while in a transaction in this database, in milliseconds`, nil, labels,
200-
), prometheus.CounterValue, idleInTransactionTimeTotal,
201-
)
202-
203-
// sessions_count (CounterValue)
204-
sessionsCount := float64(r.Sessions)
205-
ch <- prometheus.MustNewConstMetric(
206-
prometheus.NewDesc(
207-
prometheus.BuildFQName(namespace, subsystem, `sessions_count`), `Total number of sessions established to this database`, nil, labels,
208-
), prometheus.CounterValue, sessionsCount,
209-
)
210-
211-
// sessions_abandoned_count (CounterValue)
212-
sessionsAbandonedCount := float64(r.SessionsAbandoned)
213-
ch <- prometheus.MustNewConstMetric(
214-
prometheus.NewDesc(
215-
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,
216-
), prometheus.CounterValue, sessionsAbandonedCount,
217-
)
218-
219-
// sessions_fatal_count (CounterValue)
220-
sessionsFatalCount := float64(r.SessionsFatal)
221-
ch <- prometheus.MustNewConstMetric(
222-
prometheus.NewDesc(
223-
prometheus.BuildFQName(namespace, subsystem, `sessions_fatal_count`), `Number of database sessions to this database that were terminated by fatal errors`, nil, labels,
224-
), prometheus.CounterValue, sessionsFatalCount,
225-
)
226-
227-
// sessions_killed_count (CounterValue)
228-
sessionsKilledCount := float64(r.SessionsKilled)
229-
ch <- prometheus.MustNewConstMetric(
230-
prometheus.NewDesc(
231-
prometheus.BuildFQName(namespace, subsystem, `sessions_killed_count`), `Number of database sessions to this database that were terminated by operator intervention`, nil, labels,
232-
), prometheus.CounterValue, sessionsKilledCount,
233-
)
234-
235158
// stats_reset (CounterValue)
236159
var statsReset float64
237160
if r.StatsReset.IsZero() {
@@ -246,6 +169,86 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
246169
)
247170

248171
// optional metrics
172+
// checksum_failures_count (CounterValue)
173+
if r.ChecksumFailures.Valid {
174+
checksumFailuresCount := float64(r.ChecksumFailures.Int64)
175+
176+
ch <- prometheus.MustNewConstMetric(
177+
prometheus.NewDesc(
178+
prometheus.BuildFQName(namespace, subsystem, `checksum_failures_count`), `Number of data page checksum failures detected in this database`, nil, labels,
179+
), prometheus.CounterValue, checksumFailuresCount,
180+
)
181+
}
182+
// session_time_total (CounterValue)
183+
if r.SessionTime.Valid {
184+
sessionTimeTotal := r.SessionTime.Seconds()
185+
186+
ch <- prometheus.MustNewConstMetric(
187+
prometheus.NewDesc(
188+
prometheus.BuildFQName(namespace, subsystem, `session_time_total`), `Time spent by database sessions in this database, in milliseconds`, nil, labels,
189+
), prometheus.CounterValue, sessionTimeTotal,
190+
)
191+
}
192+
// active_time_total (CounterValue)
193+
if r.ActiveTime.Valid {
194+
activeTimeTotal := r.ActiveTime.Seconds()
195+
196+
ch <- prometheus.MustNewConstMetric(
197+
prometheus.NewDesc(
198+
prometheus.BuildFQName(namespace, subsystem, `active_time_total`), `Time spent executing SQL statements in this database, in milliseconds`, nil, labels,
199+
), prometheus.CounterValue, activeTimeTotal,
200+
)
201+
}
202+
// idle_in_transaction_time_total (CounterValue)
203+
if r.IdleInTransactionTime.Valid {
204+
idleInTransactionTimeTotal := r.IdleInTransactionTime.Seconds()
205+
206+
ch <- prometheus.MustNewConstMetric(
207+
prometheus.NewDesc(
208+
prometheus.BuildFQName(namespace, subsystem, `idle_in_transaction_time_total`), `Time spent idling while in a transaction in this database, in milliseconds`, nil, labels,
209+
), prometheus.CounterValue, idleInTransactionTimeTotal,
210+
)
211+
}
212+
// sessions_count (CounterValue)
213+
if r.Sessions.Valid {
214+
sessionsCount := float64(r.Sessions.Int64)
215+
216+
ch <- prometheus.MustNewConstMetric(
217+
prometheus.NewDesc(
218+
prometheus.BuildFQName(namespace, subsystem, `sessions_count`), `Total number of sessions established to this database`, nil, labels,
219+
), prometheus.CounterValue, sessionsCount,
220+
)
221+
}
222+
// sessions_abandoned_count (CounterValue)
223+
if r.SessionsAbandoned.Valid {
224+
sessionsAbandonedCount := float64(r.SessionsAbandoned.Int64)
225+
226+
ch <- prometheus.MustNewConstMetric(
227+
prometheus.NewDesc(
228+
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,
229+
), prometheus.CounterValue, sessionsAbandonedCount,
230+
)
231+
}
232+
// sessions_fatal_count (CounterValue)
233+
if r.SessionsFatal.Valid {
234+
sessionsFatalCount := float64(r.SessionsFatal.Int64)
235+
236+
ch <- prometheus.MustNewConstMetric(
237+
prometheus.NewDesc(
238+
prometheus.BuildFQName(namespace, subsystem, `sessions_fatal_count`), `Number of database sessions to this database that were terminated by fatal errors`, nil, labels,
239+
), prometheus.CounterValue, sessionsFatalCount,
240+
)
241+
}
242+
// sessions_killed_count (CounterValue)
243+
if r.SessionsKilled.Valid {
244+
sessionsKilledCount := float64(r.SessionsKilled.Int64)
245+
246+
ch <- prometheus.MustNewConstMetric(
247+
prometheus.NewDesc(
248+
prometheus.BuildFQName(namespace, subsystem, `sessions_killed_count`), `Number of database sessions to this database that were terminated by operator intervention`, nil, labels,
249+
), prometheus.CounterValue, sessionsKilledCount,
250+
)
251+
}
249252

250253
return nil
251254
}

gen/generator/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func parseModel(t *types.Type) *modelDescription {
9191
case "int64", "int", "time.Time", "float64",
9292
"./collector/models.Milliseconds", "github.com/1and1/pg-exporter/collector/models.Milliseconds":
9393
desc.metrics = append(desc.metrics, field)
94-
case "database/sql.NullInt64", "database/sql.NullFloat64",
94+
case "database/sql.NullInt64", "database/sql.NullFloat64", "database/sql.NullTime",
9595
"./collector/models.NullMilliseconds", "github.com/1and1/pg-exporter/collector/models.NullMilliseconds":
9696
desc.optionalMetrics = append(desc.optionalMetrics, field)
9797
default:

0 commit comments

Comments
 (0)