From 1fbfad3dc98cf838f476aabf6877946443760286 Mon Sep 17 00:00:00 2001
From: Felix Yuan <felix.yuan@reddit.com>
Date: Thu, 29 Jun 2023 15:48:25 -0700
Subject: [PATCH] Skip null metrics and add debug logs

Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
---
 collector/pg_stat_user_indexes.go      | 30 ++++++++++-------
 collector/pg_stat_user_indexes_test.go | 45 --------------------------
 2 files changed, 19 insertions(+), 56 deletions(-)

diff --git a/collector/pg_stat_user_indexes.go b/collector/pg_stat_user_indexes.go
index 865d641e..e16a35e1 100644
--- a/collector/pg_stat_user_indexes.go
+++ b/collector/pg_stat_user_indexes.go
@@ -17,6 +17,7 @@ import (
 	"database/sql"
 
 	"github.com/go-kit/log"
+	"github.com/go-kit/log/level"
 	"github.com/prometheus/client_golang/prometheus"
 )
 
@@ -83,20 +84,33 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
 			return err
 		}
 		if !schemaname.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because schemaname is not valid")
 			continue
 		}
 		if !relname.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because relname is not valid")
 			continue
 		}
 		if !indexrelname.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because indexrelname is not valid")
 			continue
 		}
 		labels := []string{schemaname.String, relname.String, indexrelname.String}
 
-		idxScanMetric := 0.0
-		if idxScan.Valid {
-			idxScanMetric = idxScan.Float64
+		if !idxScan.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because idx_scan is not valid")
+			continue
 		}
+		if !idxTupRead.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because idx_tup_read is not valid")
+			continue
+		}
+		if !idxTupFetch.Valid {
+			level.Debug(c.log).Log("msg", "Skipping stats on index because idx_tup_fetch is not valid")
+			continue
+		}
+
+		idxScanMetric := idxScan.Float64
 		ch <- prometheus.MustNewConstMetric(
 			statUserIndexesIdxScan,
 			prometheus.CounterValue,
@@ -104,10 +118,7 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
 			labels...,
 		)
 
-		idxTupReadMetric := 0.0
-		if idxTupRead.Valid {
-			idxTupReadMetric = idxTupRead.Float64
-		}
+		idxTupReadMetric := idxTupRead.Float64
 		ch <- prometheus.MustNewConstMetric(
 			statUserIndexesIdxTupRead,
 			prometheus.CounterValue,
@@ -115,10 +126,7 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
 			labels...,
 		)
 
-		idxTupFetchMetric := 0.0
-		if idxTupFetch.Valid {
-			idxTupFetchMetric = idxTupFetch.Float64
-		}
+		idxTupFetchMetric := idxTupFetch.Float64
 		ch <- prometheus.MustNewConstMetric(
 			statUserIndexesIdxTupFetch,
 			prometheus.CounterValue,
diff --git a/collector/pg_stat_user_indexes_test.go b/collector/pg_stat_user_indexes_test.go
index 3d25d2f3..ce59355b 100644
--- a/collector/pg_stat_user_indexes_test.go
+++ b/collector/pg_stat_user_indexes_test.go
@@ -66,48 +66,3 @@ func TestPgStatUserIndexesCollector(t *testing.T) {
 		t.Errorf("there were unfulfilled exceptions: %s", err)
 	}
 }
-
-func TestPgStatUserIndexesCollectorNull(t *testing.T) {
-	db, mock, err := sqlmock.New()
-	if err != nil {
-		t.Fatalf("Error opening a stub db connection: %s", err)
-	}
-	defer db.Close()
-	inst := &instance{db: db}
-	columns := []string{
-		"schemaname",
-		"relname",
-		"indexrelname",
-		"idx_scan",
-		"idx_tup_read",
-		"idx_tup_fetch",
-	}
-	rows := sqlmock.NewRows(columns).
-		AddRow("foo", "bar", "blah", nil, nil, nil)
-
-	mock.ExpectQuery(sanitizeQuery(statUserIndexesQuery)).WillReturnRows(rows)
-
-	ch := make(chan prometheus.Metric)
-	go func() {
-		defer close(ch)
-		c := PGStatUserIndexesCollector{}
-
-		if err := c.Update(context.Background(), inst, ch); err != nil {
-			t.Errorf("Error calling PGStatUserIndexesCollector.Update: %s", err)
-		}
-	}()
-	expected := []MetricResult{
-		{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
-		{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
-		{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
-	}
-	convey.Convey("Metrics comparison", t, func() {
-		for _, expect := range expected {
-			m := readMetric(<-ch)
-			convey.So(expect, convey.ShouldResemble, m)
-		}
-	})
-	if err := mock.ExpectationsWereMet(); err != nil {
-		t.Errorf("there were unfulfilled exceptions: %s", err)
-	}
-}