Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
postgres/archiver: replace in-query bytes conversion to conversion wh…
Browse files Browse the repository at this point in the history
…en updating metric.
  • Loading branch information
lesovsky committed May 15, 2021
1 parent 0cdbf65 commit d4cedb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions internal/collector/postgres_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const walArchivingQuery = "SELECT archived_count, failed_count, " +
"extract(epoch from now() - last_archived_time) AS since_last_archive_seconds, " +
"(SELECT count(*) * (SELECT setting FROM pg_settings WHERE name = 'wal_segment_size')::int FROM pg_ls_archive_statusdir() WHERE name ~'.ready') AS lag_bytes " +
"(SELECT count(*) FROM pg_ls_archive_statusdir() WHERE name ~'.ready') AS lag_files " +
"FROM pg_stat_archiver WHERE archived_count > 0"

type postgresWalArchivingCollector struct {
Expand Down Expand Up @@ -83,7 +83,7 @@ func (c *postgresWalArchivingCollector) Update(config Config, ch chan<- promethe
ch <- c.archived.mustNewConstMetric(stats.archived)
ch <- c.failed.mustNewConstMetric(stats.failed)
ch <- c.sinceArchivedSeconds.mustNewConstMetric(stats.sinceArchivedSeconds)
ch <- c.archivingLag.mustNewConstMetric(stats.lagBytes)
ch <- c.archivingLag.mustNewConstMetric(stats.lagFiles * float64(config.WalSegmentSize))

return nil
}
Expand All @@ -93,7 +93,7 @@ type postgresWalArchivingStat struct {
archived float64
failed float64
sinceArchivedSeconds float64
lagBytes float64
lagFiles float64
}

// parsePostgresWalArchivingStats parses PGResult, extract data and return struct with stats values.
Expand Down Expand Up @@ -125,8 +125,8 @@ func parsePostgresWalArchivingStats(r *model.PGResult) postgresWalArchivingStat
stats.failed = v
case "since_last_archive_seconds":
stats.sinceArchivedSeconds = v
case "lag_bytes":
stats.lagBytes = v
case "lag_files":
stats.lagFiles = v
default:
log.Debugf("unsupported pg_stat_archiver stat column: %s, skip", string(colname.Name))
continue
Expand Down
8 changes: 4 additions & 4 deletions internal/collector/postgres_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func Test_parsePostgresWalArchivingStats(t *testing.T) {
Ncols: 4,
Colnames: []pgproto3.FieldDescription{
{Name: []byte("archived_count")}, {Name: []byte("failed_count")},
{Name: []byte("since_last_archive_seconds")}, {Name: []byte("lag_bytes")},
{Name: []byte("since_last_archive_seconds")}, {Name: []byte("lag_files")},
},
Rows: [][]sql.NullString{
{
{String: "4587", Valid: true}, {String: "0", Valid: true},
{String: "17", Valid: true}, {String: "12345678", Valid: true},
{String: "17", Valid: true}, {String: "159", Valid: true},
},
},
},
want: postgresWalArchivingStat{archived: 4587, failed: 0, sinceArchivedSeconds: 17, lagBytes: 12345678},
want: postgresWalArchivingStat{archived: 4587, failed: 0, sinceArchivedSeconds: 17, lagFiles: 159},
},
{
name: "no rows output",
Expand All @@ -59,7 +59,7 @@ func Test_parsePostgresWalArchivingStats(t *testing.T) {
},
Rows: [][]sql.NullString{},
},
want: postgresWalArchivingStat{archived: 0, failed: 0, sinceArchivedSeconds: 0, lagBytes: 0},
want: postgresWalArchivingStat{archived: 0, failed: 0, sinceArchivedSeconds: 0, lagFiles: 0},
},
}

Expand Down

0 comments on commit d4cedb2

Please sign in to comment.