Skip to content

Commit 4519052

Browse files
committed
statediff: refactor metrics
Remove redundant statediff/indexer/prom tooling and use existing prometheus integration.
1 parent 68aa1de commit 4519052

File tree

6 files changed

+138
-311
lines changed

6 files changed

+138
-311
lines changed

statediff/indexer/indexer.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/crypto"
2828
"github.com/ethereum/go-ethereum/log"
29+
"github.com/ethereum/go-ethereum/metrics"
2930
"github.com/ethereum/go-ethereum/params"
3031
"github.com/ethereum/go-ethereum/rlp"
3132

@@ -36,11 +37,12 @@ import (
3637
"github.com/ethereum/go-ethereum/statediff/indexer/ipfs/ipld"
3738
"github.com/ethereum/go-ethereum/statediff/indexer/models"
3839
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
39-
"github.com/ethereum/go-ethereum/statediff/indexer/prom"
4040
"github.com/ethereum/go-ethereum/statediff/indexer/shared"
4141
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
4242
)
4343

44+
var indexerMetrics = RegisterIndexerMetrics(metrics.DefaultRegistry)
45+
4446
// Indexer interface to allow substitution of mocks for testing
4547
type Indexer interface {
4648
PushBlock(block *types.Block, receipts types.Receipts, totalDifficulty *big.Int) (*BlockTx, error)
@@ -109,12 +111,12 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
109111
panic(p)
110112
} else {
111113
tDiff := time.Now().Sub(t)
112-
prom.SetTimeMetric("t_state_store_code_processing", tDiff)
114+
indexerMetrics.tStateStoreCodeProcessing.Update(tDiff)
113115
traceMsg += fmt.Sprintf("state, storage, and code storage processing time: %s\r\n", tDiff.String())
114116
t = time.Now()
115117
err = tx.Commit()
116118
tDiff = time.Now().Sub(t)
117-
prom.SetTimeMetric("t_postgres_commit", tDiff)
119+
indexerMetrics.tPostgresCommit.Update(tDiff)
118120
traceMsg += fmt.Sprintf("postgres transaction commit duration: %s\r\n", tDiff.String())
119121
}
120122
traceMsg += fmt.Sprintf(" TOTAL PROCESSING DURATION: %s\r\n", time.Now().Sub(start).String())
@@ -123,7 +125,8 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
123125
},
124126
}
125127
tDiff := time.Now().Sub(t)
126-
prom.SetTimeMetric("t_free_postgres", tDiff)
128+
indexerMetrics.tFreePostgres.Update(tDiff)
129+
127130
traceMsg += fmt.Sprintf("time spent waiting for free postgres tx: %s:\r\n", tDiff.String())
128131
t = time.Now()
129132

@@ -133,15 +136,15 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
133136
return nil, err
134137
}
135138
tDiff = time.Now().Sub(t)
136-
prom.SetTimeMetric("t_header_processing", tDiff)
139+
indexerMetrics.tHeaderProcessing.Update(tDiff)
137140
traceMsg += fmt.Sprintf("header processing time: %s\r\n", tDiff.String())
138141
t = time.Now()
139142
// Publish and index uncles
140143
if err := sdi.processUncles(tx, headerID, height, uncleNodes); err != nil {
141144
return nil, err
142145
}
143146
tDiff = time.Now().Sub(t)
144-
prom.SetTimeMetric("t_uncle_processing", tDiff)
147+
indexerMetrics.tUncleProcessing.Update(tDiff)
145148
traceMsg += fmt.Sprintf("uncle processing time: %s\r\n", tDiff.String())
146149
t = time.Now()
147150
// Publish and index receipts and txs
@@ -158,7 +161,7 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
158161
return nil, err
159162
}
160163
tDiff = time.Now().Sub(t)
161-
prom.SetTimeMetric("t_tx_receipt_processing", tDiff)
164+
indexerMetrics.tTxAndRecProcessing.Update(tDiff)
162165
traceMsg += fmt.Sprintf("tx and receipt processing time: %s\r\n", tDiff.String())
163166
t = time.Now()
164167

statediff/indexer/metrics.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package indexer
2+
3+
import (
4+
"database/sql"
5+
6+
"github.com/ethereum/go-ethereum/metrics"
7+
"strings"
8+
)
9+
10+
const (
11+
indexerNamespace = "ipld_eth_indexer" // FIXME: "statediff"?
12+
)
13+
14+
// Build a fully qualified metric name
15+
func metricName(subsystem, name string) string {
16+
if name == "" {
17+
return ""
18+
}
19+
parts := []string{indexerNamespace, name}
20+
if subsystem != "" {
21+
parts = []string{indexerNamespace, subsystem, name}
22+
}
23+
// Prometheus uses _ but geth metrics uses / and replaces
24+
return strings.Join(parts, "/")
25+
}
26+
27+
type indexerMetricsContext struct {
28+
// The total number of processed blocks
29+
blocks metrics.Counter
30+
// The total number of processed transactions
31+
transactions metrics.Counter
32+
// The total number of processed receipts
33+
receipts metrics.Counter
34+
// Time spent waiting for free postgres tx
35+
tFreePostgres metrics.Timer
36+
// Postgres transaction commit duration
37+
tPostgresCommit metrics.Timer
38+
// Header processing time
39+
tHeaderProcessing metrics.Timer
40+
// Uncle processing time
41+
tUncleProcessing metrics.Timer
42+
// Tx and receipt processing time
43+
tTxAndRecProcessing metrics.Timer
44+
// State, storage, and code combined processing time
45+
tStateStoreCodeProcessing metrics.Timer
46+
}
47+
48+
func RegisterIndexerMetrics(reg metrics.Registry) indexerMetricsContext {
49+
ctx := indexerMetricsContext{
50+
blocks: metrics.NewCounter(),
51+
transactions: metrics.NewCounter(),
52+
receipts: metrics.NewCounter(),
53+
tFreePostgres: metrics.NewTimer(),
54+
tPostgresCommit: metrics.NewTimer(),
55+
tHeaderProcessing: metrics.NewTimer(),
56+
tUncleProcessing: metrics.NewTimer(),
57+
tTxAndRecProcessing: metrics.NewTimer(),
58+
tStateStoreCodeProcessing: metrics.NewTimer(),
59+
}
60+
subsys := "" // todo
61+
reg.Register(metricName(subsys, "blocks"), ctx.blocks)
62+
reg.Register(metricName(subsys, "transactions"), ctx.transactions)
63+
reg.Register(metricName(subsys, "receipts"), ctx.receipts)
64+
reg.Register(metricName(subsys, "t_free_postgres"), ctx.tFreePostgres)
65+
reg.Register(metricName(subsys, "t_postgres_commit"), ctx.tPostgresCommit)
66+
reg.Register(metricName(subsys, "t_header_processing"), ctx.tHeaderProcessing)
67+
reg.Register(metricName(subsys, "t_uncle_processing"), ctx.tUncleProcessing)
68+
reg.Register(metricName(subsys, "t_tx_receipt_processing"), ctx.tTxAndRecProcessing)
69+
reg.Register(metricName(subsys, "t_state_store_code_processing"), ctx.tStateStoreCodeProcessing)
70+
return ctx
71+
}
72+
73+
type dbMetrics struct {
74+
// Maximum number of open connections to the database
75+
maxOpen metrics.Gauge
76+
// The number of established connections both in use and idle
77+
open metrics.Gauge
78+
// The number of connections currently in use
79+
inUse metrics.Gauge
80+
// The number of idle connections
81+
idle metrics.Gauge
82+
// The total number of connections waited for
83+
waitedFor metrics.Counter
84+
// The total time blocked waiting for a new connection
85+
blockedSeconds metrics.Counter
86+
// The total number of connections closed due to SetMaxIdleConns
87+
closedMaxIdle metrics.Counter
88+
// The total number of connections closed due to SetConnMaxLifetime
89+
closedMaxLifetime metrics.Counter
90+
}
91+
92+
func RegisterDBMetrics(reg metrics.Registry) dbMetrics {
93+
ctx := dbMetrics{
94+
maxOpen: metrics.NewGauge(),
95+
open: metrics.NewGauge(),
96+
inUse: metrics.NewGauge(),
97+
idle: metrics.NewGauge(),
98+
waitedFor: metrics.NewCounter(),
99+
blockedSeconds: metrics.NewCounter(),
100+
closedMaxIdle: metrics.NewCounter(),
101+
closedMaxLifetime: metrics.NewCounter(),
102+
}
103+
subsys := "connections"
104+
reg.Register(metricName(subsys, "max_open_desc"), ctx.maxOpen)
105+
reg.Register(metricName(subsys, "open_desc"), ctx.open)
106+
reg.Register(metricName(subsys, "in_use_desc"), ctx.inUse)
107+
reg.Register(metricName(subsys, "idle_desc"), ctx.idle)
108+
reg.Register(metricName(subsys, "waited_for_desc"), ctx.waitedFor)
109+
reg.Register(metricName(subsys, "blocked_seconds_desc"), ctx.blockedSeconds)
110+
reg.Register(metricName(subsys, "closed_max_idle_desc"), ctx.closedMaxIdle)
111+
reg.Register(metricName(subsys, "closed_max_lifetime_desc"), ctx.closedMaxLifetime)
112+
return ctx
113+
}
114+
115+
func (met *dbMetrics) Update(stats sql.DBStats) {
116+
met.maxOpen.Update(int64(stats.MaxOpenConnections))
117+
met.open.Update(int64(stats.OpenConnections))
118+
met.inUse.Update(int64(stats.InUse))
119+
met.idle.Update(int64(stats.Idle))
120+
met.waitedFor.Inc(int64(stats.WaitCount))
121+
met.blockedSeconds.Inc(int64(stats.WaitDuration.Seconds()))
122+
met.closedMaxIdle.Inc(int64(stats.MaxIdleClosed))
123+
met.closedMaxLifetime.Inc(int64(stats.MaxLifetimeClosed))
124+
}

statediff/indexer/prom/db_stats_collector.go

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)