/
metrics.go
129 lines (110 loc) · 3.82 KB
/
metrics.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package metrics
import (
"fmt"
"time"
"github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics/influxdb"
"github.com/sirupsen/logrus"
)
const (
// default exponentially-decaying metrics sample reservoir size
expDecaySampleReservoirSize int = 1028
// default exponentially-decaying metrics sample alpha
expDecaySampleAlpha float64 = 0.015
)
var (
// default metrics registry
DefaultRegistry = metrics.NewRegistry()
)
// MetricsConfig metrics configurations such as influxdb settings.,
type MetricsConfig struct {
// switch to turn on or off metrics
Enabled bool
// namespace for metrics reporting
Namespace string
// interval to report metrics to influxdb
ReportInterval time.Duration `default:"10s"`
// settings for influxdb to be reported to
InfluxDb *InfluxDbConfig
}
// InfluxDbConfig influxdb configurations.
type InfluxDbConfig struct {
// host path
Host string `default:"http://127.0.0.1:8086"`
// database name
Db string `default:"metrics_db"`
// authenticated username
Username string
// authenticated password
Password string
}
// MustInitFromViper inits metrics from viper settings.
// This should be called before any metric (e.g. timer, histogram) created.
// Because `metrics.Enabled` in go-ethereum is `false` by default, which leads to noop
// metric created for static variables in any package.
//
// Note that viper must be initialized before this, otherwise metrics
// settings may not be loaded correctly. Besides, this init will panic
// and exit if any error happens.
func MustInitFromViper() {
var config MetricsConfig
viper.MustUnmarshalKey("metrics", &config)
Init(config)
}
// Init inits metrics with provided metrics configurations.
func Init(config MetricsConfig) {
if !config.Enabled { // metrics not enabled?
return
}
// go-ethereum `metrics.Enabled` must be set, otherwise it will lead to
// noop metric created for static variables in any package.
metrics.Enabled = true
if config.InfluxDb != nil {
// starts a InfluxDB reporter
go influxdb.InfluxDB(
DefaultRegistry,
config.ReportInterval,
config.InfluxDb.Host,
config.InfluxDb.Db,
config.InfluxDb.Username,
config.InfluxDb.Password,
config.Namespace,
)
}
logrus.WithField("config", fmt.Sprintf("%+v", config)).Debug("Metrics initialized")
}
// GetOrRegisterCounter gets an existed or registers a new counter from
// default registry by a specified metrics name.
func GetOrRegisterCounter(name string) metrics.Counter {
return metrics.GetOrRegisterCounter(name, DefaultRegistry)
}
// GetOrRegisterGauge gets an existed or registers a new gauge from
// default registry by a specified metrics name.
func GetOrRegisterGauge(name string) metrics.Gauge {
return metrics.GetOrRegisterGauge(name, DefaultRegistry)
}
// GetOrRegisterGaugeFloat64 gets an existed or registers a new gauge
// from default registry by a specified metrics name.
func GetOrRegisterGaugeFloat64(name string) metrics.GaugeFloat64 {
return metrics.GetOrRegisterGaugeFloat64(name, DefaultRegistry)
}
// GetOrRegisterMeter gets an existed or registers a new meter from
// default registry by a specified metrics name.
func GetOrRegisterMeter(name string) metrics.Meter {
return metrics.GetOrRegisterMeter(name, DefaultRegistry)
}
// GetOrRegisterHistogram gets an existed or registers a new histogram from
// default registry by a specified metrics name.
func GetOrRegisterHistogram(name string) metrics.Histogram {
return metrics.GetOrRegisterHistogram(
name, DefaultRegistry, metrics.NewExpDecaySample(
expDecaySampleReservoirSize, expDecaySampleAlpha,
),
)
}
// GetOrRegisterTimer gets an existed or registers a new timer from
// default registry by a specified metrics name.
func GetOrRegisterTimer(name string) metrics.Timer {
return metrics.GetOrRegisterTimer(name, DefaultRegistry)
}