diff --git a/config/config.yaml b/config/config.yaml index f36c4e3..2eb6520 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -91,13 +91,16 @@ # Metrics configurations # metrics: # enabled: false -# namespace: # reportInterval: 10s # influxdb: # host: http://127.0.0.1:8086 # db: metrics_db # username: # password: +# namespace: +# tags: +# name1: value1 +# name2: value2 # Store Configurations # store: diff --git a/metrics/api.go b/metrics/api.go new file mode 100644 index 0000000..fc42c41 --- /dev/null +++ b/metrics/api.go @@ -0,0 +1,33 @@ +package metrics + +import ( + "strings" + + "github.com/ethereum/go-ethereum/metrics" +) + +const DefaultAPIName = "metrics" + +// API provides metrics related RPC implementations. +type API struct { + reg metrics.Registry +} + +func NewAPI(reg metrics.Registry) *API { return &API{reg} } + +func NewDefaultAPI() *API { return &API{DefaultRegistry} } + +// GetMetrics returns all metrics of specified prefix. Empty prefix indicates all metrics. +func (api *API) GetMetrics(prefix ...string) (map[string]map[string]any, error) { + result := make(map[string]map[string]any) + + all := api.reg.GetAll() + + for k, v := range all { + if len(prefix) == 0 || strings.HasPrefix(k, prefix[0]) { + result[k] = v + } + } + + return result, nil +} diff --git a/metrics/metrics.go b/metrics/metrics.go index b9ba631..81a458c 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -25,8 +25,6 @@ var DefaultRegistry = metrics.NewRegistry() 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 @@ -43,6 +41,10 @@ type InfluxDbConfig struct { Username string // authenticated password Password string + // namespace for metrics reporting + Namespace string + // tags for metrics reporting + Tags map[string]string } // MustInitFromViper inits metrics from viper settings. @@ -72,14 +74,15 @@ func Init(config MetricsConfig) { if config.InfluxDb != nil { // starts a InfluxDB reporter - go influxdb.InfluxDB( + go influxdb.InfluxDBWithTags( DefaultRegistry, config.ReportInterval, config.InfluxDb.Host, config.InfluxDb.Db, config.InfluxDb.Username, config.InfluxDb.Password, - config.Namespace, + config.InfluxDb.Namespace, + config.InfluxDb.Tags, ) }