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 ( )
2019-06-19 12:50:48 +00:00
m . gauges [ metric ] = val
}
// AddGauge adds value to existing gauge
func ( m * CirconusMetrics ) AddGauge ( metric string , val interface { } ) {
m . gm . Lock ( )
defer m . gm . Unlock ( )
v , ok := m . gauges [ metric ]
if ! ok {
m . gauges [ metric ] = val
return
}
switch val . ( type ) {
default :
// ignore it, unsupported type
case int :
m . gauges [ metric ] = v . ( int ) + val . ( int )
case int8 :
m . gauges [ metric ] = v . ( int8 ) + val . ( int8 )
case int16 :
m . gauges [ metric ] = v . ( int16 ) + val . ( int16 )
case int32 :
m . gauges [ metric ] = v . ( int32 ) + val . ( int32 )
case int64 :
m . gauges [ metric ] = v . ( int64 ) + val . ( int64 )
case uint :
m . gauges [ metric ] = v . ( uint ) + val . ( uint )
case uint8 :
m . gauges [ metric ] = v . ( uint8 ) + val . ( uint8 )
case uint16 :
m . gauges [ metric ] = v . ( uint16 ) + val . ( uint16 )
case uint32 :
m . gauges [ metric ] = v . ( uint32 ) + val . ( uint32 )
case uint64 :
m . gauges [ metric ] = v . ( uint64 ) + val . ( uint64 )
case float32 :
m . gauges [ metric ] = v . ( float32 ) + val . ( float32 )
case float64 :
m . gauges [ metric ] = v . ( float64 ) + val . ( float64 )
}
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 )
}
2019-06-19 12:50:48 +00:00
// GetGaugeTest returns the current value for a gauge. (note: it is a function specifically for "testing", disable automatic submission during testing.)
func ( m * CirconusMetrics ) GetGaugeTest ( metric string ) ( interface { } , error ) {
m . gm . Lock ( )
defer m . gm . Unlock ( )
if val , ok := m . gauges [ metric ] ; ok {
return val , nil
}
return nil , fmt . Errorf ( "Gauge metric '%s' not found" , metric )
}
2016-07-19 23:40:41 +00:00
// 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
2019-06-19 12:50:48 +00:00
// getGaugeType returns accurate resmon type for underlying type of gauge value
func ( m * CirconusMetrics ) getGaugeType ( v interface { } ) string {
mt := "n"
switch v . ( type ) {
2016-08-09 23:34:48 +00:00
case int :
2019-06-19 12:50:48 +00:00
mt = "i"
2016-08-09 23:34:48 +00:00
case int8 :
2019-06-19 12:50:48 +00:00
mt = "i"
2016-08-09 23:34:48 +00:00
case int16 :
2019-06-19 12:50:48 +00:00
mt = "i"
2016-08-09 23:34:48 +00:00
case int32 :
2019-06-19 12:50:48 +00:00
mt = "i"
2016-08-09 23:34:48 +00:00
case uint :
2019-06-19 12:50:48 +00:00
mt = "I"
2016-08-09 23:34:48 +00:00
case uint8 :
2019-06-19 12:50:48 +00:00
mt = "I"
2016-08-09 23:34:48 +00:00
case uint16 :
2019-06-19 12:50:48 +00:00
mt = "I"
2016-08-09 23:34:48 +00:00
case uint32 :
2019-06-19 12:50:48 +00:00
mt = "I"
case int64 :
mt = "l"
2016-08-09 23:34:48 +00:00
case uint64 :
2019-06-19 12:50:48 +00:00
mt = "L"
2016-08-09 23:34:48 +00:00
}
2019-06-19 12:50:48 +00:00
return mt
2016-08-09 23:34:48 +00:00
}