2016-09-02 22:05:09 +00:00
|
|
|
// Copyright 2016 Circonus, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
package circonusgometrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/circonus-labs/circonusllhist"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reset removes all existing counters and gauges.
|
|
|
|
func (m *CirconusMetrics) Reset() {
|
|
|
|
m.cm.Lock()
|
|
|
|
defer m.cm.Unlock()
|
|
|
|
|
|
|
|
m.cfm.Lock()
|
|
|
|
defer m.cfm.Unlock()
|
|
|
|
|
|
|
|
m.gm.Lock()
|
|
|
|
defer m.gm.Unlock()
|
|
|
|
|
|
|
|
m.gfm.Lock()
|
|
|
|
defer m.gfm.Unlock()
|
|
|
|
|
|
|
|
m.hm.Lock()
|
|
|
|
defer m.hm.Unlock()
|
|
|
|
|
|
|
|
m.tm.Lock()
|
|
|
|
defer m.tm.Unlock()
|
|
|
|
|
|
|
|
m.tfm.Lock()
|
|
|
|
defer m.tfm.Unlock()
|
|
|
|
|
|
|
|
m.counters = make(map[string]uint64)
|
|
|
|
m.counterFuncs = make(map[string]func() uint64)
|
2017-10-27 19:06:04 +00:00
|
|
|
m.gauges = make(map[string]interface{})
|
2016-07-22 19:49:23 +00:00
|
|
|
m.gaugeFuncs = make(map[string]func() int64)
|
|
|
|
m.histograms = make(map[string]*Histogram)
|
|
|
|
m.text = make(map[string]string)
|
|
|
|
m.textFuncs = make(map[string]func() string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// snapshot returns a copy of the values of all registered counters and gauges.
|
2017-10-27 19:06:04 +00:00
|
|
|
func (m *CirconusMetrics) snapshot() (c map[string]uint64, g map[string]interface{}, h map[string]*circonusllhist.Histogram, t map[string]string) {
|
2017-01-04 21:47:38 +00:00
|
|
|
c = m.snapCounters()
|
|
|
|
g = m.snapGauges()
|
|
|
|
h = m.snapHistograms()
|
|
|
|
t = m.snapText()
|
2016-07-22 19:49:23 +00:00
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-16 18:31:07 +00:00
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
func (m *CirconusMetrics) snapCounters() map[string]uint64 {
|
2017-01-27 01:16:19 +00:00
|
|
|
m.cm.Lock()
|
|
|
|
defer m.cm.Unlock()
|
|
|
|
m.cfm.Lock()
|
|
|
|
defer m.cfm.Unlock()
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
c := make(map[string]uint64, len(m.counters)+len(m.counterFuncs))
|
2016-12-16 18:31:07 +00:00
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
for n, v := range m.counters {
|
|
|
|
c[n] = v
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if m.resetCounters && len(c) > 0 {
|
|
|
|
m.counters = make(map[string]uint64)
|
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
|
|
|
|
for n, f := range m.counterFuncs {
|
|
|
|
c[n] = f()
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (m *CirconusMetrics) snapGauges() map[string]interface{} {
|
2017-01-27 01:16:19 +00:00
|
|
|
m.gm.Lock()
|
|
|
|
defer m.gm.Unlock()
|
|
|
|
m.gfm.Lock()
|
|
|
|
defer m.gfm.Unlock()
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
g := make(map[string]interface{}, len(m.gauges)+len(m.gaugeFuncs))
|
2017-01-04 21:47:38 +00:00
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
for n, v := range m.gauges {
|
|
|
|
g[n] = v
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if m.resetGauges && len(g) > 0 {
|
2017-10-27 19:06:04 +00:00
|
|
|
m.gauges = make(map[string]interface{})
|
2017-01-04 21:47:38 +00:00
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
|
|
|
|
for n, f := range m.gaugeFuncs {
|
2017-10-27 19:06:04 +00:00
|
|
|
g[n] = f()
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CirconusMetrics) snapHistograms() map[string]*circonusllhist.Histogram {
|
2017-01-27 01:16:19 +00:00
|
|
|
m.hm.Lock()
|
|
|
|
defer m.hm.Unlock()
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
h := make(map[string]*circonusllhist.Histogram, len(m.histograms))
|
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
for n, hist := range m.histograms {
|
2016-11-02 19:34:30 +00:00
|
|
|
hist.rw.Lock()
|
2016-07-22 19:49:23 +00:00
|
|
|
h[n] = hist.hist.CopyAndReset()
|
2016-11-02 19:34:30 +00:00
|
|
|
hist.rw.Unlock()
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if m.resetHistograms && len(h) > 0 {
|
|
|
|
m.histograms = make(map[string]*Histogram)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
func (m *CirconusMetrics) snapText() map[string]string {
|
2017-01-27 01:16:19 +00:00
|
|
|
m.tm.Lock()
|
|
|
|
defer m.tm.Unlock()
|
|
|
|
m.tfm.Lock()
|
|
|
|
defer m.tfm.Unlock()
|
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
t := make(map[string]string, len(m.text)+len(m.textFuncs))
|
|
|
|
|
2016-07-22 19:49:23 +00:00
|
|
|
for n, v := range m.text {
|
|
|
|
t[n] = v
|
|
|
|
}
|
2017-01-04 21:47:38 +00:00
|
|
|
if m.resetText && len(t) > 0 {
|
|
|
|
m.text = make(map[string]string)
|
|
|
|
}
|
2016-07-22 19:49:23 +00:00
|
|
|
|
|
|
|
for n, f := range m.textFuncs {
|
|
|
|
t[n] = f()
|
|
|
|
}
|
2016-11-02 19:34:30 +00:00
|
|
|
|
2017-01-04 21:47:38 +00:00
|
|
|
return t
|
2016-07-22 19:49:23 +00:00
|
|
|
}
|