Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 2116560

Browse files
committed
Added removal methods.
Closes #5.
1 parent 35ff224 commit 2116560

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

metrics.go

+40
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ func (c Counter) SetBatchFunc(key interface{}, init func(), f func() uint64) {
7777
}
7878
}
7979

80+
// Remove removes the given counter.
81+
func (c Counter) Remove() {
82+
cm.Lock()
83+
defer cm.Unlock()
84+
85+
gm.Lock()
86+
defer gm.Unlock()
87+
88+
delete(counters, string(c))
89+
delete(counterFuncs, string(c))
90+
delete(inits, string(c))
91+
}
92+
8093
// A Gauge is an instantaneous measurement of a value.
8194
//
8295
// Use a gauge to track metrics which increase and decrease (e.g., amount of
@@ -115,6 +128,15 @@ func (g Gauge) SetBatchFunc(key interface{}, init func(), f func() int64) {
115128
}
116129
}
117130

131+
// Remove removes the given counter.
132+
func (g Gauge) Remove() {
133+
gm.Lock()
134+
defer gm.Unlock()
135+
136+
delete(gauges, string(g))
137+
delete(inits, string(g))
138+
}
139+
118140
// Reset removes all existing counters and gauges.
119141
func Reset() {
120142
cm.Lock()
@@ -179,6 +201,7 @@ func NewHistogram(name string, minValue, maxValue int64, sigfigs int) *Histogram
179201
}
180202

181203
hist := &Histogram{
204+
name: name,
182205
hist: hdrhistogram.NewWindowed(5, minValue, maxValue, sigfigs),
183206
name: name,
184207
}
@@ -194,10 +217,27 @@ func NewHistogram(name string, minValue, maxValue int64, sigfigs int) *Histogram
194217
return hist
195218
}
196219

220+
// Remove removes the given histogram.
221+
func (h *Histogram) Remove() {
222+
223+
hm.Lock()
224+
defer hm.Unlock()
225+
226+
Gauge(h.name + ".P50").Remove()
227+
Gauge(h.name + ".P75").Remove()
228+
Gauge(h.name + ".P90").Remove()
229+
Gauge(h.name + ".P95").Remove()
230+
Gauge(h.name + ".P99").Remove()
231+
Gauge(h.name + ".P999").Remove()
232+
233+
delete(histograms, h.name)
234+
}
235+
197236
type hname string // unexported to prevent collisions
198237

199238
// A Histogram measures the distribution of a stream of values.
200239
type Histogram struct {
240+
name string
201241
hist *hdrhistogram.WindowedHistogram
202242
m *hdrhistogram.Histogram
203243
rw sync.RWMutex

metrics_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ func TestCounterBatchFunc(t *testing.T) {
6666
}
6767
}
6868

69+
func TestCounterRemove(t *testing.T) {
70+
metrics.Reset()
71+
72+
metrics.Counter("whee").Add()
73+
metrics.Counter("whee").Remove()
74+
75+
counters, _ := metrics.Snapshot()
76+
if v, ok := counters["whee"]; ok {
77+
t.Errorf("Counter was %v, but expected nothing", v)
78+
}
79+
}
80+
6981
func TestGaugeValue(t *testing.T) {
7082
metrics.Reset()
7183

@@ -90,6 +102,18 @@ func TestGaugeFunc(t *testing.T) {
90102
}
91103
}
92104

105+
func TestGaugeRemove(t *testing.T) {
106+
metrics.Reset()
107+
108+
metrics.Gauge("whee").Set(1)
109+
metrics.Gauge("whee").Remove()
110+
111+
_, gauges := metrics.Snapshot()
112+
if v, ok := gauges["whee"]; ok {
113+
t.Errorf("Gauge was %v, but expected nothing", v)
114+
}
115+
}
116+
93117
func TestHistogram(t *testing.T) {
94118
metrics.Reset()
95119

@@ -127,6 +151,18 @@ func TestHistogram(t *testing.T) {
127151
}
128152
}
129153

154+
func TestHistogramRemove(t *testing.T) {
155+
metrics.Reset()
156+
157+
h := metrics.NewHistogram("heyo", 1, 1000, 3)
158+
h.Remove()
159+
160+
_, gauges := metrics.Snapshot()
161+
if v, ok := gauges["heyo.P50"]; ok {
162+
t.Errorf("Gauge was %v, but expected nothing", v)
163+
}
164+
}
165+
130166
func BenchmarkCounterAdd(b *testing.B) {
131167
metrics.Reset()
132168

0 commit comments

Comments
 (0)