-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter_test.go
119 lines (92 loc) · 3.13 KB
/
exporter_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package scraper
import (
"net/url"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
model "github.com/prometheus/client_model/go"
)
type metricResult struct {
value float64
labels map[string]string
}
type metricResultHistogram struct {
sampleSum float64
sampleCount uint64
labels map[string]string
}
func labels2Map(labels []*model.LabelPair) map[string]string {
res := map[string]string{}
for _, l := range labels {
res[l.GetName()] = l.GetValue()
}
return res
}
func readGauge(g prometheus.Metric) metricResult {
m := &model.Metric{}
g.Write(m)
return metricResult{
value: m.GetGauge().GetValue(),
labels: labels2Map(m.GetLabel()),
}
}
func readHistogram(g prometheus.Metric) metricResultHistogram {
m := &model.Metric{}
g.Write(m)
return metricResultHistogram{
sampleSum: *m.GetHistogram().SampleSum,
sampleCount: *m.GetHistogram().SampleCount,
labels: labels2Map(m.GetLabel()),
}
}
func Test_Desribe(t *testing.T) {
metrics := NewMetrics()
exporter := NewExporter(metrics, 1)
ch := make(chan *prometheus.Desc)
go exporter.Describe(ch)
d := <-ch
expectedExternalServiceUpDesc := `Desc{fqName: "sample_external_url_up", help: "URL status", constLabels: {}, variableLabels: [url]}`
actualExternalServiceUpDesc := d.String()
if expectedExternalServiceUpDesc != actualExternalServiceUpDesc {
t.Errorf("Want: %s, got: %s", expectedExternalServiceUpDesc, actualExternalServiceUpDesc)
}
d = <-ch
expectedExternalServiceResponseTimeMS := `Desc{fqName: "sample_external_url_response_time_ms", help: "URL response time in milli seconds", constLabels: {}, variableLabels: [url]}`
actualExternalServiceResponseTimeMS := d.String()
if expectedExternalServiceResponseTimeMS != actualExternalServiceResponseTimeMS {
t.Errorf("Want: %s, got: %s", expectedExternalServiceResponseTimeMS, actualExternalServiceResponseTimeMS)
}
}
func Test_Collect(t *testing.T) {
metrics := NewMetrics()
exporter := NewExporter(metrics, 10)
serverURL, _ := url.Parse("https://foo.com")
expectedQueryResult := TargetResponse{
URL: serverURL,
Status: HealthGood,
ResponseTime: time.Duration(2 * time.Second),
}
exporter.entries = []TargetResponse{expectedQueryResult}
ch := make(chan prometheus.Metric, 10)
defer close(ch)
go exporter.Collect(ch)
g := (<-ch).(prometheus.Gauge)
result := readGauge(g)
if expectedQueryResult.URL.String() != result.labels["url"] {
t.Errorf("Want: %s, got: %s", expectedQueryResult.URL, result.labels["url"])
}
if float64(expectedQueryResult.Status) != result.value {
t.Errorf("Want: %f, got: %f", float64(expectedQueryResult.Status), result.value)
}
g1 := (<-ch).(prometheus.Histogram)
hResult := readHistogram(g1)
if expectedQueryResult.URL.String() != hResult.labels["url"] {
t.Errorf("Want: %s, got: %s", expectedQueryResult.URL, hResult.labels["url"])
}
if float64(expectedQueryResult.ResponseTime.Milliseconds()) != hResult.sampleSum {
t.Errorf("Want: %f, got: %f", float64(expectedQueryResult.ResponseTime.Milliseconds()), hResult.sampleSum)
}
if 1 != hResult.sampleCount {
t.Errorf("Want: %d, got: %d", 1, hResult.sampleCount)
}
}