From 29dda0aea0ea2fc7aa9a973b015578656dd1aec3 Mon Sep 17 00:00:00 2001 From: SuperQ Date: Tue, 27 Jun 2023 16:05:31 +0200 Subject: [PATCH] Cleanup collectors Fix up `replication` and `process_idle` Update input params to match the rest of the collectors. Signed-off-by: SuperQ --- collector/pg_process_idle.go | 4 ++-- collector/pg_replication.go | 4 ++-- collector/pg_replication_test.go | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/collector/pg_process_idle.go b/collector/pg_process_idle.go index cc53fbb73..4f4aa4cdd 100644 --- a/collector/pg_process_idle.go +++ b/collector/pg_process_idle.go @@ -43,8 +43,8 @@ var pgProcessIdleSeconds = prometheus.NewDesc( prometheus.Labels{}, ) -func (PGProcessIdleCollector) Update(ctx context.Context, inst *instance, ch chan<- prometheus.Metric) error { - db := inst.getDB() +func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { + db := instance.getDB() row := db.QueryRowContext(ctx, `WITH metrics AS ( diff --git a/collector/pg_replication.go b/collector/pg_replication.go index 1a8a3569f..ee92e74fc 100644 --- a/collector/pg_replication.go +++ b/collector/pg_replication.go @@ -15,7 +15,6 @@ package collector import ( "context" - "database/sql" "github.com/prometheus/client_golang/prometheus" ) @@ -64,7 +63,8 @@ var ( END as is_replica` ) -func (c *PGReplicationCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { +func (c *PGReplicationCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { + db := instance.getDB() row := db.QueryRowContext(ctx, pgReplicationQuery, ) diff --git a/collector/pg_replication_test.go b/collector/pg_replication_test.go index 4d240cdf3..b6df698e3 100644 --- a/collector/pg_replication_test.go +++ b/collector/pg_replication_test.go @@ -29,6 +29,8 @@ func TestPgReplicationCollector(t *testing.T) { } defer db.Close() + inst := &instance{db: db} + columns := []string{"lag", "is_replica"} rows := sqlmock.NewRows(columns). AddRow(1000, 1) @@ -39,7 +41,7 @@ func TestPgReplicationCollector(t *testing.T) { defer close(ch) c := PGReplicationCollector{} - if err := c.Update(context.Background(), db, ch); err != nil { + if err := c.Update(context.Background(), inst, ch); err != nil { t.Errorf("Error calling PGReplicationCollector.Update: %s", err) } }()