forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
averager.go
61 lines (48 loc) · 1.31 KB
/
averager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metric
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/haowang0402/avalanchego/utils/wrappers"
)
type Averager interface {
Observe(float64)
}
type averager struct {
count prometheus.Counter
sum prometheus.Gauge
}
func NewAverager(namespace, name, desc string, reg prometheus.Registerer) (Averager, error) {
errs := wrappers.Errs{}
a := NewAveragerWithErrs(namespace, name, desc, reg, &errs)
return a, errs.Err
}
func NewAveragerWithErrs(namespace, name, desc string, reg prometheus.Registerer, errs *wrappers.Errs) Averager {
a := averager{
count: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_count", name),
Help: fmt.Sprintf("# of observations of %s", desc),
}),
sum: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: fmt.Sprintf("%s_sum", name),
Help: fmt.Sprintf("Sum of %s", desc),
}),
}
errs.Add(
reg.Register(a.count),
reg.Register(a.sum),
)
return &a
}
func (a *averager) Observe(v float64) {
a.count.Inc()
a.sum.Add(v)
}
type noAverager struct{}
func NewNoAverager() Averager {
return noAverager{}
}
func (noAverager) Observe(float64) {}