@@ -12,27 +12,21 @@ import (
12
12
"github.com/stretchr/testify/assert"
13
13
)
14
14
15
- func unregister (p * Prometheus ) {
16
- prometheus .Unregister (p .reqCnt )
17
- prometheus .Unregister (p .reqDur )
18
- prometheus .Unregister (p .reqSz )
19
- prometheus .Unregister (p .resSz )
20
- }
21
-
22
15
func TestPrometheus_Use (t * testing.T ) {
23
16
e := echo .New ()
24
17
p := NewPrometheus ("echo" , nil )
18
+ p .Registerer = prometheus .NewRegistry ()
25
19
p .Use (e )
26
20
27
21
assert .Equal (t , 1 , len (e .Routes ()), "only one route should be added" )
28
22
assert .NotNil (t , e , "the engine should not be empty" )
29
23
assert .Equal (t , e .Routes ()[0 ].Path , p .MetricsPath , "the path should match the metrics path" )
30
- unregister (p )
31
24
}
32
25
33
26
func TestPrometheus_Buckets (t * testing.T ) {
34
27
e := echo .New ()
35
28
p := NewPrometheus ("echo" , nil )
29
+ p .Registerer = prometheus .NewRegistry ()
36
30
p .Use (e )
37
31
38
32
path := "/ping"
@@ -51,24 +45,24 @@ func TestPrometheus_Buckets(t *testing.T) {
51
45
assert .NotRegexp (t , "request_size_bytes.*le=\" 0.005\" " , r .Body .String (), "request should NOT have time bucket (like, 0.005s)" )
52
46
})
53
47
54
- unregister (p )
55
48
}
56
49
57
50
func TestPath (t * testing.T ) {
58
51
p := NewPrometheus ("echo" , nil )
52
+ p .Registerer = prometheus .NewRegistry ()
59
53
assert .Equal (t , p .MetricsPath , defaultMetricPath , "no usage of path should yield default path" )
60
- unregister (p )
61
54
}
62
55
63
56
func TestSubsystem (t * testing.T ) {
64
57
p := NewPrometheus ("echo" , nil )
58
+ p .Registerer = prometheus .NewRegistry ()
65
59
assert .Equal (t , p .Subsystem , "echo" , "subsystem should be default" )
66
- unregister (p )
67
60
}
68
61
69
62
func TestUse (t * testing.T ) {
70
63
e := echo .New ()
71
64
p := NewPrometheus ("echo" , nil )
65
+ p .Registerer = prometheus .NewRegistry ()
72
66
73
67
g := gofight .New ()
74
68
g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
@@ -80,7 +74,6 @@ func TestUse(t *testing.T) {
80
74
g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
81
75
assert .Equal (t , http .StatusOK , r .Code )
82
76
})
83
- unregister (p )
84
77
}
85
78
86
79
func TestIgnore (t * testing.T ) {
@@ -95,6 +88,7 @@ func TestIgnore(t *testing.T) {
95
88
return false
96
89
}
97
90
p := NewPrometheus ("echo" , ignore )
91
+ p .Registerer = prometheus .NewRegistry ()
98
92
p .Use (e )
99
93
100
94
g := gofight .New ()
@@ -111,12 +105,12 @@ func TestIgnore(t *testing.T) {
111
105
assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
112
106
assert .NotContains (t , r .Body .String (), lipath , "ignored path must not be present" )
113
107
})
114
- unregister (p )
115
108
}
116
109
117
110
func TestMetricsGenerated (t * testing.T ) {
118
111
e := echo .New ()
119
112
p := NewPrometheus ("echo" , nil )
113
+ p .Registerer = prometheus .NewRegistry ()
120
114
p .Use (e )
121
115
122
116
path := "/ping"
@@ -130,38 +124,38 @@ func TestMetricsGenerated(t *testing.T) {
130
124
assert .Contains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
131
125
assert .Contains (t , r .Body .String (), lpath , "path must be present" )
132
126
})
133
- unregister (p )
134
127
}
135
128
136
129
func TestMetricsPathIgnored (t * testing.T ) {
137
130
e := echo .New ()
138
131
p := NewPrometheus ("echo" , nil )
132
+ p .Registerer = prometheus .NewRegistry ()
139
133
p .Use (e )
140
134
141
135
g := gofight .New ()
142
136
g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
143
137
assert .Equal (t , http .StatusOK , r .Code )
144
138
assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
145
139
})
146
- unregister (p )
147
140
}
148
141
149
142
func TestMetricsPushGateway (t * testing.T ) {
150
143
e := echo .New ()
151
144
p := NewPrometheus ("echo" , nil )
145
+ p .Registerer = prometheus .NewRegistry ()
152
146
p .Use (e )
153
147
154
148
g := gofight .New ()
155
149
g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
156
150
assert .Equal (t , http .StatusOK , r .Code )
157
151
assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_request_duration" , p .Subsystem ))
158
152
})
159
- unregister (p )
160
153
}
161
154
162
155
func TestMetricsForErrors (t * testing.T ) {
163
156
e := echo .New ()
164
157
p := NewPrometheus ("echo" , nil )
158
+ p .Registerer = prometheus .NewRegistry ()
165
159
p .Use (e )
166
160
167
161
e .GET ("/handler_for_ok" , func (c echo.Context ) error {
@@ -190,5 +184,4 @@ func TestMetricsForErrors(t *testing.T) {
190
184
assert .Contains (t , body , `echo_requests_total{code="409",host="",method="GET",url="/handler_for_nok"} 2` )
191
185
assert .Contains (t , body , `echo_requests_total{code="502",host="",method="GET",url="/handler_for_error"} 1` )
192
186
})
193
- unregister (p )
194
187
}
0 commit comments