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"
"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,

View File

@ -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)
}
}