-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
123 lines (103 loc) · 3.35 KB
/
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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package metrics
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/baa.v1"
)
var (
apiResponseTime *prometheus.SummaryVec
apiResponseSize *prometheus.SummaryVec
apiRequestTotal *prometheus.CounterVec
apiRequestBytes *prometheus.CounterVec
)
//Config prometheus metrics config
type Config struct {
// metrics path
MetricsPath string
// prometheus namespace
Namespace string
}
// Metrics return metrics middleware
func Metrics(config Config) baa.HandlerFunc {
// registry metrics
if config.Namespace == "" {
config.Namespace = "default"
}
if config.MetricsPath == "" {
config.MetricsPath = "/metrics"
}
initMetrics(config.Namespace)
return func(c *baa.Context) {
start := time.Now()
c.Next()
// don't keep track of the requests of promethues collect
if c.Req.RequestURI == config.MetricsPath {
return
}
collectAPIRequestBytes(c.Req.Method, c.Req.RequestURI, strconv.Itoa(c.Resp.Status()), c.Req.RemoteAddr, float64(c.Req.ContentLength))
collectAPIRequestTotal(c.Req.Method, c.Req.RequestURI, strconv.Itoa(c.Resp.Status()), c.Req.RemoteAddr)
collectAPIResponseTime(c.Req.Method, c.Req.RequestURI, strconv.Itoa(c.Resp.Status()), c.Req.RemoteAddr, float64(time.Since(start).Seconds()*1000))
collectAPIResponseSize(c.Req.Method, c.Req.RequestURI, strconv.Itoa(c.Resp.Status()), c.Req.RemoteAddr, float64(c.Resp.Size()))
}
}
func initMetrics(namespace string) {
apiResponseTime = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: "api",
Name: "response_time",
Help: "Reponse Time of Each Request in Microsecond.",
},
[]string{"method", "endpoint", "status", "host"},
)
apiResponseSize = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: namespace,
Subsystem: "api",
Name: "response_size",
Help: "Reponse Bytes Size of Each Request.",
},
[]string{"method", "endpoint", "status", "host"},
)
apiRequestTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "api",
Name: "request_total",
Help: "Total Number of Each Request.",
},
[]string{"method", "endpoint", "status", "host"},
)
apiRequestBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "api",
Name: "reuest_bytes",
Help: "Total Data Bytes of Each Request.",
},
[]string{"method", "endpoint", "status", "host"},
)
prometheus.MustRegister(
apiResponseTime,
apiResponseSize,
apiRequestBytes,
apiRequestTotal,
)
}
// collectAPIResponseTime collect api resp time
func collectAPIResponseTime(method, endpoint, status, host string, value float64) {
apiResponseTime.WithLabelValues(method, endpoint, status, host).Observe(value)
}
// collectAPIResponseSize collect api resp size
func collectAPIResponseSize(method, endpoint, status, host string, value float64) {
apiResponseSize.WithLabelValues(method, endpoint, status, host).Observe(value)
}
// collectAPIRequestTotal collect api req total
func collectAPIRequestTotal(method, endpoint, status, host string) {
apiRequestTotal.WithLabelValues(method, endpoint, status, host).Inc()
}
// collectAPIRequestBytes collect api req bytes
func collectAPIRequestBytes(method, endpoint, status, host string, value float64) {
apiRequestBytes.WithLabelValues(method, endpoint, status, host).Add(value)
}