Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Skip NULL values.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed May 15, 2021
1 parent 580fbb5 commit 20569d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions internal/collector/collector_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func updateMultipleMetrics(row []sql.NullString, desc typedDesc, colnames []stri
}

for i, resColname := range colnames { // walk through column names from data row
// Skip NULL values.
if !row[i].Valid {
continue
}

// Check for value.
if descColname == resColname && !valueOK {
var err error
Expand Down Expand Up @@ -343,6 +348,11 @@ func updateSingleMetric(row []sql.NullString, desc typedDesc, colnames []string,
}

for i, colname := range colnames {
// Skip NULL values.
if !row[i].Valid {
continue
}

// Check for value.
if colname == desc.value {
var err error
Expand Down
7 changes: 4 additions & 3 deletions internal/collector/collector_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ func Test_updateMetrics(t *testing.T) {

func Test_updateMultipleMetrics(t *testing.T) {
row := []sql.NullString{
{String: "", Valid: false}, // NULL value
{String: "852", Valid: true}, {String: "456", Valid: true}, {String: "753", Valid: true}, // ins, upd, del
{String: "example", Valid: true}, // relname
}
colnames := []string{"inserted", "updated", "deleted", "relname"}
colnames := []string{"nullable", "inserted", "updated", "deleted", "relname"}

testcases := []struct {
desc typedDesc
Expand Down Expand Up @@ -473,9 +474,9 @@ func Test_updateMultipleMetrics(t *testing.T) {

func Test_updateSingleMetric(t *testing.T) {
row := []sql.NullString{
{String: "123", Valid: true}, {String: "987654", Valid: true}, {String: "example", Valid: true},
{String: "123", Valid: true}, {String: "987654", Valid: true}, {String: "example", Valid: true}, {String: "", Valid: false},
}
colnames := []string{"seq_scan", "idx_scan", "relname"}
colnames := []string{"seq_scan", "idx_scan", "relname", "nullable"}

testcases := []struct {
desc typedDesc
Expand Down

0 comments on commit 20569d7

Please sign in to comment.