@@ -12,21 +12,27 @@ 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+
1522func TestPrometheus_Use (t * testing.T ) {
1623 e := echo .New ()
1724 p := NewPrometheus ("echo" , nil )
18- p .Registerer = prometheus .NewRegistry ()
1925 p .Use (e )
2026
2127 assert .Equal (t , 1 , len (e .Routes ()), "only one route should be added" )
2228 assert .NotNil (t , e , "the engine should not be empty" )
2329 assert .Equal (t , e .Routes ()[0 ].Path , p .MetricsPath , "the path should match the metrics path" )
30+ unregister (p )
2431}
2532
2633func TestPrometheus_Buckets (t * testing.T ) {
2734 e := echo .New ()
2835 p := NewPrometheus ("echo" , nil )
29- p .Registerer = prometheus .NewRegistry ()
3036 p .Use (e )
3137
3238 path := "/ping"
@@ -45,24 +51,24 @@ func TestPrometheus_Buckets(t *testing.T) {
4551 assert .NotRegexp (t , "request_size_bytes.*le=\" 0.005\" " , r .Body .String (), "request should NOT have time bucket (like, 0.005s)" )
4652 })
4753
54+ unregister (p )
4855}
4956
5057func TestPath (t * testing.T ) {
5158 p := NewPrometheus ("echo" , nil )
52- p .Registerer = prometheus .NewRegistry ()
5359 assert .Equal (t , p .MetricsPath , defaultMetricPath , "no usage of path should yield default path" )
60+ unregister (p )
5461}
5562
5663func TestSubsystem (t * testing.T ) {
5764 p := NewPrometheus ("echo" , nil )
58- p .Registerer = prometheus .NewRegistry ()
5965 assert .Equal (t , p .Subsystem , "echo" , "subsystem should be default" )
66+ unregister (p )
6067}
6168
6269func TestUse (t * testing.T ) {
6370 e := echo .New ()
6471 p := NewPrometheus ("echo" , nil )
65- p .Registerer = prometheus .NewRegistry ()
6672
6773 g := gofight .New ()
6874 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
@@ -74,6 +80,7 @@ func TestUse(t *testing.T) {
7480 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
7581 assert .Equal (t , http .StatusOK , r .Code )
7682 })
83+ unregister (p )
7784}
7885
7986func TestIgnore (t * testing.T ) {
@@ -88,7 +95,6 @@ func TestIgnore(t *testing.T) {
8895 return false
8996 }
9097 p := NewPrometheus ("echo" , ignore )
91- p .Registerer = prometheus .NewRegistry ()
9298 p .Use (e )
9399
94100 g := gofight .New ()
@@ -105,12 +111,12 @@ func TestIgnore(t *testing.T) {
105111 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
106112 assert .NotContains (t , r .Body .String (), lipath , "ignored path must not be present" )
107113 })
114+ unregister (p )
108115}
109116
110117func TestMetricsGenerated (t * testing.T ) {
111118 e := echo .New ()
112119 p := NewPrometheus ("echo" , nil )
113- p .Registerer = prometheus .NewRegistry ()
114120 p .Use (e )
115121
116122 path := "/ping"
@@ -124,38 +130,38 @@ func TestMetricsGenerated(t *testing.T) {
124130 assert .Contains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
125131 assert .Contains (t , r .Body .String (), lpath , "path must be present" )
126132 })
133+ unregister (p )
127134}
128135
129136func TestMetricsPathIgnored (t * testing.T ) {
130137 e := echo .New ()
131138 p := NewPrometheus ("echo" , nil )
132- p .Registerer = prometheus .NewRegistry ()
133139 p .Use (e )
134140
135141 g := gofight .New ()
136142 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
137143 assert .Equal (t , http .StatusOK , r .Code )
138144 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_requests_total" , p .Subsystem ))
139145 })
146+ unregister (p )
140147}
141148
142149func TestMetricsPushGateway (t * testing.T ) {
143150 e := echo .New ()
144151 p := NewPrometheus ("echo" , nil )
145- p .Registerer = prometheus .NewRegistry ()
146152 p .Use (e )
147153
148154 g := gofight .New ()
149155 g .GET (p .MetricsPath ).Run (e , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
150156 assert .Equal (t , http .StatusOK , r .Code )
151157 assert .NotContains (t , r .Body .String (), fmt .Sprintf ("%s_request_duration" , p .Subsystem ))
152158 })
159+ unregister (p )
153160}
154161
155162func TestMetricsForErrors (t * testing.T ) {
156163 e := echo .New ()
157164 p := NewPrometheus ("echo" , nil )
158- p .Registerer = prometheus .NewRegistry ()
159165 p .Use (e )
160166
161167 e .GET ("/handler_for_ok" , func (c echo.Context ) error {
@@ -184,4 +190,5 @@ func TestMetricsForErrors(t *testing.T) {
184190 assert .Contains (t , body , `echo_requests_total{code="409",host="",method="GET",url="/handler_for_nok"} 2` )
185191 assert .Contains (t , body , `echo_requests_total{code="502",host="",method="GET",url="/handler_for_error"} 1` )
186192 })
193+ unregister (p )
187194}
0 commit comments