Skip to content

Commit

Permalink
cmd/utils: allow for multiple influxdb flags (ethereum#18520)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Dec 22, 2024
1 parent 4b4f917 commit 47a2993
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/XDC/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ var (
utils.MetricsInfluxDBDatabaseFlag,
utils.MetricsInfluxDBUsernameFlag,
utils.MetricsInfluxDBPasswordFlag,
utils.MetricsInfluxDBHostTagFlag,
utils.MetricsInfluxDBTagsFlag,
}
)

Expand Down
37 changes: 27 additions & 10 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,13 @@ var (
Value: metrics.DefaultConfig.InfluxDBPassword,
Category: flags.MetricsCategory,
}
// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
// It is used so that we can group all nodes and average a measurement across all of them, but also so
// that we can select a specific node and inspect its measurements.
// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
// For example `host` tag could be used so that we can group all nodes and average a measurement
// across all of them, but also so that we can select a specific node and inspect its measurements.
// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
MetricsInfluxDBHostTagFlag = &cli.StringFlag{
Name: "metrics-influxdb.host.tag",
Usage: "InfluxDB `host` tag attached to all measurements",
MetricsInfluxDBTagsFlag = &cli.StringFlag{
Name: "metrics-influxdb.tags",
Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
Value: metrics.DefaultConfig.InfluxDBTags,
Category: flags.MetricsCategory,
}
Expand Down Expand Up @@ -1518,14 +1518,14 @@ func SetupMetrics(ctx *cli.Context) {
database = ctx.String(MetricsInfluxDBDatabaseFlag.Name)
username = ctx.String(MetricsInfluxDBUsernameFlag.Name)
password = ctx.String(MetricsInfluxDBPasswordFlag.Name)
hosttag = ctx.String(MetricsInfluxDBHostTagFlag.Name)
)

if enableExport {
log.Info("Enabling metrics export to InfluxDB")
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "xdc.", map[string]string{
"host": hosttag,
})

tagsMap := SplitTagsFlag(ctx.String(MetricsInfluxDBTagsFlag.Name))

go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "xdc.", tagsMap)
}

if ctx.IsSet(MetricsHTTPFlag.Name) {
Expand All @@ -1538,6 +1538,23 @@ func SetupMetrics(ctx *cli.Context) {
}
}

func SplitTagsFlag(tagsFlag string) map[string]string {
tags := strings.Split(tagsFlag, ",")
tagsMap := map[string]string{}

for _, t := range tags {
if t != "" {
kv := strings.Split(t, "=")

if len(kv) == 2 {
tagsMap[kv[0]] = kv[1]
}
}
}

return tagsMap
}

// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
var (
Expand Down
60 changes: 60 additions & 0 deletions cmd/utils/flags_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// Package utils contains internal helper functions for go-ethereum commands.
package utils

import (
Expand All @@ -8,6 +25,49 @@ import (
"testing"
)

func Test_SplitTagsFlag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args string
want map[string]string
}{
{
"2 tags case",
"host=localhost,bzzkey=123",
map[string]string{
"host": "localhost",
"bzzkey": "123",
},
},
{
"1 tag case",
"host=localhost123",
map[string]string{
"host": "localhost123",
},
},
{
"empty case",
"",
map[string]string{},
},
{
"garbage",
"smth=smthelse=123",
map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := SplitTagsFlag(tt.args); !reflect.DeepEqual(got, tt.want) {
t.Errorf("splitTagsFlag() = %v, want %v", got, tt.want)
}
})
}
}

func TestWalkMatch(t *testing.T) {
type args struct {
root string
Expand Down
2 changes: 1 addition & 1 deletion metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ var DefaultConfig = Config{
InfluxDBDatabase: "xdc",
InfluxDBUsername: "test",
InfluxDBPassword: "test",
InfluxDBTags: "localhost",
InfluxDBTags: "host=localhost",
}

0 comments on commit 47a2993

Please sign in to comment.