9dc7358869
* allow exposing meta information for registered metrics New public method `ExposeMetadata` allows enabling exposition of dummy meta-info for all exposed metrics across all Sets. This feature is needed to improve compatibility with 3rd-party scrapers that require meta information to be present. This commit doesn't update exposition of default system/process metrics to keep the list of changes small. This change should be added in a follow-up commit. https://github.com/VictoriaMetrics/metrics/issues/48 * cleanup * wip * wip * wip * wip --------- Co-authored-by: Aliaksandr Valialkin <valyala@victoriametrics.com>
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package metrics
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
// NewFloatCounter registers and returns new counter of float64 type with the given name.
|
|
//
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
// For instance,
|
|
//
|
|
// - foo
|
|
// - foo{bar="baz"}
|
|
// - foo{bar="baz",aaa="b"}
|
|
//
|
|
// The returned counter is safe to use from concurrent goroutines.
|
|
func NewFloatCounter(name string) *FloatCounter {
|
|
return defaultSet.NewFloatCounter(name)
|
|
}
|
|
|
|
// FloatCounter is a float64 counter guarded by RWmutex.
|
|
//
|
|
// It may be used as a gauge if Add and Sub are called.
|
|
type FloatCounter struct {
|
|
mu sync.Mutex
|
|
n float64
|
|
}
|
|
|
|
// Add adds n to fc.
|
|
func (fc *FloatCounter) Add(n float64) {
|
|
fc.mu.Lock()
|
|
fc.n += n
|
|
fc.mu.Unlock()
|
|
}
|
|
|
|
// Sub substracts n from fc.
|
|
func (fc *FloatCounter) Sub(n float64) {
|
|
fc.mu.Lock()
|
|
fc.n -= n
|
|
fc.mu.Unlock()
|
|
}
|
|
|
|
// Get returns the current value for fc.
|
|
func (fc *FloatCounter) Get() float64 {
|
|
fc.mu.Lock()
|
|
n := fc.n
|
|
fc.mu.Unlock()
|
|
return n
|
|
}
|
|
|
|
// Set sets fc value to n.
|
|
func (fc *FloatCounter) Set(n float64) {
|
|
fc.mu.Lock()
|
|
fc.n = n
|
|
fc.mu.Unlock()
|
|
}
|
|
|
|
// marshalTo marshals fc with the given prefix to w.
|
|
func (fc *FloatCounter) marshalTo(prefix string, w io.Writer) {
|
|
v := fc.Get()
|
|
fmt.Fprintf(w, "%s %g\n", prefix, v)
|
|
}
|
|
|
|
func (fc *FloatCounter) metricType() string {
|
|
return "counter"
|
|
}
|
|
|
|
// GetOrCreateFloatCounter returns registered FloatCounter with the given name
|
|
// or creates new FloatCounter if the registry doesn't contain FloatCounter with
|
|
// the given name.
|
|
//
|
|
// name must be valid Prometheus-compatible metric with possible labels.
|
|
// For instance,
|
|
//
|
|
// - foo
|
|
// - foo{bar="baz"}
|
|
// - foo{bar="baz",aaa="b"}
|
|
//
|
|
// The returned FloatCounter is safe to use from concurrent goroutines.
|
|
//
|
|
// Performance tip: prefer NewFloatCounter instead of GetOrCreateFloatCounter.
|
|
func GetOrCreateFloatCounter(name string) *FloatCounter {
|
|
return defaultSet.GetOrCreateFloatCounter(name)
|
|
}
|