diff --git a/cmd/XDC/main.go b/cmd/XDC/main.go index f5bdd24709d29..37b5dde48ab87 100644 --- a/cmd/XDC/main.go +++ b/cmd/XDC/main.go @@ -170,7 +170,7 @@ var ( utils.MetricsInfluxDBDatabaseFlag, utils.MetricsInfluxDBUsernameFlag, utils.MetricsInfluxDBPasswordFlag, - utils.MetricsInfluxDBHostTagFlag, + utils.MetricsInfluxDBTagsFlag, } ) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 39a06e0e3b09f..04ddbca93622a 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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, } @@ -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) { @@ -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 ( diff --git a/cmd/utils/flags_test.go b/cmd/utils/flags_test.go index adbf25b45bf61..728cbb7070143 100644 --- a/cmd/utils/flags_test.go +++ b/cmd/utils/flags_test.go @@ -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 . + +// Package utils contains internal helper functions for go-ethereum commands. package utils import ( @@ -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 diff --git a/metrics/config.go b/metrics/config.go index 4570628c243b9..1150a007aaf7b 100644 --- a/metrics/config.go +++ b/metrics/config.go @@ -41,5 +41,5 @@ var DefaultConfig = Config{ InfluxDBDatabase: "xdc", InfluxDBUsername: "test", InfluxDBPassword: "test", - InfluxDBTags: "localhost", + InfluxDBTags: "host=localhost", }