-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics_test.go
125 lines (106 loc) · 4.66 KB
/
metrics_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
120
121
122
123
124
125
package main
import (
"errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestMetricCounting(t *testing.T) {
assert := assert.New(t)
dr := prometheus.NewRegistry()
var reg prometheus.Registerer = dr
var gat prometheus.Gatherer = dr
promColl := PromCollectors{}
promColl.Register(reg)
result := Result{StatusCode: 200, Length: 5, Duration: 6, Hash: 7}
promColl.Update("my-site.example.com", result, nil)
srv := httptest.NewServer(promhttp.HandlerFor(gat, promhttp.HandlerOpts{}))
defer srv.Close()
resp, _ := http.Get(srv.URL)
assert.Equal(resp.StatusCode, 200, "response from prometheus handler")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
expectedBody := `# HELP sitest_code Response code
# TYPE sitest_code gauge
sitest_code{site="my-site.example.com"} 200.0
# HELP sitest_count Total number of performed check
# TYPE sitest_count counter
sitest_count{site="my-site.example.com"} 1.0
# HELP sitest_duration_seconds Histogram of request duration
# TYPE sitest_duration_seconds histogram
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.005"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.01"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.025"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.05"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.1"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.25"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.5"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="1.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="2.5"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="5.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="10.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="+Inf"} 1.0
sitest_duration_seconds_sum{site="my-site.example.com"} 6e-09
sitest_duration_seconds_count{site="my-site.example.com"} 1.0
# HELP sitest_hash Page hash
# TYPE sitest_hash gauge
sitest_hash{site="my-site.example.com"} 7.0
# HELP sitest_length Page length
# TYPE sitest_length gauge
sitest_length{site="my-site.example.com"} 5.0
`
assert.Equal(string(body[:]), expectedBody, "expected body")
}
func TestMetricError(t *testing.T) {
assert := assert.New(t)
dr := prometheus.NewRegistry()
var reg prometheus.Registerer = dr
var gat prometheus.Gatherer = dr
promColl := PromCollectors{}
promColl.Register(reg)
result := Result{}
promColl.Update("my-site.example.com", result, errors.New("test-error"))
srv := httptest.NewServer(promhttp.HandlerFor(gat, promhttp.HandlerOpts{}))
defer srv.Close()
resp, _ := http.Get(srv.URL)
assert.Equal(resp.StatusCode, 200, "response from prometheus handler")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
expectedBody := `# HELP sitest_code Response code
# TYPE sitest_code gauge
sitest_code{site="my-site.example.com"} 0.0
# HELP sitest_count Total number of performed check
# TYPE sitest_count counter
sitest_count{site="my-site.example.com"} 1.0
# HELP sitest_duration_seconds Histogram of request duration
# TYPE sitest_duration_seconds histogram
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.005"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.01"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.025"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.05"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.1"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.25"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="0.5"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="1.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="2.5"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="5.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="10.0"} 1.0
sitest_duration_seconds_bucket{site="my-site.example.com",le="+Inf"} 1.0
sitest_duration_seconds_sum{site="my-site.example.com"} 0.0
sitest_duration_seconds_count{site="my-site.example.com"} 1.0
# HELP sitest_error Total number of error
# TYPE sitest_error counter
sitest_error{site="my-site.example.com"} 1.0
# HELP sitest_hash Page hash
# TYPE sitest_hash gauge
sitest_hash{site="my-site.example.com"} 0.0
# HELP sitest_length Page length
# TYPE sitest_length gauge
sitest_length{site="my-site.example.com"} 0.0
`
assert.Equal(string(body[:]), expectedBody, "expected body")
}