Skip to content

Commit 21acf0b

Browse files
nonsensefjl
authored andcommitted
cmd/utils: allow for multiple influxdb tags (ethereum#18520)
This PR is replacing the metrics.influxdb.host.tag cmd-line flag with metrics.influxdb.tags - a comma-separated key/value tags, that are passed to the InfluxDB reporter, so that we can index measurements with multiple tags, and not just one host tag. This will be useful for Swarm, where we want to index measurements not just with the host tag, but also with bzzkey and git commit version (for long-running deployments).
1 parent b04da9d commit 21acf0b

File tree

6 files changed

+121
-42
lines changed

6 files changed

+121
-42
lines changed

cmd/geth/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/log"
3939
"github.com/ethereum/go-ethereum/metrics"
4040
"github.com/ethereum/go-ethereum/node"
41-
"gopkg.in/urfave/cli.v1"
41+
cli "gopkg.in/urfave/cli.v1"
4242
)
4343

4444
const (
@@ -173,7 +173,7 @@ var (
173173
utils.MetricsInfluxDBDatabaseFlag,
174174
utils.MetricsInfluxDBUsernameFlag,
175175
utils.MetricsInfluxDBPasswordFlag,
176-
utils.MetricsInfluxDBHostTagFlag,
176+
utils.MetricsInfluxDBTagsFlag,
177177
}
178178
)
179179

cmd/geth/usage.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"github.com/ethereum/go-ethereum/cmd/utils"
2828
"github.com/ethereum/go-ethereum/internal/debug"
29-
"gopkg.in/urfave/cli.v1"
29+
cli "gopkg.in/urfave/cli.v1"
3030
)
3131

3232
// AppHelpTemplate is the test template for the default, global app help topic.
@@ -229,7 +229,7 @@ var AppHelpFlagGroups = []flagGroup{
229229
utils.MetricsInfluxDBDatabaseFlag,
230230
utils.MetricsInfluxDBUsernameFlag,
231231
utils.MetricsInfluxDBPasswordFlag,
232-
utils.MetricsInfluxDBHostTagFlag,
232+
utils.MetricsInfluxDBTagsFlag,
233233
},
234234
},
235235
{

cmd/swarm/swarm-smoke/main.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func main() {
130130
swarmmetrics.MetricsInfluxDBDatabaseFlag,
131131
swarmmetrics.MetricsInfluxDBUsernameFlag,
132132
swarmmetrics.MetricsInfluxDBPasswordFlag,
133-
swarmmetrics.MetricsInfluxDBHostTagFlag,
133+
swarmmetrics.MetricsInfluxDBTagsFlag,
134134
}...)
135135

136136
app.Flags = append(app.Flags, tracing.Flags...)
@@ -183,13 +183,14 @@ func emitMetrics(ctx *cli.Context) error {
183183
database = ctx.GlobalString(swarmmetrics.MetricsInfluxDBDatabaseFlag.Name)
184184
username = ctx.GlobalString(swarmmetrics.MetricsInfluxDBUsernameFlag.Name)
185185
password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
186-
hosttag = ctx.GlobalString(swarmmetrics.MetricsInfluxDBHostTagFlag.Name)
186+
tags = ctx.GlobalString(swarmmetrics.MetricsInfluxDBTagsFlag.Name)
187187
)
188-
return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
189-
"host": hosttag,
190-
"version": gitCommit,
191-
"filesize": fmt.Sprintf("%v", filesize),
192-
})
188+
189+
tagsMap := utils.SplitTagsFlag(tags)
190+
tagsMap["version"] = gitCommit
191+
tagsMap["filesize"] = fmt.Sprintf("%v", filesize)
192+
193+
return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", tagsMap)
193194
}
194195

195196
return nil

cmd/utils/flags.go

+29-12
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import (
5858
"github.com/ethereum/go-ethereum/p2p/netutil"
5959
"github.com/ethereum/go-ethereum/params"
6060
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
61-
"gopkg.in/urfave/cli.v1"
61+
cli "gopkg.in/urfave/cli.v1"
6262
)
6363

6464
var (
@@ -656,14 +656,14 @@ var (
656656
Usage: "Password to authorize access to the database",
657657
Value: "test",
658658
}
659-
// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
660-
// It is used so that we can group all nodes and average a measurement across all of them, but also so
661-
// that we can select a specific node and inspect its measurements.
659+
// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
660+
// For example `host` tag could be used so that we can group all nodes and average a measurement
661+
// across all of them, but also so that we can select a specific node and inspect its measurements.
662662
// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
663-
MetricsInfluxDBHostTagFlag = cli.StringFlag{
664-
Name: "metrics.influxdb.host.tag",
665-
Usage: "InfluxDB `host` tag attached to all measurements",
666-
Value: "localhost",
663+
MetricsInfluxDBTagsFlag = cli.StringFlag{
664+
Name: "metrics.influxdb.tags",
665+
Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
666+
Value: "host=localhost",
667667
}
668668

669669
EWASMInterpreterFlag = cli.StringFlag{
@@ -1460,16 +1460,33 @@ func SetupMetrics(ctx *cli.Context) {
14601460
database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
14611461
username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
14621462
password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
1463-
hosttag = ctx.GlobalString(MetricsInfluxDBHostTagFlag.Name)
14641463
)
14651464

14661465
if enableExport {
1466+
tagsMap := SplitTagsFlag(ctx.GlobalString(MetricsInfluxDBTagsFlag.Name))
1467+
14671468
log.Info("Enabling metrics export to InfluxDB")
1468-
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", map[string]string{
1469-
"host": hosttag,
1470-
})
1469+
1470+
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
1471+
}
1472+
}
1473+
}
1474+
1475+
func SplitTagsFlag(tagsFlag string) map[string]string {
1476+
tags := strings.Split(tagsFlag, ",")
1477+
tagsMap := map[string]string{}
1478+
1479+
for _, t := range tags {
1480+
if t != "" {
1481+
kv := strings.Split(t, "=")
1482+
1483+
if len(kv) == 2 {
1484+
tagsMap[kv[0]] = kv[1]
1485+
}
14711486
}
14721487
}
1488+
1489+
return tagsMap
14731490
}
14741491

14751492
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.

cmd/utils/flags_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package utils contains internal helper functions for go-ethereum commands.
18+
package utils
19+
20+
import (
21+
"reflect"
22+
"testing"
23+
)
24+
25+
func Test_SplitTagsFlag(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
args string
29+
want map[string]string
30+
}{
31+
{
32+
"2 tags case",
33+
"host=localhost,bzzkey=123",
34+
map[string]string{
35+
"host": "localhost",
36+
"bzzkey": "123",
37+
},
38+
},
39+
{
40+
"1 tag case",
41+
"host=localhost123",
42+
map[string]string{
43+
"host": "localhost123",
44+
},
45+
},
46+
{
47+
"empty case",
48+
"",
49+
map[string]string{},
50+
},
51+
{
52+
"garbage",
53+
"smth=smthelse=123",
54+
map[string]string{},
55+
},
56+
}
57+
for _, tt := range tests {
58+
t.Run(tt.name, func(t *testing.T) {
59+
if got := SplitTagsFlag(tt.args); !reflect.DeepEqual(got, tt.want) {
60+
t.Errorf("splitTagsFlag() = %v, want %v", got, tt.want)
61+
}
62+
})
63+
}
64+
}

swarm/metrics/flags.go

+16-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
gethmetrics "github.com/ethereum/go-ethereum/metrics"
2424
"github.com/ethereum/go-ethereum/metrics/influxdb"
2525
"github.com/ethereum/go-ethereum/swarm/log"
26-
"gopkg.in/urfave/cli.v1"
26+
cli "gopkg.in/urfave/cli.v1"
2727
)
2828

2929
var (
@@ -55,14 +55,14 @@ var (
5555
Usage: "Metrics InfluxDB password",
5656
Value: "",
5757
}
58-
// The `host` tag is part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
59-
// It is used so that we can group all nodes and average a measurement across all of them, but also so
60-
// that we can select a specific node and inspect its measurements.
58+
// Tags are part of every measurement sent to InfluxDB. Queries on tags are faster in InfluxDB.
59+
// For example `host` tag could be used so that we can group all nodes and average a measurement
60+
// across all of them, but also so that we can select a specific node and inspect its measurements.
6161
// https://docs.influxdata.com/influxdb/v1.4/concepts/key_concepts/#tag-key
62-
MetricsInfluxDBHostTagFlag = cli.StringFlag{
63-
Name: "metrics.influxdb.host.tag",
64-
Usage: "Metrics InfluxDB `host` tag attached to all measurements",
65-
Value: "localhost",
62+
MetricsInfluxDBTagsFlag = cli.StringFlag{
63+
Name: "metrics.influxdb.tags",
64+
Usage: "Comma-separated InfluxDB tags (key/values) attached to all measurements",
65+
Value: "host=localhost",
6666
}
6767
)
6868

@@ -75,37 +75,34 @@ var Flags = []cli.Flag{
7575
MetricsInfluxDBDatabaseFlag,
7676
MetricsInfluxDBUsernameFlag,
7777
MetricsInfluxDBPasswordFlag,
78-
MetricsInfluxDBHostTagFlag,
78+
MetricsInfluxDBTagsFlag,
7979
}
8080

8181
func Setup(ctx *cli.Context) {
8282
if gethmetrics.Enabled {
8383
log.Info("Enabling swarm metrics collection")
8484
var (
85-
enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
86-
enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
8785
endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
8886
database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
8987
username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
9088
password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
91-
hosttag = ctx.GlobalString(MetricsInfluxDBHostTagFlag.Name)
89+
enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
90+
enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
9291
)
9392

9493
// Start system runtime metrics collection
9594
go gethmetrics.CollectProcessMetrics(2 * time.Second)
9695

96+
tagsMap := utils.SplitTagsFlag(ctx.GlobalString(MetricsInfluxDBTagsFlag.Name))
97+
9798
if enableExport {
9899
log.Info("Enabling swarm metrics export to InfluxDB")
99-
go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", map[string]string{
100-
"host": hosttag,
101-
})
100+
go influxdb.InfluxDBWithTags(gethmetrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "swarm.", tagsMap)
102101
}
103102

104103
if enableAccountingExport {
105-
log.Info("Exporting accounting metrics to InfluxDB")
106-
go influxdb.InfluxDBWithTags(gethmetrics.AccountingRegistry, 10*time.Second, endpoint, database, username, password, "accounting.", map[string]string{
107-
"host": hosttag,
108-
})
104+
log.Info("Exporting swarm accounting metrics to InfluxDB")
105+
go influxdb.InfluxDBWithTags(gethmetrics.AccountingRegistry, 10*time.Second, endpoint, database, username, password, "accounting.", tagsMap)
109106
}
110107
}
111108
}

0 commit comments

Comments
 (0)