-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
80 lines (66 loc) · 1.93 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
package middleware
import (
"net/http"
"time"
"github.com/patrickkoss/grpc-gateway-example/internal/adapter/logging"
"github.com/patrickkoss/grpc-gateway-example/internal/adapter/metrics_collector"
)
func MetricsMiddleware(
collector metrics_collector.HttpApiMetrics,
logger logging.Logger,
) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
handleRequestWithMetrics(collector, logger, next, w, r)
}
return http.HandlerFunc(fn)
}
}
func handleRequestWithMetrics(
collector metrics_collector.HttpApiMetrics,
logger logging.Logger,
next http.Handler,
w http.ResponseWriter,
r *http.Request,
) {
defer logRecovery(logger, w)
start := time.Now()
wrapped := wrapResponseWriter(w)
next.ServeHTTP(wrapped, r)
collectResponseStatus(collector, wrapped.status)
status := getStatusOrDefault(wrapped.status)
collectRequestMetrics(collector, r, status, start)
}
func logRecovery(logger logging.Logger, w http.ResponseWriter) {
if err := recover(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
logger.Error("failed to handle request metrics")
}
}
func collectResponseStatus(collector metrics_collector.HttpApiMetrics, status int) {
if status < 500 && status >= 400 {
collector.Collect400TotalRequests()
} else if status >= 500 {
collector.Collect500TotalRequests()
}
}
func getStatusOrDefault(status int) int {
if status == 0 {
return http.StatusOK
}
return status
}
func collectRequestMetrics(
collector metrics_collector.HttpApiMetrics,
r *http.Request,
status int,
start time.Time,
) {
collector.CollectTotalRequests()
method, path := r.Method, r.URL.EscapedPath()
duration := time.Since(start).Seconds()
contentLength := float64(r.ContentLength)
collector.CollectRequest(method, path, status)
collector.CollectRequestDuration(method, path, duration)
collector.CollectRequestContentLength(method, path, contentLength)
}