Return gauge object from NewGauge
to be consistent with NewCounter
and NewSummary
This commit is contained in:
parent
d9e8d487bc
commit
6b40edc7ad
15
README.md
15
README.md
@ -21,18 +21,21 @@
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/VictoriaMetrics/metrics"
|
import "github.com/VictoriaMetrics/metrics"
|
||||||
// ...
|
|
||||||
|
// Register various time series.
|
||||||
|
// Time series name may contain labels in Prometheus format - see below.
|
||||||
var (
|
var (
|
||||||
|
// Register counter without labels.
|
||||||
requestsTotal = metrics.NewCounter("requests_total")
|
requestsTotal = metrics.NewCounter("requests_total")
|
||||||
|
|
||||||
|
// Register summary with a single label.
|
||||||
requestDuration = metrics.NewSummary(`requests_duration_seconds{handler="/my/super/handler"}`)
|
requestDuration = metrics.NewSummary(`requests_duration_seconds{handler="/my/super/handler"}`)
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
// Register gauge with two labels.
|
||||||
metrics.NewGauge(`queue_size{queue="foobar",topic="baz"}`, func() float64 {
|
queueSize = metrics.NewGauge(`queue_size{queue="foobar",topic="baz"}`, func() float64 {
|
||||||
return float64(foobarQueue.Len())
|
return float64(foobarQueue.Len())
|
||||||
})
|
})
|
||||||
}
|
)
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
func requestHandler() {
|
func requestHandler() {
|
||||||
@ -43,7 +46,7 @@ func requestHandler() {
|
|||||||
}
|
}
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
// Register `/metrics` handler for exposing the registered metrics.
|
// `/metrics` handler for exposing the registered metrics.
|
||||||
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
|
||||||
metrics.WritePrometheus(w, true)
|
metrics.WritePrometheus(w, true)
|
||||||
})
|
})
|
||||||
|
79
metrics.go
79
metrics.go
@ -22,16 +22,7 @@ import (
|
|||||||
"github.com/valyala/histogram"
|
"github.com/valyala/histogram"
|
||||||
)
|
)
|
||||||
|
|
||||||
type gauge struct {
|
// NewGauge registers and returns gauge with the given name, which calls f
|
||||||
f func() float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *gauge) marshalTo(prefix string, w io.Writer) {
|
|
||||||
v := g.f()
|
|
||||||
fmt.Fprintf(w, "%s %g\n", prefix, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewGauge registers gauge with the given name, which calls f
|
|
||||||
// to obtain gauge value.
|
// to obtain gauge value.
|
||||||
//
|
//
|
||||||
// name must be valid Prometheus-compatible metric with possible labels.
|
// name must be valid Prometheus-compatible metric with possible labels.
|
||||||
@ -42,18 +33,27 @@ func (g *gauge) marshalTo(prefix string, w io.Writer) {
|
|||||||
// * foo{bar="baz",aaa="b"}
|
// * foo{bar="baz",aaa="b"}
|
||||||
//
|
//
|
||||||
// f must be safe for concurrent calls.
|
// f must be safe for concurrent calls.
|
||||||
func NewGauge(name string, f func() float64) {
|
func NewGauge(name string, f func() float64) *Gauge {
|
||||||
g := &gauge{
|
g := &Gauge{
|
||||||
f: f,
|
f: f,
|
||||||
}
|
}
|
||||||
registerMetric(name, g)
|
registerMetric(name, g)
|
||||||
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Counter is a counter.
|
// Gauge is a float64 gauge.
|
||||||
//
|
type Gauge struct {
|
||||||
// It may be used as a gauge if Dec and Set are called.
|
f func() float64
|
||||||
type Counter struct {
|
}
|
||||||
n uint64
|
|
||||||
|
// Get returns the current value for g.
|
||||||
|
func (g *Gauge) Get() float64 {
|
||||||
|
return g.f()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Gauge) marshalTo(prefix string, w io.Writer) {
|
||||||
|
v := g.f()
|
||||||
|
fmt.Fprintf(w, "%s %g\n", prefix, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCounter registers and returns new counter with the given name.
|
// NewCounter registers and returns new counter with the given name.
|
||||||
@ -72,25 +72,11 @@ func NewCounter(name string) *Counter {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerMetric(name string, m metric) {
|
// Counter is a counter.
|
||||||
if err := validateMetric(name); err != nil {
|
//
|
||||||
// Do not use logger.Panicf here, since it may be uninitialized yet.
|
// It may be used as a gauge if Dec and Set are called.
|
||||||
panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
|
type Counter struct {
|
||||||
}
|
n uint64
|
||||||
metricsMapLock.Lock()
|
|
||||||
ok := isRegisteredMetric(metricsMap, name)
|
|
||||||
if !ok {
|
|
||||||
nm := namedMetric{
|
|
||||||
name: name,
|
|
||||||
metric: m,
|
|
||||||
}
|
|
||||||
metricsMap = append(metricsMap, nm)
|
|
||||||
}
|
|
||||||
metricsMapLock.Unlock()
|
|
||||||
if ok {
|
|
||||||
// Do not use logger.Panicf here, since it may be uninitialized yet.
|
|
||||||
panic(fmt.Errorf("BUG: metric with name %q is already registered", name))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inc increments c.
|
// Inc increments c.
|
||||||
@ -236,3 +222,24 @@ func WritePrometheus(w io.Writer, exposeProcessMetrics bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var startTime = time.Now()
|
var startTime = time.Now()
|
||||||
|
|
||||||
|
func registerMetric(name string, m metric) {
|
||||||
|
if err := validateMetric(name); err != nil {
|
||||||
|
// Do not use logger.Panicf here, since it may be uninitialized yet.
|
||||||
|
panic(fmt.Errorf("BUG: invalid metric name %q: %s", name, err))
|
||||||
|
}
|
||||||
|
metricsMapLock.Lock()
|
||||||
|
ok := isRegisteredMetric(metricsMap, name)
|
||||||
|
if !ok {
|
||||||
|
nm := namedMetric{
|
||||||
|
name: name,
|
||||||
|
metric: m,
|
||||||
|
}
|
||||||
|
metricsMap = append(metricsMap, nm)
|
||||||
|
}
|
||||||
|
metricsMapLock.Unlock()
|
||||||
|
if ok {
|
||||||
|
// Do not use logger.Panicf here, since it may be uninitialized yet.
|
||||||
|
panic(fmt.Errorf("BUG: metric with name %q is already registered", name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user