-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathiris.go
37 lines (27 loc) · 988 Bytes
/
iris.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
// Package iris is a helper package to get an Iris compatible middleware.
package iris
import (
"context"
"github.com/kataras/iris/v12"
"github.com/slok/go-http-metrics/middleware"
)
// Handler returns a Iris measuring middleware.
func Handler(handlerID string, m middleware.Middleware) iris.Handler {
return func(ctx iris.Context) {
c := context.WithValue(ctx.Request().Context(), middleware.HandlerIDCtx, handlerID)
req := ctx.Request().WithContext(c)
ctx.ResetRequest(req)
r := &reporter{ctx: ctx}
m.Measure(handlerID, r, func() {
ctx.Next()
})
}
}
type reporter struct {
ctx iris.Context
}
func (r *reporter) Method() string { return r.ctx.Method() }
func (r *reporter) Context() context.Context { return r.ctx.Request().Context() }
func (r *reporter) URLPath() string { return r.ctx.Path() }
func (r *reporter) StatusCode() int { return r.ctx.GetStatusCode() }
func (r *reporter) BytesWritten() int64 { return int64(r.ctx.ResponseWriter().Written()) }