Skip to content

Commit 1fbfad3

Browse files
committed
Skip null metrics and add debug logs
Signed-off-by: Felix Yuan <[email protected]>
1 parent 66a2253 commit 1fbfad3

File tree

2 files changed

+19
-56
lines changed

2 files changed

+19
-56
lines changed

collector/pg_stat_user_indexes.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"database/sql"
1818

1919
"github.com/go-kit/log"
20+
"github.com/go-kit/log/level"
2021
"github.com/prometheus/client_golang/prometheus"
2122
)
2223

@@ -83,42 +84,49 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
8384
return err
8485
}
8586
if !schemaname.Valid {
87+
level.Debug(c.log).Log("msg", "Skipping stats on index because schemaname is not valid")
8688
continue
8789
}
8890
if !relname.Valid {
91+
level.Debug(c.log).Log("msg", "Skipping stats on index because relname is not valid")
8992
continue
9093
}
9194
if !indexrelname.Valid {
95+
level.Debug(c.log).Log("msg", "Skipping stats on index because indexrelname is not valid")
9296
continue
9397
}
9498
labels := []string{schemaname.String, relname.String, indexrelname.String}
9599

96-
idxScanMetric := 0.0
97-
if idxScan.Valid {
98-
idxScanMetric = idxScan.Float64
100+
if !idxScan.Valid {
101+
level.Debug(c.log).Log("msg", "Skipping stats on index because idx_scan is not valid")
102+
continue
103+
}
104+
if !idxTupRead.Valid {
105+
level.Debug(c.log).Log("msg", "Skipping stats on index because idx_tup_read is not valid")
106+
continue
107+
}
108+
if !idxTupFetch.Valid {
109+
level.Debug(c.log).Log("msg", "Skipping stats on index because idx_tup_fetch is not valid")
110+
continue
99111
}
112+
113+
idxScanMetric := idxScan.Float64
100114
ch <- prometheus.MustNewConstMetric(
101115
statUserIndexesIdxScan,
102116
prometheus.CounterValue,
103117
idxScanMetric,
104118
labels...,
105119
)
106120

107-
idxTupReadMetric := 0.0
108-
if idxTupRead.Valid {
109-
idxTupReadMetric = idxTupRead.Float64
110-
}
121+
idxTupReadMetric := idxTupRead.Float64
111122
ch <- prometheus.MustNewConstMetric(
112123
statUserIndexesIdxTupRead,
113124
prometheus.CounterValue,
114125
idxTupReadMetric,
115126
labels...,
116127
)
117128

118-
idxTupFetchMetric := 0.0
119-
if idxTupFetch.Valid {
120-
idxTupFetchMetric = idxTupFetch.Float64
121-
}
129+
idxTupFetchMetric := idxTupFetch.Float64
122130
ch <- prometheus.MustNewConstMetric(
123131
statUserIndexesIdxTupFetch,
124132
prometheus.CounterValue,

collector/pg_stat_user_indexes_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,3 @@ func TestPgStatUserIndexesCollector(t *testing.T) {
6666
t.Errorf("there were unfulfilled exceptions: %s", err)
6767
}
6868
}
69-
70-
func TestPgStatUserIndexesCollectorNull(t *testing.T) {
71-
db, mock, err := sqlmock.New()
72-
if err != nil {
73-
t.Fatalf("Error opening a stub db connection: %s", err)
74-
}
75-
defer db.Close()
76-
inst := &instance{db: db}
77-
columns := []string{
78-
"schemaname",
79-
"relname",
80-
"indexrelname",
81-
"idx_scan",
82-
"idx_tup_read",
83-
"idx_tup_fetch",
84-
}
85-
rows := sqlmock.NewRows(columns).
86-
AddRow("foo", "bar", "blah", nil, nil, nil)
87-
88-
mock.ExpectQuery(sanitizeQuery(statUserIndexesQuery)).WillReturnRows(rows)
89-
90-
ch := make(chan prometheus.Metric)
91-
go func() {
92-
defer close(ch)
93-
c := PGStatUserIndexesCollector{}
94-
95-
if err := c.Update(context.Background(), inst, ch); err != nil {
96-
t.Errorf("Error calling PGStatUserIndexesCollector.Update: %s", err)
97-
}
98-
}()
99-
expected := []MetricResult{
100-
{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
101-
{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
102-
{labels: labelMap{"schemaname": "foo", "relname": "bar", "indexrelname": "blah"}, value: 0, metricType: dto.MetricType_COUNTER},
103-
}
104-
convey.Convey("Metrics comparison", t, func() {
105-
for _, expect := range expected {
106-
m := readMetric(<-ch)
107-
convey.So(expect, convey.ShouldResemble, m)
108-
}
109-
})
110-
if err := mock.ExpectationsWereMet(); err != nil {
111-
t.Errorf("there were unfulfilled exceptions: %s", err)
112-
}
113-
}

0 commit comments

Comments
 (0)