Skip null metrics and add debug logs

Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
This commit is contained in:
Felix Yuan 2023-06-29 15:48:25 -07:00
parent 66a22531ff
commit 1fbfad3dc9
2 changed files with 19 additions and 56 deletions

View File

@ -17,6 +17,7 @@ import (
"database/sql" "database/sql"
"github.com/go-kit/log" "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -83,20 +84,33 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
return err return err
} }
if !schemaname.Valid { if !schemaname.Valid {
level.Debug(c.log).Log("msg", "Skipping stats on index because schemaname is not valid")
continue continue
} }
if !relname.Valid { if !relname.Valid {
level.Debug(c.log).Log("msg", "Skipping stats on index because relname is not valid")
continue continue
} }
if !indexrelname.Valid { if !indexrelname.Valid {
level.Debug(c.log).Log("msg", "Skipping stats on index because indexrelname is not valid")
continue continue
} }
labels := []string{schemaname.String, relname.String, indexrelname.String} labels := []string{schemaname.String, relname.String, indexrelname.String}
idxScanMetric := 0.0 if !idxScan.Valid {
if idxScan.Valid { level.Debug(c.log).Log("msg", "Skipping stats on index because idx_scan is not valid")
idxScanMetric = idxScan.Float64 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( ch <- prometheus.MustNewConstMetric(
statUserIndexesIdxScan, statUserIndexesIdxScan,
prometheus.CounterValue, prometheus.CounterValue,
@ -104,10 +118,7 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
labels..., labels...,
) )
idxTupReadMetric := 0.0 idxTupReadMetric := idxTupRead.Float64
if idxTupRead.Valid {
idxTupReadMetric = idxTupRead.Float64
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statUserIndexesIdxTupRead, statUserIndexesIdxTupRead,
prometheus.CounterValue, prometheus.CounterValue,
@ -115,10 +126,7 @@ func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *insta
labels..., labels...,
) )
idxTupFetchMetric := 0.0 idxTupFetchMetric := idxTupFetch.Float64
if idxTupFetch.Valid {
idxTupFetchMetric = idxTupFetch.Float64
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statUserIndexesIdxTupFetch, statUserIndexesIdxTupFetch,
prometheus.CounterValue, prometheus.CounterValue,

View File

@ -66,48 +66,3 @@ func TestPgStatUserIndexesCollector(t *testing.T) {
t.Errorf("there were unfulfilled exceptions: %s", err) 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)
}
}