-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
39 lines (30 loc) · 879 Bytes
/
metrics.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
package middleware
import (
"context"
"github.com/gin-gonic/gin"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
)
// Metrics returns a Gin measuring middleware.
func Metrics() gin.HandlerFunc {
handlerID := ""
m := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})
return func(c *gin.Context) {
r := &reporter{c: c}
m.Measure(handlerID, r, func() {
c.Next()
})
}
}
type reporter struct {
c *gin.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.FullPath()
}
func (r *reporter) StatusCode() int { return r.c.Writer.Status() }
func (r *reporter) BytesWritten() int64 { return int64(r.c.Writer.Size()) }