-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstats.go
More file actions
355 lines (302 loc) · 11.9 KB
/
stats.go
File metadata and controls
355 lines (302 loc) · 11.9 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package warc
import (
"fmt"
"sort"
"strings"
"sync"
"sync/atomic"
)
const (
// totalDataWritten is the name of the metric that tracks the total data written to WARC files.
totalDataWritten string = "total_data_written"
totalDataWrittenHelp string = "Total data written to WARC files in bytes"
// localDedupedBytesTotal is the name of the metric that tracks the total bytes deduped using local dedupe.
localDedupedBytesTotal string = "local_deduped_bytes_total"
localDedupedBytesTotalHelp string = "Total bytes deduped using local dedupe"
// localDedupedTotal is the name of the metric that tracks the total records deduped using local dedupe.
localDedupedTotal string = "local_deduped_total"
localDedupedTotalHelp string = "Total records deduped using local dedupe"
// doppelgangerDedupedBytesTotal is the name of the metric that tracks the total bytes deduped using Doppelganger.
doppelgangerDedupedBytesTotal string = "doppelganger_deduped_bytes_total"
doppelgangerDedupedBytesTotalHelp string = "Total bytes deduped using Doppelganger"
// doppelgangerDedupedTotal is the name of the metric that tracks the total records deduped using Doppelganger.
doppelgangerDedupedTotal string = "doppelganger_deduped_total"
doppelgangerDedupedTotalHelp string = "Total records deduped using Doppelganger"
// cdxDedupedBytesTotal is the name of the metric that tracks the total bytes deduped using CDX.
cdxDedupedBytesTotal string = "cdx_deduped_bytes_total"
cdxDedupedBytesTotalHelp string = "Total bytes deduped using CDX"
// cdxDedupedTotal is the name of the metric that tracks the total records deduped using CDX.
cdxDedupedTotal string = "cdx_deduped_total"
cdxDedupedTotalHelp string = "Total records deduped using CDX"
// proxyRequestsTotal is the name of the metric that tracks the total number of requests gone through a proxy.
proxyRequestsTotal string = "proxy_requests_total"
proxyRequestsTotalHelp string = "Total number of requests gone through a proxy"
// proxyErrorsTotal is the name of the metric that tracks the total number of errors occurred with a proxy.
proxyErrorsTotal string = "proxy_errors_total"
proxyErrorsTotalHelp string = "Total number of errors occurred with a proxy"
// proxyLastUsedNanoseconds is the name of the metric that tracks the last time a proxy was used.
proxyLastUsedNanoseconds string = "proxy_last_used_nanoseconds"
proxyLastUsedNanosecondsHelp string = "Last time a proxy was used in seconds (unix timestamp ns)"
)
// Labels represents Prometheus-style label values as key-value pairs.
// Labels are used with WithLabels() to specify concrete label values when recording metrics.
//
// Example usage with labels:
//
// registry := newLocalRegistry()
//
// // Register a counter with label names (declares which dimensions the metric tracks)
// httpRequests := registry.RegisterCounter("http_requests_total", "Total HTTP requests", []string{"method", "status"})
//
// // Use WithLabels to specify label values when recording
// httpRequests.WithLabels(Labels{"method": "GET", "status": "200"}).Inc()
// httpRequests.WithLabels(Labels{"method": "POST", "status": "201"}).Add(5)
// httpRequests.WithLabels(Labels{"method": "GET", "status": "404"}).Inc()
//
// // Each unique combination of label values creates a separate metric series
// // The counter with method=GET,status=200 has value 1
// // The counter with method=POST,status=201 has value 5
// // The counter with method=GET,status=404 has value 1
//
// Example usage without labels:
//
// // For metrics without labels, pass nil or empty slice when registering
// totalCounter := registry.RegisterCounter("requests_total", "All requests", nil)
// // WithLabels(nil) is required but the labels parameter is ignored
// totalCounter.WithLabels(nil).Inc()
//
// Labels are internally sorted by key to ensure consistent metric identification
// regardless of the order in which they are specified. This means:
//
// Labels{"method": "GET", "status": "200"} == Labels{"status": "200", "method": "GET"}
type Labels map[string]string
// labelsToString converts labels to a consistent string representation for use as map keys.
// Labels are sorted by key to ensure consistent ordering.
func labelsToString(labels Labels) string {
if len(labels) == 0 {
return ""
}
// Sort keys for consistent ordering
keys := make([]string, 0, len(labels))
for k := range labels {
keys = append(keys, k)
}
sort.Strings(keys)
// Build string representation
parts := make([]string, 0, len(labels))
for _, k := range keys {
parts = append(parts, fmt.Sprintf("%s=%q", k, labels[k]))
}
return strings.Join(parts, ",")
}
// makeMetricKey creates a unique key for a metric with labels.
func makeMetricKey(name string, labels Labels) string {
if len(labels) == 0 {
return name
}
return name + "{" + labelsToString(labels) + "}"
}
// RegistryOpts is an interface that provides a WithLabels method to get a metric with specific labels.
type RegistryOpts[T any] interface {
// WithLabels returns a Counter for the given label values.
// If the counter has no labels, labels parameter is ignored.
WithLabels(labels Labels) T
}
// Counter represents a monotonically increasing metric.
type Counter interface {
RegistryOpts[Counter]
// Inc increments the counter by 1.
Inc()
// Add adds the given value to the counter.
Add(value int64)
// Get returns the current value of the counter.
// This is used to support unit-testing of the metrics.
Get() int64
}
// Gauge represents a metric that can go up or down.
type Gauge interface {
RegistryOpts[Gauge]
// Set sets the gauge to the given value.
Set(value int64)
// Inc increments the gauge by 1.
Inc()
// Dec decrements the gauge by 1.
Dec()
// Add adds the given value to the gauge.
Add(value int64)
// Sub subtracts the given value from the gauge.
Sub(value int64)
// Get returns the current value of the gauge.
// This is used to support unit-testing of the metrics.
Get() int64
}
// Histogram represents a metric for observing distributions of values.
type Histogram interface {
RegistryOpts[Histogram]
// Observe adds a single observation to the histogram.
Observe(value int64)
}
// StatsRegistry provides a registry for external libraries to register and update metrics.
// The StatsRegistry implementation is expected to be thread-safe so that gowarc can safely register and update metrics from multiple goroutines.
type StatsRegistry interface {
// RegisterCounter registers a new counter metric with optional label names.
// labelNames specifies which labels this counter will use (e.g., []string{"method", "status"}).
// Returns a Counter that can be used with WithLabels() to specify label values.
// If labelNames is nil or empty, returns a Counter that ignores labels.
RegisterCounter(name, help string, labelNames []string) Counter
// RegisterGauge registers a new gauge metric with optional label names.
// labelNames specifies which labels this gauge will use (e.g., []string{"location", "type"}).
// Returns a Gauge that can be used with WithLabels() to specify label values.
// If labelNames is nil or empty, returns a Gauge that ignores labels.
RegisterGauge(name, help string, labelNames []string) Gauge
// RegisterHistogram registers a new histogram metric with the given buckets and optional label names.
// If buckets is nil, uses Prometheus default buckets.
// labelNames specifies which labels this histogram will use (e.g., []string{"endpoint", "method"}).
// Returns a Histogram that can be used with WithLabels() to specify label values.
// If labelNames is nil or empty, returns a Histogram that ignores labels.
RegisterHistogram(name, help string, buckets []int64, labelNames []string) Histogram
}
// Nil-safe implementations for when no StatsRegistry is provided.
type localCounter struct {
v atomic.Int64
}
func (n *localCounter) WithLabels(_ Labels) Counter { return n }
func (n *localCounter) Inc() { n.v.Add(1) }
func (n *localCounter) Add(value int64) { n.v.Add(value) }
func (n *localCounter) Get() int64 { return n.v.Load() }
type localGauge struct {
v atomic.Int64
}
func (n *localGauge) WithLabels(_ Labels) Gauge { return n }
func (n *localGauge) Set(value int64) { n.v.Store(value) }
func (n *localGauge) Inc() { n.v.Add(1) }
func (n *localGauge) Dec() { n.v.Add(-1) }
func (n *localGauge) Add(value int64) { n.v.Add(value) }
func (n *localGauge) Sub(value int64) { n.v.Add(-value) }
func (n *localGauge) Get() int64 { return n.v.Load() }
type localHistogram struct{}
func (n *localHistogram) WithLabels(_ Labels) Histogram { return n }
func (n *localHistogram) Observe(_ int64) {}
// labeledCounter implements Counter with label support
type labeledCounter struct {
name string
registry *localRegistry
}
func (l *labeledCounter) WithLabels(labels Labels) Counter {
return l.registry.getOrCreateCounter(l.name, labels)
}
func (l *labeledCounter) Inc() {
l.registry.getOrCreateCounter(l.name, nil).Inc()
}
func (l *labeledCounter) Add(value int64) {
l.registry.getOrCreateCounter(l.name, nil).Add(value)
}
func (l *labeledCounter) Get() int64 {
return l.registry.getOrCreateCounter(l.name, nil).Get()
}
// labeledGauge implements Gauge with label support
type labeledGauge struct {
name string
registry *localRegistry
}
func (l *labeledGauge) WithLabels(labels Labels) Gauge {
return l.registry.getOrCreateGauge(l.name, labels)
}
func (l *labeledGauge) Set(value int64) {
l.registry.getOrCreateGauge(l.name, nil).Set(value)
}
func (l *labeledGauge) Inc() {
l.registry.getOrCreateGauge(l.name, nil).Inc()
}
func (l *labeledGauge) Dec() {
l.registry.getOrCreateGauge(l.name, nil).Dec()
}
func (l *labeledGauge) Add(value int64) {
l.registry.getOrCreateGauge(l.name, nil).Add(value)
}
func (l *labeledGauge) Sub(value int64) {
l.registry.getOrCreateGauge(l.name, nil).Sub(value)
}
func (l *labeledGauge) Get() int64 {
return l.registry.getOrCreateGauge(l.name, nil).Get()
}
// labeledHistogram implements Histogram with label support
type labeledHistogram struct {
name string
registry *localRegistry
}
func (l *labeledHistogram) WithLabels(labels Labels) Histogram {
return l.registry.getOrCreateHistogram(l.name, labels)
}
func (l *labeledHistogram) Observe(value int64) {
l.registry.getOrCreateHistogram(l.name, nil).Observe(value)
}
type localRegistry struct {
sync.Mutex
gauges map[string]*localGauge
counters map[string]*localCounter
histograms map[string]*localHistogram
}
func newLocalRegistry() *localRegistry {
return &localRegistry{}
}
func (n *localRegistry) getOrCreateCounter(name string, labels Labels) Counter {
n.Lock()
defer n.Unlock()
if n.counters == nil {
n.counters = make(map[string]*localCounter)
}
key := makeMetricKey(name, labels)
if c, ok := n.counters[key]; ok {
return c
}
c := &localCounter{}
n.counters[key] = c
return c
}
func (n *localRegistry) getOrCreateGauge(name string, labels Labels) Gauge {
n.Lock()
defer n.Unlock()
if n.gauges == nil {
n.gauges = make(map[string]*localGauge)
}
key := makeMetricKey(name, labels)
if g, ok := n.gauges[key]; ok {
return g
}
g := &localGauge{}
n.gauges[key] = g
return g
}
func (n *localRegistry) getOrCreateHistogram(name string, labels Labels) Histogram {
n.Lock()
defer n.Unlock()
if n.histograms == nil {
n.histograms = make(map[string]*localHistogram)
}
key := makeMetricKey(name, labels)
if h, ok := n.histograms[key]; ok {
return h
}
h := &localHistogram{}
n.histograms[key] = h
return h
}
func (n *localRegistry) RegisterCounter(name, _ string, _ []string) Counter {
return &labeledCounter{
name: name,
registry: n,
}
}
func (n *localRegistry) RegisterGauge(name, _ string, _ []string) Gauge {
return &labeledGauge{
name: name,
registry: n,
}
}
func (n *localRegistry) RegisterHistogram(name, _ string, _ []int64, _ []string) Histogram {
return &labeledHistogram{
name: name,
registry: n,
}
}