-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
144 lines (112 loc) · 3.3 KB
/
types.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
package metrics
import (
"fmt"
"io"
"sync"
"sync/atomic"
"time"
"github.com/VictoriaMetrics/metrics"
)
// this file exposes the metrics types from VictoriaMetrics/metrics package
// the Counter type is replaced with LazyCounter to avoid registering unused metrics
type Histogram = metrics.Histogram
func NewHistogram(name string) *Histogram {
return metrics.NewHistogram(name)
}
func GetOrCreateHistogram(name string) *Histogram {
return metrics.GetOrCreateHistogram(name)
}
type Gauge = metrics.Gauge
func NewGauge(name string, f func() float64) *Gauge {
return metrics.NewGauge(name, f)
}
func GetOrCreateGauge(name string, f func() float64) *Gauge {
return metrics.GetOrCreateGauge(name, f)
}
type Summary = metrics.Summary
func NewSummary(name string) *Summary {
return metrics.NewSummary(name)
}
func NewSummaryExt(name string, window time.Duration, quantiles []float64) *Summary {
return metrics.NewSummaryExt(name, window, quantiles)
}
func GetOrCreateSummaryExt(name string, window time.Duration, quantiles []float64) *Summary {
return metrics.GetOrCreateSummaryExt(name, window, quantiles)
}
type FloatCounter = metrics.FloatCounter
func NewFloatCounter(name string) *FloatCounter {
return metrics.NewFloatCounter(name)
}
func GetOrCreateFloatCounter(name string) *FloatCounter {
return metrics.GetOrCreateFloatCounter(name)
}
func WritePrometheus(w io.Writer, exposeProcessMetrics bool) {
metrics.WritePrometheus(w, exposeProcessMetrics)
}
var (
// a set of all lazy counters. We do not support removing counters from this set.
lazyCountersSet = make(map[string]*LazyCounter)
lazyCountersSetLock sync.Mutex
)
// LazyCounter is a counter that is lazily initialized when it is first used,
// to avoid registering unused metrics.
// It is safe to use from concurrent goroutines.
type LazyCounter struct {
name string
inner atomic.Pointer[metrics.Counter]
}
type Counter = LazyCounter
// NewCounter creates a new LazyCounter with the given name.
// If a counter with the given name already exists, the program panics.
func NewCounter(name string) *LazyCounter {
lazyCountersSetLock.Lock()
defer lazyCountersSetLock.Unlock()
if _, ok := lazyCountersSet[name]; ok {
panic(fmt.Errorf("lazy counter with name %s already exists", name))
}
return newCounterUnsafe(name)
}
func GetOrCreateCounter(name string) *LazyCounter {
lazyCountersSetLock.Lock()
defer lazyCountersSetLock.Unlock()
if c, ok := lazyCountersSet[name]; ok {
return c
}
return newCounterUnsafe(name)
}
func (mc *LazyCounter) Inc() {
mc.setActiveIfNeeded()
mc.inner.Load().Inc()
}
func (mc *LazyCounter) Dec() {
mc.setActiveIfNeeded()
mc.inner.Load().Dec()
}
func (mc *LazyCounter) Get() uint64 {
if !mc.IsActive() {
return 0
}
return mc.inner.Load().Get()
}
func (mc *LazyCounter) Set(n uint64) {
mc.setActiveIfNeeded()
mc.inner.Load().Set(n)
}
func (mc *LazyCounter) Add(n int) {
mc.setActiveIfNeeded()
mc.inner.Load().Add(n)
}
func (mc *LazyCounter) IsActive() bool {
return mc.inner.Load() != nil
}
func (mc *LazyCounter) setActiveIfNeeded() {
if mc.IsActive() {
return
}
counter := metrics.GetOrCreateCounter(mc.name)
mc.inner.Store(counter)
}
func newCounterUnsafe(name string) *LazyCounter {
lazyCountersSet[name] = &LazyCounter{name: name, inner: atomic.Pointer[metrics.Counter]{}}
return lazyCountersSet[name]
}