@@ -8,14 +8,15 @@ import (
8
8
"context"
9
9
"errors"
10
10
"fmt"
11
- "github.com/labstack/echo/v4"
12
- "github.com/prometheus/client_golang/prometheus"
13
- "github.com/stretchr/testify/assert"
14
11
"net/http"
15
12
"net/http/httptest"
16
13
"strings"
17
14
"testing"
18
15
"time"
16
+
17
+ "github.com/labstack/echo/v4"
18
+ "github.com/prometheus/client_golang/prometheus"
19
+ "github.com/stretchr/testify/assert"
19
20
)
20
21
21
22
func TestCustomRegistryMetrics (t * testing.T ) {
@@ -161,6 +162,64 @@ func TestMiddlewareConfig_LabelFuncs(t *testing.T) {
161
162
assert .Contains (t , body , `echo_request_duration_seconds_count{code="200",host="example.com",method="overridden_GET",scheme="http",url="/ok"} 1` )
162
163
}
163
164
165
+ func TestMiddlewareConfig_StatusCodeResolver (t * testing.T ) {
166
+ e := echo .New ()
167
+ customRegistry := prometheus .NewRegistry ()
168
+ customResolver := func (c echo.Context , err error ) int {
169
+ if err == nil {
170
+ return c .Response ().Status
171
+ }
172
+ msg := err .Error ()
173
+ if strings .Contains (msg , "NOT FOUND" ) {
174
+ return http .StatusNotFound
175
+ }
176
+ if strings .Contains (msg , "NOT Authorized" ) {
177
+ return http .StatusUnauthorized
178
+ }
179
+ return http .StatusInternalServerError
180
+ }
181
+ e .Use (NewMiddlewareWithConfig (MiddlewareConfig {
182
+ Skipper : func (c echo.Context ) bool {
183
+ return strings .HasSuffix (c .Path (), "ignore" )
184
+ },
185
+ Subsystem : "myapp" ,
186
+ Registerer : customRegistry ,
187
+ StatusCodeResolver : customResolver ,
188
+ }))
189
+ e .GET ("/metrics" , NewHandlerWithConfig (HandlerConfig {Gatherer : customRegistry }))
190
+
191
+ e .GET ("/handler_for_ok" , func (c echo.Context ) error {
192
+ return c .JSON (http .StatusOK , "OK" )
193
+ })
194
+ e .GET ("/handler_for_nok" , func (c echo.Context ) error {
195
+ return c .JSON (http .StatusConflict , "NOK" )
196
+ })
197
+ e .GET ("/handler_for_not_found" , func (c echo.Context ) error {
198
+ return errors .New ("NOT FOUND" )
199
+ })
200
+ e .GET ("/handler_for_not_authorized" , func (c echo.Context ) error {
201
+ return errors .New ("NOT Authorized" )
202
+ })
203
+ e .GET ("/handler_for_unknown_error" , func (c echo.Context ) error {
204
+ return errors .New ("i do not know" )
205
+ })
206
+
207
+ assert .Equal (t , http .StatusOK , request (e , "/handler_for_ok" ))
208
+ assert .Equal (t , http .StatusConflict , request (e , "/handler_for_nok" ))
209
+ assert .Equal (t , http .StatusInternalServerError , request (e , "/handler_for_not_found" ))
210
+ assert .Equal (t , http .StatusInternalServerError , request (e , "/handler_for_not_authorized" ))
211
+ assert .Equal (t , http .StatusInternalServerError , request (e , "/handler_for_unknown_error" ))
212
+
213
+ body , code := requestBody (e , "/metrics" )
214
+ assert .Equal (t , http .StatusOK , code )
215
+ assert .Contains (t , body , fmt .Sprintf ("%s_requests_total" , "myapp" ))
216
+ assert .Contains (t , body , `myapp_requests_total{code="200",host="example.com",method="GET",url="/handler_for_ok"} 1` )
217
+ assert .Contains (t , body , `myapp_requests_total{code="409",host="example.com",method="GET",url="/handler_for_nok"} 1` )
218
+ assert .Contains (t , body , `myapp_requests_total{code="404",host="example.com",method="GET",url="/handler_for_not_found"} 1` )
219
+ assert .Contains (t , body , `myapp_requests_total{code="401",host="example.com",method="GET",url="/handler_for_not_authorized"} 1` )
220
+ assert .Contains (t , body , `myapp_requests_total{code="500",host="example.com",method="GET",url="/handler_for_unknown_error"} 1` )
221
+ }
222
+
164
223
func TestMiddlewareConfig_HistogramOptsFunc (t * testing.T ) {
165
224
e := echo .New ()
166
225
customRegistry := prometheus .NewRegistry ()
0 commit comments