From c4e92f106858ce5b250580602a5d5dcbc6c8b8bc Mon Sep 17 00:00:00 2001
From: SuperQ <superq@gmail.com>
Date: Tue, 13 Jun 2023 17:13:27 +0200
Subject: [PATCH] Fix up collector registration

Use const definitions to make collector registration consistent.
* Use collector subsystem name consistently.
* Fix up replication metric name unit.

Signed-off-by: SuperQ <superq@gmail.com>
---
 collector/pg_database.go                      | 10 +++++++--
 collector/pg_postmaster.go                    | 10 +++++++--
 collector/pg_process_idle.go                  |  6 ++---
 collector/pg_replication.go                   | 16 +++++++++++---
 ...cation_slots.go => pg_replication_slot.go} | 22 +++++++++++++++----
 ...ts_test.go => pg_replication_slot_test.go} |  0
 collector/pg_stat_bgwriter.go                 |  6 ++---
 collector/pg_stat_database.go                 |  6 ++---
 collector/pg_stat_statements.go               |  6 ++---
 collector/pg_stat_user_tables.go              |  6 ++---
 collector/pg_statio_user_tables.go            |  6 ++---
 11 files changed, 65 insertions(+), 29 deletions(-)
 rename collector/{replication_slots.go => pg_replication_slot.go} (84%)
 rename collector/{replication_slots_test.go => pg_replication_slot_test.go} (100%)

diff --git a/collector/pg_database.go b/collector/pg_database.go
index 8a027e2db..661f84cd8 100644
--- a/collector/pg_database.go
+++ b/collector/pg_database.go
@@ -21,8 +21,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const databaseSubsystem = "database"
+
 func init() {
-	registerCollector("database", defaultEnabled, NewPGDatabaseCollector)
+	registerCollector(databaseSubsystem, defaultEnabled, NewPGDatabaseCollector)
 }
 
 type PGDatabaseCollector struct {
@@ -43,7 +45,11 @@ func NewPGDatabaseCollector(config collectorConfig) (Collector, error) {
 
 var (
 	pgDatabaseSizeDesc = prometheus.NewDesc(
-		"pg_database_size_bytes",
+		prometheus.BuildFQName(
+			namespace,
+			databaseSubsystem,
+			"size_bytes",
+		),
 		"Disk space used by the database",
 		[]string{"datname"}, nil,
 	)
diff --git a/collector/pg_postmaster.go b/collector/pg_postmaster.go
index b7d844e18..4a0cec6d4 100644
--- a/collector/pg_postmaster.go
+++ b/collector/pg_postmaster.go
@@ -20,8 +20,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const postmasterSubsystem = "postmaster"
+
 func init() {
-	registerCollector("postmaster", defaultEnabled, NewPGPostmasterCollector)
+	registerCollector(postmasterSubsystem, defaultEnabled, NewPGPostmasterCollector)
 }
 
 type PGPostmasterCollector struct {
@@ -33,7 +35,11 @@ func NewPGPostmasterCollector(collectorConfig) (Collector, error) {
 
 var (
 	pgPostMasterStartTimeSeconds = prometheus.NewDesc(
-		"pg_postmaster_start_time_seconds",
+		prometheus.BuildFQName(
+			namespace,
+			postmasterSubsystem,
+			"start_time_seconds",
+		),
 		"Time at which postmaster started",
 		[]string{}, nil,
 	)
diff --git a/collector/pg_process_idle.go b/collector/pg_process_idle.go
index b8c1a6cb7..8ee65a436 100644
--- a/collector/pg_process_idle.go
+++ b/collector/pg_process_idle.go
@@ -21,16 +21,16 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const processIdleSubsystem = "process_idle"
+
 func init() {
-	registerCollector("statements", defaultEnabled, NewPGProcessIdleCollector)
+	registerCollector(processIdleSubsystem, defaultEnabled, NewPGProcessIdleCollector)
 }
 
 type PGProcessIdleCollector struct {
 	log log.Logger
 }
 
-const processIdleSubsystem = "process_idle"
-
 func NewPGProcessIdleCollector(config collectorConfig) (Collector, error) {
 	return &PGProcessIdleCollector{log: config.logger}, nil
 }
diff --git a/collector/pg_replication.go b/collector/pg_replication.go
index 10e4de521..1a8a3569f 100644
--- a/collector/pg_replication.go
+++ b/collector/pg_replication.go
@@ -20,8 +20,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const replicationSubsystem = "replication"
+
 func init() {
-	registerCollector("replication", defaultEnabled, NewPGReplicationCollector)
+	registerCollector(replicationSubsystem, defaultEnabled, NewPGReplicationCollector)
 }
 
 type PGReplicationCollector struct {
@@ -33,12 +35,20 @@ func NewPGReplicationCollector(collectorConfig) (Collector, error) {
 
 var (
 	pgReplicationLag = prometheus.NewDesc(
-		"pg_replication_lag",
+		prometheus.BuildFQName(
+			namespace,
+			replicationSubsystem,
+			"lag_seconds",
+		),
 		"Replication lag behind master in seconds",
 		[]string{}, nil,
 	)
 	pgReplicationIsReplica = prometheus.NewDesc(
-		"pg_replication_is_replica",
+		prometheus.BuildFQName(
+			namespace,
+			replicationSubsystem,
+			"is_replica",
+		),
 		"Indicates if the server is a replica",
 		[]string{}, nil,
 	)
diff --git a/collector/replication_slots.go b/collector/pg_replication_slot.go
similarity index 84%
rename from collector/replication_slots.go
rename to collector/pg_replication_slot.go
index 0aaf39cdc..8f105ff49 100644
--- a/collector/replication_slots.go
+++ b/collector/pg_replication_slot.go
@@ -21,8 +21,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const replicationSlotSubsystem = "replication_slot"
+
 func init() {
-	registerCollector("replication_slot", defaultEnabled, NewPGReplicationSlotCollector)
+	registerCollector(replicationSlotSubsystem, defaultEnabled, NewPGReplicationSlotCollector)
 }
 
 type PGReplicationSlotCollector struct {
@@ -35,17 +37,29 @@ func NewPGReplicationSlotCollector(config collectorConfig) (Collector, error) {
 
 var (
 	pgReplicationSlotCurrentWalDesc = prometheus.NewDesc(
-		"pg_replication_slot_current_wal_lsn",
+		prometheus.BuildFQName(
+			namespace,
+			replicationSlotSubsystem,
+			"slot_current_wal_lsn",
+		),
 		"current wal lsn value",
 		[]string{"slot_name"}, nil,
 	)
 	pgReplicationSlotCurrentFlushDesc = prometheus.NewDesc(
-		"pg_replication_slot_confirmed_flush_lsn",
+		prometheus.BuildFQName(
+			namespace,
+			replicationSlotSubsystem,
+			"slot_confirmed_flush_lsn",
+		),
 		"last lsn confirmed flushed to the replication slot",
 		[]string{"slot_name"}, nil,
 	)
 	pgReplicationSlotIsActiveDesc = prometheus.NewDesc(
-		"pg_replication_slot_is_active",
+		prometheus.BuildFQName(
+			namespace,
+			replicationSlotSubsystem,
+			"slot_is_active",
+		),
 		"whether the replication slot is active or not",
 		[]string{"slot_name"}, nil,
 	)
diff --git a/collector/replication_slots_test.go b/collector/pg_replication_slot_test.go
similarity index 100%
rename from collector/replication_slots_test.go
rename to collector/pg_replication_slot_test.go
diff --git a/collector/pg_stat_bgwriter.go b/collector/pg_stat_bgwriter.go
index 6efe87d52..5daf606c9 100644
--- a/collector/pg_stat_bgwriter.go
+++ b/collector/pg_stat_bgwriter.go
@@ -21,8 +21,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const bgWriterSubsystem = "stat_bgwriter"
+
 func init() {
-	registerCollector("bgwriter", defaultEnabled, NewPGStatBGWriterCollector)
+	registerCollector(bgWriterSubsystem, defaultEnabled, NewPGStatBGWriterCollector)
 }
 
 type PGStatBGWriterCollector struct {
@@ -32,8 +34,6 @@ func NewPGStatBGWriterCollector(collectorConfig) (Collector, error) {
 	return &PGStatBGWriterCollector{}, nil
 }
 
-const bgWriterSubsystem = "stat_bgwriter"
-
 var (
 	statBGWriterCheckpointsTimedDesc = prometheus.NewDesc(
 		prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"),
diff --git a/collector/pg_stat_database.go b/collector/pg_stat_database.go
index 705ac0ce0..346ed9ea9 100644
--- a/collector/pg_stat_database.go
+++ b/collector/pg_stat_database.go
@@ -20,8 +20,10 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const statDatabaseSubsystem = "stat_database"
+
 func init() {
-	registerCollector("stat_database", defaultEnabled, NewPGStatDatabaseCollector)
+	registerCollector(statDatabaseSubsystem, defaultEnabled, NewPGStatDatabaseCollector)
 }
 
 type PGStatDatabaseCollector struct{}
@@ -30,8 +32,6 @@ func NewPGStatDatabaseCollector(config collectorConfig) (Collector, error) {
 	return &PGStatDatabaseCollector{}, nil
 }
 
-const statDatabaseSubsystem = "stat_database"
-
 var (
 	statDatabaseNumbackends = prometheus.NewDesc(
 		prometheus.BuildFQName(
diff --git a/collector/pg_stat_statements.go b/collector/pg_stat_statements.go
index 78485f619..23e1f1567 100644
--- a/collector/pg_stat_statements.go
+++ b/collector/pg_stat_statements.go
@@ -21,19 +21,19 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const statStatementsSubsystem = "stat_statements"
+
 func init() {
 	// WARNING:
 	//   Disabled by default because this set of metrics can be quite expensive on a busy server
 	//   Every unique query will cause a new timeseries to be created
-	registerCollector("statements", defaultDisabled, NewPGStatStatementsCollector)
+	registerCollector(statStatementsSubsystem, defaultDisabled, NewPGStatStatementsCollector)
 }
 
 type PGStatStatementsCollector struct {
 	log log.Logger
 }
 
-const statStatementsSubsystem = "stat_statements"
-
 func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) {
 	return &PGStatStatementsCollector{log: config.logger}, nil
 }
diff --git a/collector/pg_stat_user_tables.go b/collector/pg_stat_user_tables.go
index 1f267a7ee..05aced91f 100644
--- a/collector/pg_stat_user_tables.go
+++ b/collector/pg_stat_user_tables.go
@@ -22,16 +22,16 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const userTableSubsystem = "stat_user_tables"
+
 func init() {
-	registerCollector("user_tables", defaultEnabled, NewPGStatUserTablesCollector)
+	registerCollector(userTableSubsystem, defaultEnabled, NewPGStatUserTablesCollector)
 }
 
 type PGStatUserTablesCollector struct {
 	log log.Logger
 }
 
-const userTableSubsystem = "stat_user_tables"
-
 func NewPGStatUserTablesCollector(config collectorConfig) (Collector, error) {
 	return &PGStatUserTablesCollector{log: config.logger}, nil
 }
diff --git a/collector/pg_statio_user_tables.go b/collector/pg_statio_user_tables.go
index e84631df5..043433d86 100644
--- a/collector/pg_statio_user_tables.go
+++ b/collector/pg_statio_user_tables.go
@@ -21,16 +21,16 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 )
 
+const statioUserTableSubsystem = "statio_user_tables"
+
 func init() {
-	registerCollector("statio_user_tables", defaultEnabled, NewPGStatIOUserTablesCollector)
+	registerCollector(statioUserTableSubsystem, defaultEnabled, NewPGStatIOUserTablesCollector)
 }
 
 type PGStatIOUserTablesCollector struct {
 	log log.Logger
 }
 
-const statioUserTableSubsystem = "statio_user_tables"
-
 func NewPGStatIOUserTablesCollector(config collectorConfig) (Collector, error) {
 	return &PGStatIOUserTablesCollector{log: config.logger}, nil
 }