Skip to content

Commit

Permalink
fixed NullMillisconds and NullTime support
Browse files Browse the repository at this point in the history
  • Loading branch information
tbe committed Jun 15, 2022
1 parent cd1ae8c commit 9bf8198
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
9 changes: 7 additions & 2 deletions collector/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ func (m Milliseconds) Seconds() float64 {
return float64(m) / 1000
}

type NullMilliseconds sql.NullFloat64
type NullMilliseconds struct {
sql.NullFloat64
}

func (m *NullMilliseconds) Scan(src interface{}) error {
return m.NullFloat64.Scan(src)
}

func (m NullMilliseconds) Seconds() float64 {
if m.Valid {
return m.Float64 / 1000
}
return 0.0
}

2 changes: 1 addition & 1 deletion collector/models/pgStatDatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PgStatDatabase struct {
TempBytes int64 `bun:"temp_bytes" help:"Total amount of data written to temporary files by queries in this database" metric:"temp_bytes_total"`
Deadlocks int64 `bun:"deadlocks" help:"Number of deadlocks detected in this database" metric:"deadlocks_total"`
ChecksumFailures sql.NullInt64 `bun:"checksum_failures" help:"Number of data page checksum failures detected in this database" metric:"checksum_failures_count"` // new in PG12
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
ChecksumLastFailure bun.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
BlkReadTime Milliseconds `bun:"blk_read_time" help:"Time spent reading data file blocks by backends in this database" metric:"blk_read_seconds_total"`
BlkWriteTime Milliseconds `bun:"blk_write_time" help:"Time spent writing data file blocks by backends in this database" metric:"blk_write_seconds_total"`
SessionTime NullMilliseconds `bun:"session_time" help:"Time spent by database sessions in this database, in milliseconds" metric:"session_time_total"` // new in PG14
Expand Down
10 changes: 10 additions & 0 deletions collector/models/pgStatDatabase_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ func (r *PgStatDatabase) ToMetrics(namespace string, subsystem string, ch chan<-
), prometheus.CounterValue, checksumFailuresCount,
)
}
// checksum_last_failure (CounterValue)
if !r.ChecksumLastFailure.IsZero() {
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,
)
}
// session_time_total (CounterValue)
if r.SessionTime.Valid {
sessionTimeTotal := r.SessionTime.Seconds()
Expand Down
2 changes: 2 additions & 0 deletions gen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func (m *mappingGen) GenerateType(c *generator.Context, t *types.Type, w io.Writ
tmpl = nullFloat64Metric
case "./collector/models.NullMilliseconds", "github.com/1and1/pg-exporter/collector/models.NullMilliseconds":
tmpl = nullMillisecondsMetric
case "github.com/uptrace/bun/schema.NullTime":
tmpl = nullTimeMetric
}
tmpl += "\n"
sw.Do(tmpl, metric)
Expand Down
2 changes: 1 addition & 1 deletion gen/generator/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func parseModel(t *types.Type) *modelDescription {
case "int64", "int", "time.Time", "float64",
"./collector/models.Milliseconds", "github.com/1and1/pg-exporter/collector/models.Milliseconds":
desc.metrics = append(desc.metrics, field)
case "database/sql.NullInt64", "database/sql.NullFloat64", "database/sql.NullTime",
case "database/sql.NullInt64", "database/sql.NullFloat64", "github.com/uptrace/bun/schema.NullTime",
"./collector/models.NullMilliseconds", "github.com/1and1/pg-exporter/collector/models.NullMilliseconds":
desc.optionalMetrics = append(desc.optionalMetrics, field)
default:
Expand Down
3 changes: 3 additions & 0 deletions gen/generator/snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const nullMillisecondsMetric = `if r.$.Field$.Valid {
$.VarName$ := r.$.Field$.Seconds()
`

const nullTimeMetric = `if !r.$.Field$.IsZero() {
$.VarName$ := float64(r.$.Field$.Unix())
`

// sql.NullInt64 metric
const nullInt64Metric = `if r.$.Field$.Valid {
Expand Down

0 comments on commit 9bf8198

Please sign in to comment.