-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathecho.go
40 lines (30 loc) · 993 Bytes
/
echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Package echo is a helper package to get an echo compatible middleware
package echo
import (
"context"
"github.com/labstack/echo/v4"
"github.com/slok/go-http-metrics/middleware"
)
// Handler returns a Echo measuring middleware.
func Handler(handlerID string, m middleware.Middleware) echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return echo.HandlerFunc(func(c echo.Context) error {
r := &reporter{c: c}
var err error
m.Measure(handlerID, r, func() {
if err = h(c); err != nil {
c.Error(err)
}
})
return err
})
}
}
type reporter struct {
c echo.Context
}
func (r *reporter) Method() string { return r.c.Request().Method }
func (r *reporter) Context() context.Context { return r.c.Request().Context() }
func (r *reporter) URLPath() string { return r.c.Request().URL.Path }
func (r *reporter) StatusCode() int { return r.c.Response().Status }
func (r *reporter) BytesWritten() int64 { return r.c.Response().Size }