Skip to content

Commit 95ef252

Browse files
committed
Do not add roleMetrics when row is not valid
Signed-off-by: Jocelyn Thode <[email protected]>
1 parent 3c1fb00 commit 95ef252

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

collector/pg_roles.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ func (c PGRolesCollector) Update(ctx context.Context, instance *instance, ch cha
7171
return err
7272
}
7373

74-
rolnameLabel := "unknown"
75-
if rolname.Valid {
76-
rolnameLabel = rolname.String
74+
if !rolname.Valid {
75+
continue
7776
}
77+
rolnameLabel := rolname.String
7878

79-
connLimitMetric := 0.0
80-
if connLimit.Valid {
81-
connLimitMetric = float64(connLimit.Int64)
79+
if !connLimit.Valid {
80+
continue
8281
}
82+
connLimitMetric := float64(connLimit.Int64)
8383

8484
ch <- prometheus.MustNewConstMetric(
8585
pgRolesConnectionLimitsDesc,

collector/pg_roles_test.go

-36
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,3 @@ func TestPGRolesCollector(t *testing.T) {
5656
t.Errorf("there were unfulfilled exceptions: %s", err)
5757
}
5858
}
59-
60-
func TestPGRolesCollectorNullMetric(t *testing.T) {
61-
db, mock, err := sqlmock.New()
62-
if err != nil {
63-
t.Fatalf("Error opening a stub db connection: %s", err)
64-
}
65-
defer db.Close()
66-
67-
inst := &instance{db: db}
68-
69-
mock.ExpectQuery(sanitizeQuery(pgRolesConnectionLimitsQuery)).WillReturnRows(sqlmock.NewRows([]string{"rolname", "rolconnlimit"}).
70-
AddRow(nil, nil))
71-
72-
ch := make(chan prometheus.Metric)
73-
go func() {
74-
defer close(ch)
75-
c := PGRolesCollector{}
76-
77-
if err := c.Update(context.Background(), inst, ch); err != nil {
78-
t.Errorf("Error calling PGRolesCollector.Update: %s", err)
79-
}
80-
}()
81-
82-
expected := []MetricResult{
83-
{labels: labelMap{"rolname": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
84-
}
85-
convey.Convey("Metrics comparison", t, func() {
86-
for _, expect := range expected {
87-
m := readMetric(<-ch)
88-
convey.So(expect, convey.ShouldResemble, m)
89-
}
90-
})
91-
if err := mock.ExpectationsWereMet(); err != nil {
92-
t.Errorf("there were unfulfilled exceptions: %s", err)
93-
}
94-
}

0 commit comments

Comments
 (0)