-
Notifications
You must be signed in to change notification settings - Fork 1
/
gin.go
66 lines (54 loc) · 1.61 KB
/
gin.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package prome
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
)
// MiddlewareRequestCount returns a gin HandlerFunc which can be used as
// middleware to capture request count.
func (c *Client) MiddlewareRequestCount(metricsName string) gin.HandlerFunc {
if metricsName == "" {
metricsName = fmt.Sprintf("%s_request_count", c.ServiceName)
}
cv := c.AddCounterVec(prometheus.CounterOpts{
Name: metricsName,
Help: "Request count",
}, []string{"method", "path"})
return func(c *gin.Context) {
cv.WithLabelValues(c.Request.Method, c.FullPath()).Inc()
c.Next()
}
}
// DefaultRequestDurationSummaryObjectives represents objectives of request
// duration middleware summary.
var DefaultRequestDurationSummaryObjectives = map[float64]float64{
0.5: 0.05,
0.9: 0.01,
0.95: 0.005,
0.99: 0.001,
}
// MiddlewareRequestDuration returns a gin handler which can be used as
// middleware to capture request duration summary.
func (c *Client) MiddlewareRequestDuration(
metricsName string, objectives map[float64]float64,
) gin.HandlerFunc {
if metricsName == "" {
metricsName = fmt.Sprintf("%s_request_duration", c.ServiceName)
}
if objectives == nil {
objectives = DefaultRequestDurationSummaryObjectives
}
sv := c.AddSummaryVec(prometheus.SummaryOpts{
Name: metricsName,
Objectives: objectives,
Help: "Request duration (Unit: ns)",
}, []string{"method", "path"})
return func(c *gin.Context) {
startTime := time.Now()
c.Next()
sv.WithLabelValues(
c.Request.Method, c.FullPath(),
).Observe(float64(time.Since(startTime).Nanoseconds()))
}
}