/
middleware.go
98 lines (81 loc) · 3.02 KB
/
middleware.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package prometheus
import (
"net/http"
"strings"
"sync"
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type MetricsManager struct {
prometheusMetrics *Metrics
routers struct {
data []*httprouter.Router
sync.Mutex
}
}
func NewMetricsManager(app, version, hash, buildTime string) *MetricsManager {
return NewMetricsManagerWithPrefix(app, "", version, hash, buildTime)
}
// NewMetricsManagerWithPrefix creates MetricsManager that uses metricsPrefix parameters as a prefix
// for all metrics registered within this middleware. Constants HttpMetrics or GrpcMetrics can be used
// respectively. Setting empty string in metricsPrefix will be equivalent to calling NewMetricsManager.
func NewMetricsManagerWithPrefix(app, metricsPrefix, version, hash, buildTime string) *MetricsManager {
return &MetricsManager{
prometheusMetrics: NewMetrics(app, metricsPrefix, version, hash, buildTime),
}
}
// Main middleware method to collect metrics for Prometheus.
func (pmm *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
pmm.prometheusMetrics.Instrument(rw, next, pmm.getLabelForPath(r))(rw, r)
}
func (pmm *MetricsManager) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
f := grpcPrometheus.StreamServerInterceptor
return f(srv, ss, info, handler)
}
func (pmm *MetricsManager) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
f := grpcPrometheus.UnaryServerInterceptor
return f(ctx, req, info, handler)
}
func (pmm *MetricsManager) Register(server *grpc.Server) {
grpcPrometheus.Register(server)
}
func (pmm *MetricsManager) RegisterRouter(router *httprouter.Router) {
pmm.routers.Lock()
defer pmm.routers.Unlock()
pmm.routers.data = append(pmm.routers.data, router)
}
func (pmm *MetricsManager) getLabelForPath(r *http.Request) string {
// looking for a match in one of registered routers
pmm.routers.Lock()
defer pmm.routers.Unlock()
for _, router := range pmm.routers.data {
handler, params, _ := router.Lookup(r.Method, r.URL.Path)
if handler != nil {
return reconstructEndpoint(r.URL.Path, params)
}
}
return "{unmatched}"
}
// To reduce cardinality of labels, values of matched path parameters must be replaced with {param}
func reconstructEndpoint(path string, params httprouter.Params) string {
// if map is empty, then nothing to change in the path
if len(params) == 0 {
return path
}
// construct a list of parameter values
paramValues := make(map[string]struct{}, len(params))
for _, param := range params {
paramValues[param.Value] = struct{}{}
}
parts := strings.Split(path, "/")
for index, part := range parts {
if _, ok := paramValues[part]; ok {
parts[index] = "{param}"
}
}
return strings.Join(parts, "/")
}