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