2016-11-09 20:30:07 +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-19 23:40:41 +00:00
|
|
|
package circonusgometrics
|
|
|
|
|
|
|
|
// A Gauge is an instantaneous measurement of a value.
|
|
|
|
//
|
|
|
|
// Use a gauge to track metrics which increase and decrease (e.g., amount of
|
|
|
|
// free memory).
|
|
|
|
|
2016-08-09 23:34:48 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2016-07-19 23:40:41 +00:00
|
|
|
// Gauge sets a gauge to a value
|
2016-08-09 23:34:48 +00:00
|
|
|
func (m *CirconusMetrics) Gauge(metric string, val interface{}) {
|
2016-07-19 23:40:41 +00:00
|
|
|
m.SetGauge(metric, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGauge sets a gauge to a value
|
2016-08-09 23:34:48 +00:00
|
|
|
func (m *CirconusMetrics) SetGauge(metric string, val interface{}) {
|
2016-07-19 23:40:41 +00:00
|
|
|
m.gm.Lock()
|
|
|
|
defer m.gm.Unlock()
|
2016-08-09 23:34:48 +00:00
|
|
|
m.gauges[metric] = m.gaugeValString(val)
|
2016-07-19 23:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveGauge removes a gauge
|
|
|
|
func (m *CirconusMetrics) RemoveGauge(metric string) {
|
|
|
|
m.gm.Lock()
|
|
|
|
defer m.gm.Unlock()
|
|
|
|
delete(m.gauges, metric)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGaugeFunc sets a gauge to a function [called at flush interval]
|
|
|
|
func (m *CirconusMetrics) SetGaugeFunc(metric string, fn func() int64) {
|
|
|
|
m.gfm.Lock()
|
|
|
|
defer m.gfm.Unlock()
|
|
|
|
m.gaugeFuncs[metric] = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveGaugeFunc removes a gauge function
|
|
|
|
func (m *CirconusMetrics) RemoveGaugeFunc(metric string) {
|
|
|
|
m.gfm.Lock()
|
|
|
|
defer m.gfm.Unlock()
|
|
|
|
delete(m.gaugeFuncs, metric)
|
|
|
|
}
|
2016-08-09 23:34:48 +00:00
|
|
|
|
|
|
|
// gaugeValString converts an interface value (of a supported type) to a string
|
|
|
|
func (m *CirconusMetrics) gaugeValString(val interface{}) string {
|
|
|
|
vs := ""
|
|
|
|
switch v := val.(type) {
|
|
|
|
default:
|
|
|
|
// ignore it, unsupported type
|
|
|
|
case int:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case int8:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case int16:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case int32:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case int64:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case uint:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case uint8:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case uint16:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case uint32:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case uint64:
|
|
|
|
vs = fmt.Sprintf("%d", v)
|
|
|
|
case float32:
|
|
|
|
vs = fmt.Sprintf("%f", v)
|
|
|
|
case float64:
|
|
|
|
vs = fmt.Sprintf("%f", v)
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|