-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathquery_test.go
More file actions
66 lines (59 loc) · 2.17 KB
/
query_test.go
File metadata and controls
66 lines (59 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sql_exporter
import (
"testing"
"github.com/burningalchemist/sql_exporter/config"
dto "github.com/prometheus/client_model/go"
)
func TestNewQueryAutoMetricsDisabled(t *testing.T) {
q, err := NewQuery("", &config.QueryConfig{Name: "q1", Query: "SELECT 1"}, nil, false)
if err != nil {
t.Fatalf("NewQuery: %v", err)
}
if q.durationDesc != nil || q.rowsDesc != nil {
t.Fatalf("expected no auto-metric descs when disabled, got duration=%v rows=%v", q.durationDesc, q.rowsDesc)
}
}
func TestNewQueryAutoMetricsEnabled(t *testing.T) {
targetName, targetVal := "target", "db1"
constLabels := []*dto.LabelPair{{Name: &targetName, Value: &targetVal}}
q, err := NewQuery("", &config.QueryConfig{Name: "q1", Query: "SELECT 1"}, constLabels, true)
if err != nil {
t.Fatalf("NewQuery: %v", err)
}
if q.durationDesc == nil || q.rowsDesc == nil {
t.Fatalf("expected auto-metric descs to be set when enabled")
}
if got := q.durationDesc.Name(); got != queryDurationName {
t.Errorf("duration metric name = %q, want %q", got, queryDurationName)
}
if got := q.rowsDesc.Name(); got != queryRowsName {
t.Errorf("rows metric name = %q, want %q", got, queryRowsName)
}
gotLabels := q.durationDesc.ConstLabels()
if len(gotLabels) != 2 {
t.Fatalf("expected 2 const labels (target, query), got %d", len(gotLabels))
}
labels := make(map[string]string, len(gotLabels))
for _, lp := range gotLabels {
labels[lp.GetName()] = lp.GetValue()
}
if labels[queryLabelName] != "q1" {
t.Errorf("query label = %q, want q1", labels[queryLabelName])
}
if labels["target"] != "db1" {
t.Errorf("target label = %q, want db1", labels["target"])
}
}
func TestNewQueryAutoMetricsEnabledNoConstLabels(t *testing.T) {
q, err := NewQuery("", &config.QueryConfig{Name: "singleton", Query: "SELECT 1"}, nil, true)
if err != nil {
t.Fatalf("NewQuery: %v", err)
}
gotLabels := q.durationDesc.ConstLabels()
if len(gotLabels) != 1 {
t.Fatalf("expected just the query label, got %d labels", len(gotLabels))
}
if gotLabels[0].GetName() != queryLabelName || gotLabels[0].GetValue() != "singleton" {
t.Errorf("expected query=singleton, got %s=%s", gotLabels[0].GetName(), gotLabels[0].GetValue())
}
}