Skip to content

Commit f28da4f

Browse files
JerzyLanonsense
authored andcommitted
swarm/metrics: Send the accounting registry to InfluxDB (ethereum#18470)
1 parent 2abeb35 commit f28da4f

File tree

4 files changed

+50
-43
lines changed

4 files changed

+50
-43
lines changed

metrics/registry.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ func (r *PrefixedRegistry) UnregisterAll() {
312312
}
313313

314314
var (
315-
DefaultRegistry = NewRegistry()
316-
EphemeralRegistry = NewRegistry()
315+
DefaultRegistry = NewRegistry()
316+
EphemeralRegistry = NewRegistry()
317+
AccountingRegistry = NewRegistry() // registry used in swarm
317318
)
318319

319320
// Call the given function for each registered metric.

p2p/protocols/accounting.go

+11-24
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,21 @@ var (
2727
// All metrics are cumulative
2828

2929
// total amount of units credited
30-
mBalanceCredit metrics.Counter
30+
mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", metrics.AccountingRegistry)
3131
// total amount of units debited
32-
mBalanceDebit metrics.Counter
32+
mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", metrics.AccountingRegistry)
3333
// total amount of bytes credited
34-
mBytesCredit metrics.Counter
34+
mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", metrics.AccountingRegistry)
3535
// total amount of bytes debited
36-
mBytesDebit metrics.Counter
36+
mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", metrics.AccountingRegistry)
3737
// total amount of credited messages
38-
mMsgCredit metrics.Counter
38+
mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", metrics.AccountingRegistry)
3939
// total amount of debited messages
40-
mMsgDebit metrics.Counter
40+
mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", metrics.AccountingRegistry)
4141
// how many times local node had to drop remote peers
42-
mPeerDrops metrics.Counter
42+
mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", metrics.AccountingRegistry)
4343
// how many times local node overdrafted and dropped
44-
mSelfDrops metrics.Counter
45-
46-
MetricsRegistry metrics.Registry
44+
mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", metrics.AccountingRegistry)
4745
)
4846

4947
// Prices defines how prices are being passed on to the accounting instance
@@ -110,24 +108,13 @@ func NewAccounting(balance Balance, po Prices) *Accounting {
110108
return ah
111109
}
112110

113-
// SetupAccountingMetrics creates a separate registry for p2p accounting metrics;
111+
// SetupAccountingMetrics uses a separate registry for p2p accounting metrics;
114112
// this registry should be independent of any other metrics as it persists at different endpoints.
115-
// It also instantiates the given metrics and starts the persisting go-routine which
113+
// It also starts the persisting go-routine which
116114
// at the passed interval writes the metrics to a LevelDB
117115
func SetupAccountingMetrics(reportInterval time.Duration, path string) *AccountingMetrics {
118-
// create an empty registry
119-
MetricsRegistry = metrics.NewRegistry()
120-
// instantiate the metrics
121-
mBalanceCredit = metrics.NewRegisteredCounterForced("account.balance.credit", MetricsRegistry)
122-
mBalanceDebit = metrics.NewRegisteredCounterForced("account.balance.debit", MetricsRegistry)
123-
mBytesCredit = metrics.NewRegisteredCounterForced("account.bytes.credit", MetricsRegistry)
124-
mBytesDebit = metrics.NewRegisteredCounterForced("account.bytes.debit", MetricsRegistry)
125-
mMsgCredit = metrics.NewRegisteredCounterForced("account.msg.credit", MetricsRegistry)
126-
mMsgDebit = metrics.NewRegisteredCounterForced("account.msg.debit", MetricsRegistry)
127-
mPeerDrops = metrics.NewRegisteredCounterForced("account.peerdrops", MetricsRegistry)
128-
mSelfDrops = metrics.NewRegisteredCounterForced("account.selfdrops", MetricsRegistry)
129116
// create the DB and start persisting
130-
return NewAccountingMetrics(MetricsRegistry, reportInterval, path)
117+
return NewAccountingMetrics(metrics.AccountingRegistry, reportInterval, path)
131118
}
132119

133120
// Send takes a peer, a size and a msg and

p2p/protocols/reporter_test.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,41 @@ func TestReporter(t *testing.T) {
4343
metrics := SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
4444
log.Debug("Done.")
4545

46-
//do some metrics
46+
//change metrics
4747
mBalanceCredit.Inc(12)
4848
mBytesCredit.Inc(34)
4949
mMsgDebit.Inc(9)
5050

51+
//store expected metrics
52+
expectedBalanceCredit := mBalanceCredit.Count()
53+
expectedBytesCredit := mBytesCredit.Count()
54+
expectedMsgDebit := mMsgDebit.Count()
55+
5156
//give the reporter time to write the metrics to DB
5257
time.Sleep(20 * time.Millisecond)
5358

54-
//set the metrics to nil - this effectively simulates the node having shut down...
55-
mBalanceCredit = nil
56-
mBytesCredit = nil
57-
mMsgDebit = nil
5859
//close the DB also, or we can't create a new one
5960
metrics.Close()
6061

62+
//clear the metrics - this effectively simulates the node having shut down...
63+
mBalanceCredit.Clear()
64+
mBytesCredit.Clear()
65+
mMsgDebit.Clear()
66+
6167
//setup the metrics again
6268
log.Debug("Setting up metrics second time")
6369
metrics = SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
6470
defer metrics.Close()
6571
log.Debug("Done.")
6672

6773
//now check the metrics, they should have the same value as before "shutdown"
68-
if mBalanceCredit.Count() != 12 {
69-
t.Fatalf("Expected counter to be %d, but is %d", 12, mBalanceCredit.Count())
74+
if mBalanceCredit.Count() != expectedBalanceCredit {
75+
t.Fatalf("Expected counter to be %d, but is %d", expectedBalanceCredit, mBalanceCredit.Count())
7076
}
71-
if mBytesCredit.Count() != 34 {
72-
t.Fatalf("Expected counter to be %d, but is %d", 23, mBytesCredit.Count())
77+
if mBytesCredit.Count() != expectedBytesCredit {
78+
t.Fatalf("Expected counter to be %d, but is %d", expectedBytesCredit, mBytesCredit.Count())
7379
}
74-
if mMsgDebit.Count() != 9 {
75-
t.Fatalf("Expected counter to be %d, but is %d", 9, mMsgDebit.Count())
80+
if mMsgDebit.Count() != expectedMsgDebit {
81+
t.Fatalf("Expected counter to be %d, but is %d", expectedMsgDebit, mMsgDebit.Count())
7682
}
7783
}

swarm/metrics/flags.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var (
3131
Name: "metrics.influxdb.export",
3232
Usage: "Enable metrics export/push to an external InfluxDB database",
3333
}
34+
MetricsEnableInfluxDBAccountingExportFlag = cli.BoolFlag{
35+
Name: "metrics.influxdb.accounting",
36+
Usage: "Enable accounting metrics export/push to an external InfluxDB database",
37+
}
3438
MetricsInfluxDBEndpointFlag = cli.StringFlag{
3539
Name: "metrics.influxdb.endpoint",
3640
Usage: "Metrics InfluxDB endpoint",
@@ -66,6 +70,7 @@ var (
6670
var Flags = []cli.Flag{
6771
utils.MetricsEnabledFlag,
6872
MetricsEnableInfluxDBExportFlag,
73+
MetricsEnableInfluxDBAccountingExportFlag,
6974
MetricsInfluxDBEndpointFlag,
7075
MetricsInfluxDBDatabaseFlag,
7176
MetricsInfluxDBUsernameFlag,
@@ -77,12 +82,13 @@ func Setup(ctx *cli.Context) {
7782
if gethmetrics.Enabled {
7883
log.Info("Enabling swarm metrics collection")
7984
var (
80-
enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
81-
endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
82-
database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
83-
username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
84-
password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
85-
hosttag = ctx.GlobalString(MetricsInfluxDBHostTagFlag.Name)
85+
enableExport = ctx.GlobalBool(MetricsEnableInfluxDBExportFlag.Name)
86+
enableAccountingExport = ctx.GlobalBool(MetricsEnableInfluxDBAccountingExportFlag.Name)
87+
endpoint = ctx.GlobalString(MetricsInfluxDBEndpointFlag.Name)
88+
database = ctx.GlobalString(MetricsInfluxDBDatabaseFlag.Name)
89+
username = ctx.GlobalString(MetricsInfluxDBUsernameFlag.Name)
90+
password = ctx.GlobalString(MetricsInfluxDBPasswordFlag.Name)
91+
hosttag = ctx.GlobalString(MetricsInfluxDBHostTagFlag.Name)
8692
)
8793

8894
// Start system runtime metrics collection
@@ -94,5 +100,12 @@ func Setup(ctx *cli.Context) {
94100
"host": hosttag,
95101
})
96102
}
103+
104+
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+
})
109+
}
97110
}
98111
}

0 commit comments

Comments
 (0)