-
Notifications
You must be signed in to change notification settings - Fork 8
/
metrics.go
166 lines (153 loc) · 6.35 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
159
160
161
162
163
164
165
166
// SPDX-FileCopyrightText: 2022 Marshall Wace <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/mailgun/groupcache/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
metricsPort int
httpRequestsMetric *prometheus.CounterVec
jwtRequestsMetric *prometheus.CounterVec
httpRequestsLatencyMetric *prometheus.GaugeVec
groupGetsMetric prometheus.Gauge
groupCacheHitsMetric prometheus.Gauge
groupPeersGetHighestLatencyMetric prometheus.Gauge
groupPeerLoadsMetric prometheus.Gauge
groupPeerErrorsMetric prometheus.Gauge
groupLoadsMetric prometheus.Gauge
groupLoadsDedupedMetric prometheus.Gauge
groupLocalLoadsMetric prometheus.Gauge
groupLocalLoadErrsMetric prometheus.Gauge
groupServerRequestsMetric prometheus.Gauge
cacheBytesMetric *prometheus.GaugeVec
cacheItemsMetric *prometheus.GaugeVec
cacheGetsMetric *prometheus.GaugeVec
cacheHitsMetric *prometheus.GaugeVec
cacheEvictionsMetric *prometheus.GaugeVec
)
func initMetrics() {
httpRequestsMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "cachenator_http_requests_total",
Help: "Total number of HTTP requests",
}, []string{"method", "path", "status"})
jwtRequestsMetric = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "cachenator_jwt_requests_total",
Help: "Total number of JWT-authenticated HTTP requests",
}, []string{"success", "error"})
httpRequestsLatencyMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_http_requests_latency",
Help: "HTTP requests latency",
}, []string{"method", "path", "status"})
groupGetsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_gets_total",
Help: "Total number of get requests",
})
groupCacheHitsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_hits_total",
Help: "Total number of both main and hot cache hits",
})
groupPeersGetHighestLatencyMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_peers_get_highest_latency",
Help: "Highest latency get request from peers",
})
groupPeerLoadsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_peer_loads_total",
Help: "Total number of remote loads or remote cache hits",
})
groupPeerErrorsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_peer_errors_total",
Help: "Total number of peer errors",
})
groupLoadsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_loads_total",
Help: "Total number of both local and remote cache loads",
})
groupLoadsDedupedMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_loads_deduped_total",
Help: "Total number of deduplicated cache loads",
})
groupLocalLoadsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_local_loads_total",
Help: "Total number of local cache loads",
})
groupLocalLoadErrsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_local_load_errors_total",
Help: "Total number of local cache load errors",
})
groupServerRequestsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cachenator_server_requests_total",
Help: "Total number of gets from other peers",
})
cacheBytesMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_cache_bytes",
Help: "Current (main/hot) cache bytes",
}, []string{"type"})
cacheItemsMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_cache_items",
Help: "Current (main/hot) cache items",
}, []string{"type"})
cacheGetsMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_cache_gets_total",
Help: "Total number of (main/hot) cache get requests",
}, []string{"type"})
cacheHitsMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_cache_hits_total",
Help: "Total number of (main/hot) cache hits",
}, []string{"type"})
cacheEvictionsMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "cachenator_cache_evictions_total",
Help: "Total number of (main/hot) cache evictions",
}, []string{"type"})
}
func collectMetrics() {
cacheTypes := []groupcache.CacheType{groupcache.MainCache, groupcache.HotCache}
for {
groupGetsMetric.Set(float64(cacheGroup.Stats.Gets.Get()))
groupCacheHitsMetric.Set(float64(cacheGroup.Stats.CacheHits.Get()))
groupPeersGetHighestLatencyMetric.Set(
float64(cacheGroup.Stats.GetFromPeersLatencyLower.Get()))
groupPeerLoadsMetric.Set(float64(cacheGroup.Stats.PeerLoads.Get()))
groupPeerErrorsMetric.Set(float64(cacheGroup.Stats.PeerErrors.Get()))
groupLoadsMetric.Set(float64(cacheGroup.Stats.Loads.Get()))
groupLoadsDedupedMetric.Set(float64(cacheGroup.Stats.LoadsDeduped.Get()))
groupLocalLoadsMetric.Set(float64(cacheGroup.Stats.LocalLoads.Get()))
groupLocalLoadErrsMetric.Set(float64(cacheGroup.Stats.LocalLoadErrs.Get()))
groupServerRequestsMetric.Set(float64(cacheGroup.Stats.ServerRequests.Get()))
for _, cacheType := range cacheTypes {
cacheBytesMetric.WithLabelValues(cacheTypeName(cacheType)).Set(
float64(cacheGroup.CacheStats(cacheType).Bytes))
cacheItemsMetric.WithLabelValues(cacheTypeName(cacheType)).Set(
float64(cacheGroup.CacheStats(cacheType).Items))
cacheGetsMetric.WithLabelValues(cacheTypeName(cacheType)).Set(
float64(cacheGroup.CacheStats(cacheType).Gets))
cacheHitsMetric.WithLabelValues(cacheTypeName(cacheType)).Set(
float64(cacheGroup.CacheStats(cacheType).Hits))
cacheEvictionsMetric.WithLabelValues(cacheTypeName(cacheType)).Set(
float64(cacheGroup.CacheStats(cacheType).Evictions))
}
time.Sleep(10 * time.Second)
}
}
func cacheTypeName(cacheType groupcache.CacheType) string {
if cacheType == groupcache.MainCache {
return "main"
}
return "hot"
}
func runMetricsServer() {
http.Handle("/metrics", promhttp.Handler())
addr := fmt.Sprintf("127.0.0.1:%d", metricsPort)
if os.Getenv("GIN_MODE") == "release" {
addr = fmt.Sprintf("0.0.0.0:%d", metricsPort)
}
log.Infof("Prometheus metrics HTTP server listening at %s/metrics", addr)
http.ListenAndServe(addr, nil)
}