/
metrics.go
49 lines (39 loc) · 1.24 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
package middleware
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/metrics"
)
type MetricsMiddleware struct {
metrics.Emitter
}
// Metric records request metrics for tracking
func (mm MetricsMiddleware) Metrics(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiVersion := r.URL.Query().Get(api.APIVersionKey)
t := time.Now()
w = &logResponseWriter{ResponseWriter: w, statusCode: http.StatusOK}
h.ServeHTTP(w, r)
//get the route pattern that matched
rctx := chi.RouteContext(r.Context())
routePattern := strings.Join(rctx.RoutePatterns, "")
mm.EmitGauge("frontend.count", 1, map[string]string{
"verb": r.Method,
"api-version": apiVersion,
"code": strconv.Itoa(w.(*logResponseWriter).statusCode),
"route": routePattern,
})
mm.EmitGauge("frontend.duration", time.Since(t).Milliseconds(), map[string]string{
"verb": r.Method,
"api-version": apiVersion,
"code": strconv.Itoa(w.(*logResponseWriter).statusCode),
"route": routePattern,
})
})
}