-
Notifications
You must be signed in to change notification settings - Fork 88
/
metrics.go
158 lines (128 loc) · 4.15 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package search
import (
"errors"
"net/http"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var postsReceived = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_posts_received",
Help: "Number of posts received",
})
var postsIndexed = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_posts_indexed",
Help: "Number of posts indexed",
})
var postsFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_posts_failed",
Help: "Number of posts that failed indexing",
})
var postsDeleted = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_posts_deleted",
Help: "Number of posts deleted",
})
var profilesReceived = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_profiles_received",
Help: "Number of profiles received",
})
var profilesIndexed = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_profiles_indexed",
Help: "Number of profiles indexed",
})
var profilesFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_profiles_failed",
Help: "Number of profiles that failed indexing",
})
var profilesDeleted = promauto.NewCounter(prometheus.CounterOpts{
Name: "search_profiles_deleted",
Help: "Number of profiles deleted",
})
var currentSeq = promauto.NewGauge(prometheus.GaugeOpts{
Name: "search_current_seq",
Help: "Current sequence number",
})
var reqSz = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_size_bytes",
Help: "A histogram of request sizes for requests.",
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
}, []string{"code", "method", "path", "extras"})
var reqDur = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "A histogram of latencies for requests.",
Buckets: prometheus.ExponentialBuckets(0.0001, 2, 18),
}, []string{"code", "method", "path", "extras"})
var reqCnt = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "A counter for requests to the wrapped handler.",
}, []string{"code", "method", "path", "extras"})
var resSz = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "http_response_size_bytes",
Help: "A histogram of response sizes for requests.",
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
}, []string{"code", "method", "path", "extras"})
// MetricsMiddleware defines handler function for metrics middleware
func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
path := c.Path()
if path == "/metrics" || path == "/_health" {
return next(c)
}
start := time.Now()
requestSize := computeApproximateRequestSize(c.Request())
err := next(c)
status := c.Response().Status
if err != nil {
var httpError *echo.HTTPError
if errors.As(err, &httpError) {
status = httpError.Code
}
if status == 0 || status == http.StatusOK {
status = http.StatusInternalServerError
}
}
elapsed := float64(time.Since(start)) / float64(time.Second)
statusStr := strconv.Itoa(status)
method := c.Request().Method
responseSize := float64(c.Response().Size)
// Custom label for Typeahead search queries
typeahead := false
if q := strings.TrimSpace(c.QueryParam("typeahead")); q == "true" || q == "1" || q == "y" {
typeahead = true
}
labels := []string{statusStr, method, path}
if typeahead {
labels = append(labels, "typeahead")
} else {
labels = append(labels, "_none")
}
reqDur.WithLabelValues(labels...).Observe(elapsed)
reqCnt.WithLabelValues(labels...).Inc()
reqSz.WithLabelValues(labels...).Observe(float64(requestSize))
resSz.WithLabelValues(labels...).Observe(responseSize)
return err
}
}
func computeApproximateRequestSize(r *http.Request) int {
s := 0
if r.URL != nil {
s = len(r.URL.Path)
}
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)
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
if r.ContentLength != -1 {
s += int(r.ContentLength)
}
return s
}