@@ -12,27 +12,21 @@ import (
1212 "github.com/stretchr/testify/assert"
1313)
1414
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-
2215func TestPrometheus_Use (t * testing.T ) {
2316 e := echo .New ()
2417 p := NewPrometheus ("echo" , nil )
18+ p .Registerer = prometheus .NewRegistry ()
2519 p .Use (e )
2620
2721 assert .Equal (t , 1 , len (e .Routes ()), "only one route should be added" )
2822 assert .NotNil (t , e , "the engine should not be empty" )
2923 assert .Equal (t , e .Routes ()[0 ].Path , p .MetricsPath , "the path should match the metrics path" )
30- unregister (p )
3124}
3225
3326func TestPrometheus_Buckets (t * testing.T ) {
3427 e := echo .New ()
3528 p := NewPrometheus ("echo" , nil )
29+ p .Registerer = prometheus .NewRegistry ()
3630 p .Use (e )
3731
3832 path := "/ping"
@@ -51,24 +45,24 @@ func TestPrometheus_Buckets(t *testing.T) {
5145 assert .NotRegexp (t , "request_size_bytes.*le=\" 0.005\" " , r .Body .String (), "request should NOT have time bucket (like, 0.005s)" )
5246 })
5347
54- unregister (p )
5548}
5649
5750func TestPath (t * testing.T ) {
5851 p := NewPrometheus ("echo" , nil )
52+ p .Registerer = prometheus .NewRegistry ()
5953 assert .Equal (t , p .MetricsPath , defaultMetricPath , "no usage of path should yield default path" )
60- unregister (p )
6154}
6255
6356func TestSubsystem (t * testing.T ) {
6457 p := NewPrometheus ("echo" , nil )
58+ p .Registerer = prometheus .NewRegistry ()
6559 assert .Equal (t , p .Subsystem , "echo" , "subsystem should be default" )
66- unregister (p )
6760}
6861
6962func TestUse (t * testing.T ) {
7063 e := echo .New ()
7164 p := NewPrometheus ("echo" , nil )
65+ p .Registerer = prometheus .NewRegistry ()
7266
7367 g := gofight .New ()
7468 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
@@ -80,7 +74,6 @@ func TestUse(t *testing.T) {
8074 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
8175 assert .Equal (t , http .StatusOK , r .Code )
8276 })
83- unregister (p )
8477}
8578
8679func TestIgnore (t * testing.T ) {
@@ -95,6 +88,7 @@ func TestIgnore(t *testing.T) {
9588 return false
9689 }
9790 p := NewPrometheus ("echo" , ignore )
91+ p .Registerer = prometheus .NewRegistry ()
9892 p .Use (e )
9993
10094 g := gofight .New ()
@@ -111,12 +105,12 @@ func TestIgnore(t *testing.T) {
111105 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
112106 assert .NotContains (t , r .Body .String (), lipath , "ignored path must not be present" )
113107 })
114- unregister (p )
115108}
116109
117110func TestMetricsGenerated (t * testing.T ) {
118111 e := echo .New ()
119112 p := NewPrometheus ("echo" , nil )
113+ p .Registerer = prometheus .NewRegistry ()
120114 p .Use (e )
121115
122116 path := "/ping"
@@ -130,38 +124,38 @@ func TestMetricsGenerated(t *testing.T) {
130124 assert .Contains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
131125 assert .Contains (t , r .Body .String (), lpath , "path must be present" )
132126 })
133- unregister (p )
134127}
135128
136129func TestMetricsPathIgnored (t * testing.T ) {
137130 e := echo .New ()
138131 p := NewPrometheus ("echo" , nil )
132+ p .Registerer = prometheus .NewRegistry ()
139133 p .Use (e )
140134
141135 g := gofight .New ()
142136 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
143137 assert .Equal (t , http .StatusOK , r .Code )
144138 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
145139 })
146- unregister (p )
147140}
148141
149142func TestMetricsPushGateway (t * testing.T ) {
150143 e := echo .New ()
151144 p := NewPrometheus ("echo" , nil )
145+ p .Registerer = prometheus .NewRegistry ()
152146 p .Use (e )
153147
154148 g := gofight .New ()
155149 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
156150 assert .Equal (t , http .StatusOK , r .Code )
157151 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_request_duration" , p .Subsystem ))
158152 })
159- unregister (p )
160153}
161154
162155func TestMetricsForErrors (t * testing.T ) {
163156 e := echo .New ()
164157 p := NewPrometheus ("echo" , nil )
158+ p .Registerer = prometheus .NewRegistry ()
165159 p .Use (e )
166160
167161 e .GET ("/handler_for_ok" , func (c echo.Context ) error {
@@ -190,5 +184,4 @@ func TestMetricsForErrors(t *testing.T) {
190184 assert .Contains (t , body , `echo_requests_total{code="409",host="",method="GET",url="/handler_for_nok"} 2` )
191185 assert .Contains (t , body , `echo_requests_total{code="502",host="",method="GET",url="/handler_for_error"} 1` )
192186 })
193- unregister (p )
194187}
0 commit comments