Skip to content

Commit

Permalink
Enhance metrics with influxdb tags and rpc api (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
boqiu authored Oct 17, 2024
1 parent e8cea80 commit 07e6763
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 4 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions metrics/api.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 7 additions & 4 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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,
)
}

Expand Down

0 comments on commit 07e6763

Please sign in to comment.