-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first set of tests + some helper functions cribbed from
mysqld_exporter Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
- Loading branch information
Showing
5 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package collector | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
type labelMap map[string]string | ||
|
||
type MetricResult struct { | ||
labels labelMap | ||
value float64 | ||
metricType dto.MetricType | ||
} | ||
|
||
func readMetric(m prometheus.Metric) MetricResult { | ||
pb := &dto.Metric{} | ||
m.Write(pb) | ||
labels := make(labelMap, len(pb.Label)) | ||
for _, v := range pb.Label { | ||
labels[v.GetName()] = v.GetValue() | ||
} | ||
if pb.Gauge != nil { | ||
return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE} | ||
} | ||
if pb.Counter != nil { | ||
return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER} | ||
} | ||
if pb.Untyped != nil { | ||
return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED} | ||
} | ||
panic("Unsupported metric type") | ||
} | ||
|
||
func sanitizeQuery(q string) string { | ||
q = strings.Join(strings.Fields(q), " ") | ||
q = strings.Replace(q, "(", "\\(", -1) | ||
q = strings.Replace(q, ")", "\\)", -1) | ||
q = strings.Replace(q, "*", "\\*", -1) | ||
q = strings.Replace(q, "$", "\\$", -1) | ||
return q | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
"github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestPGDatabaseCollector(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("Error opening a stub db connection: %s", err) | ||
} | ||
defer db.Close() | ||
|
||
mock.ExpectQuery(sanitizeQuery(pgDatabaseQuery)).WillReturnRows(sqlmock.NewRows([]string{"datname"}). | ||
AddRow("postgres")) | ||
|
||
mock.ExpectQuery(sanitizeQuery(pgDatabaseSizeQuery)).WithArgs("postgres").WillReturnRows(sqlmock.NewRows([]string{"pg_database_size"}). | ||
AddRow(1024)) | ||
|
||
ch := make(chan prometheus.Metric) | ||
go func() { | ||
defer close(ch) | ||
c := PGDatabaseCollector{} | ||
if err := c.Update(context.Background(), db, ch); err != nil { | ||
t.Errorf("Error calling PGDatabaseCollector.Update: %s", err) | ||
} | ||
}() | ||
|
||
expected := []MetricResult{ | ||
{labels: labelMap{"datname": "postgres"}, value: 1024, metricType: dto.MetricType_GAUGE}, | ||
} | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters