-
Notifications
You must be signed in to change notification settings - Fork 0
/
ginprometheus.go
137 lines (114 loc) · 3.22 KB
/
ginprometheus.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ginprometheus
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var defaultMetricPath = "/metrics"
// Prometheus contains the metrics gathered by the instance and its web path.
type Prometheus struct {
reqCnt *prometheus.CounterVec
reqDur, reqSz, resSz *prometheus.SummaryVec
ginRouter *gin.Engine
MetricsPath string
}
// New generates a new set of metrics with a certain subsystem name.
func New(subsystem string) *Prometheus {
p := &Prometheus{
MetricsPath: defaultMetricPath,
}
p.registerMetrics(subsystem)
return p
}
func (p *Prometheus) registerMetrics(subsystem string) {
p.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: "requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
[]string{"code", "method", "url"},
)
prometheus.MustRegister(p.reqCnt)
p.reqDur = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "request_duration_seconds",
Help: "The HTTP request latencies in seconds.",
},
[]string{"code", "method", "url"},
)
prometheus.MustRegister(p.reqDur)
p.reqSz = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "request_size_bytes",
Help: "The HTTP request sizes in bytes.",
},
[]string{"code", "method", "url"},
)
prometheus.MustRegister(p.reqSz)
p.resSz = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Subsystem: subsystem,
Name: "response_size_bytes",
Help: "The HTTP response sizes in bytes.",
},
[]string{"code", "method", "url"},
)
prometheus.MustRegister(p.resSz)
}
// Use adds the middleware to a gin engine.
func (p *Prometheus) Use(e *gin.Engine) {
p.ginRouter = e
e.Use(p.handlerFunc())
e.GET(p.MetricsPath, prometheusHandler())
}
func (p *Prometheus) handlerFunc() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.URL.String() == p.MetricsPath {
c.Next()
return
}
reqSz := make(chan int64)
go computeApproximateRequestSize(c.Request, reqSz)
start := time.Now()
c.Next()
status := strconv.Itoa(c.Writer.Status())
elapsed := float64(time.Since(start)) / float64(time.Second)
resSz := float64(c.Writer.Size())
p.reqDur.WithLabelValues(status, c.Request.Method, c.FullPath()).Observe(elapsed)
p.reqCnt.WithLabelValues(status, c.Request.Method, c.FullPath()).Inc()
p.reqSz.WithLabelValues(status, c.Request.Method, c.FullPath()).Observe(float64(<-reqSz))
p.resSz.WithLabelValues(status, c.Request.Method, c.FullPath()).Observe(resSz)
}
}
func prometheusHandler() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func computeApproximateRequestSize(r *http.Request, out chan int64) {
s := 0
if r.URL != nil {
s = len(r.URL.String())
}
s += len(r.Method)
s += len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
}
s += len(r.Host)
if r.ContentLength != -1 {
out <- int64(s) + r.ContentLength
return
}
out <- int64(s)
}