Add Reset(map[string]string) to Metric interface

Change-Id: I289cf8796adbd6ff55f23bba7730145329de00e1
This commit is contained in:
Bernerd Schaefer 2014-02-19 14:53:34 +01:00
parent 197e83f47e
commit 29ebb580db
6 changed files with 71 additions and 0 deletions

View File

@ -65,6 +65,14 @@ func (metric *counter) Set(labels map[string]string, value float64) float64 {
return value
}
func (metric *counter) Reset(labels map[string]string) {
metric.mutex.Lock()
defer metric.mutex.Unlock()
signature := labelsToSignature(labels)
delete(metric.values, signature)
}
func (metric *counter) ResetAll() {
metric.mutex.Lock()
defer metric.mutex.Unlock()

View File

@ -70,6 +70,24 @@ func testCounter(t tester) {
value: `{"type":"counter","value":[{"labels":{},"value":5}]}`,
},
},
{
in: input{
steps: []func(g Counter){
func(g Counter) {
g.Set(map[string]string{"handler": "/foo"}, 13)
},
func(g Counter) {
g.Set(map[string]string{"handler": "/bar"}, 17)
},
func(g Counter) {
g.Reset(map[string]string{"handler": "/bar"})
},
},
},
out: output{
value: `{"type":"counter","value":[{"labels":{"handler":"/foo"},"value":13}]}`,
},
},
{
in: input{
steps: []func(g Counter){

View File

@ -72,6 +72,14 @@ func (metric *gauge) Set(labels map[string]string, value float64) float64 {
return value
}
func (metric *gauge) Reset(labels map[string]string) {
metric.mutex.Lock()
defer metric.mutex.Unlock()
signature := labelsToSignature(labels)
delete(metric.values, signature)
}
func (metric *gauge) ResetAll() {
metric.mutex.Lock()
defer metric.mutex.Unlock()

View File

@ -70,6 +70,24 @@ func testGauge(t tester) {
value: `{"type":"gauge","value":[{"labels":{},"value":5}]}`,
},
},
{
in: input{
steps: []func(g Gauge){
func(g Gauge) {
g.Set(map[string]string{"handler": "/foo"}, 13)
},
func(g Gauge) {
g.Set(map[string]string{"handler": "/bar"}, 17)
},
func(g Gauge) {
g.Reset(map[string]string{"handler": "/bar"})
},
},
},
out: output{
value: `{"type":"gauge","value":[{"labels":{"handler":"/foo"},"value":13}]}`,
},
},
{
in: input{
steps: []func(g Gauge){

View File

@ -296,6 +296,23 @@ func (h *histogram) Purge() {
h.lastPurge = time.Now()
}
func (h *histogram) Reset(labels map[string]string) {
h.mutex.Lock()
defer h.mutex.Unlock()
signature := labelsToSignature(labels)
value, ok := h.values[signature]
if !ok {
return
}
for _, bucket := range value.buckets {
bucket.Reset()
}
delete(h.values, signature)
}
func (h *histogram) ResetAll() {
h.mutex.Lock()
defer h.mutex.Unlock()

View File

@ -16,6 +16,8 @@ import (
type Metric interface {
// Produce a JSON representation of the metric.
json.Marshaler
// Reset removes any stored values associated with a given labelset.
Reset(labels map[string]string)
// Reset the parent metrics and delete all child metrics.
ResetAll()
// Produce a human-consumable representation of the metric.