Skip to content

Commit 665fd4a

Browse files
committed
cmd/keeper, cmd/utils, metrics: mock enough stuff to build with tamago
1 parent 8fad02a commit 665fd4a

File tree

11 files changed

+247
-156
lines changed

11 files changed

+247
-156
lines changed

cmd/keeper/dummy_fastcache/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/ethereum/go-ethereum/cmd/keeper/dummy_fastcache
2+
3+
go 1.24.0

cmd/keeper/dummy_fastcache/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fastcache
2+
3+
type Cache struct{}
4+
5+
func (*Cache) Get(dst, k []byte) []byte { return nil }
6+
func (*Cache) HasGet(dst, k []byte) ([]byte, bool) { return nil, false }
7+
func (*Cache) Set(k, v []byte) {}
8+
func (*Cache) Reset() {}
9+
func (*Cache) Del([]byte) {}
10+
11+
func New(int) *Cache {
12+
return &Cache{}
13+
}

cmd/keeper/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ require (
4040
)
4141

4242
replace github.com/ethereum/go-ethereum => ../../
43+
44+
replace github.com/VictoriaMetrics/fastcache => ./dummy_fastcache

cmd/utils/flags.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ import (
2424
"errors"
2525
"fmt"
2626
"math/big"
27-
"net"
2827
"net/http"
2928
"os"
3029
"path/filepath"
3130
godebug "runtime/debug"
3231
"strconv"
3332
"strings"
34-
"time"
3533

3634
"github.com/ethereum/go-ethereum/accounts"
3735
"github.com/ethereum/go-ethereum/accounts/keystore"
@@ -60,8 +58,6 @@ import (
6058
"github.com/ethereum/go-ethereum/internal/flags"
6159
"github.com/ethereum/go-ethereum/log"
6260
"github.com/ethereum/go-ethereum/metrics"
63-
"github.com/ethereum/go-ethereum/metrics/exp"
64-
"github.com/ethereum/go-ethereum/metrics/influxdb"
6561
"github.com/ethereum/go-ethereum/miner"
6662
"github.com/ethereum/go-ethereum/node"
6763
"github.com/ethereum/go-ethereum/p2p"
@@ -2134,54 +2130,6 @@ func RegisterSyncOverrideService(stack *node.Node, eth *eth.Ethereum, target com
21342130
syncer.Register(stack, eth, target, exitWhenSynced)
21352131
}
21362132

2137-
// SetupMetrics configures the metrics system.
2138-
func SetupMetrics(cfg *metrics.Config) {
2139-
if !cfg.Enabled {
2140-
return
2141-
}
2142-
log.Info("Enabling metrics collection")
2143-
metrics.Enable()
2144-
2145-
// InfluxDB exporter.
2146-
var (
2147-
enableExport = cfg.EnableInfluxDB
2148-
enableExportV2 = cfg.EnableInfluxDBV2
2149-
)
2150-
if cfg.EnableInfluxDB && cfg.EnableInfluxDBV2 {
2151-
Fatalf("Flags %v can't be used at the same time", strings.Join([]string{MetricsEnableInfluxDBFlag.Name, MetricsEnableInfluxDBV2Flag.Name}, ", "))
2152-
}
2153-
var (
2154-
endpoint = cfg.InfluxDBEndpoint
2155-
database = cfg.InfluxDBDatabase
2156-
username = cfg.InfluxDBUsername
2157-
password = cfg.InfluxDBPassword
2158-
2159-
token = cfg.InfluxDBToken
2160-
bucket = cfg.InfluxDBBucket
2161-
organization = cfg.InfluxDBOrganization
2162-
tagsMap = SplitTagsFlag(cfg.InfluxDBTags)
2163-
)
2164-
if enableExport {
2165-
log.Info("Enabling metrics export to InfluxDB")
2166-
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
2167-
} else if enableExportV2 {
2168-
log.Info("Enabling metrics export to InfluxDB (v2)")
2169-
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
2170-
}
2171-
2172-
// Expvar exporter.
2173-
if cfg.HTTP != "" {
2174-
address := net.JoinHostPort(cfg.HTTP, fmt.Sprintf("%d", cfg.Port))
2175-
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
2176-
exp.Setup(address)
2177-
} else if cfg.HTTP == "" && cfg.Port != 0 {
2178-
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
2179-
}
2180-
2181-
// Enable system metrics collection.
2182-
go metrics.CollectProcessMetrics(3 * time.Second)
2183-
}
2184-
21852133
// SplitTagsFlag parses a comma-separated list of k=v metrics tags.
21862134
func SplitTagsFlag(tagsFlag string) map[string]string {
21872135
tags := strings.Split(tagsFlag, ",")

cmd/utils/metrics.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2026 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+
//go:build !tamago
18+
// +build !tamago
19+
20+
// Package utils contains internal helper functions for go-ethereum commands.
21+
package utils
22+
23+
import (
24+
"fmt"
25+
"net"
26+
"strings"
27+
"time"
28+
29+
"github.com/ethereum/go-ethereum/log"
30+
"github.com/ethereum/go-ethereum/metrics"
31+
"github.com/ethereum/go-ethereum/metrics/exp"
32+
"github.com/ethereum/go-ethereum/metrics/influxdb"
33+
)
34+
35+
// SetupMetrics configures the metrics system.
36+
func SetupMetrics(cfg *metrics.Config) {
37+
if !cfg.Enabled {
38+
return
39+
}
40+
log.Info("Enabling metrics collection")
41+
metrics.Enable()
42+
43+
// InfluxDB exporter.
44+
var (
45+
enableExport = cfg.EnableInfluxDB
46+
enableExportV2 = cfg.EnableInfluxDBV2
47+
)
48+
if cfg.EnableInfluxDB && cfg.EnableInfluxDBV2 {
49+
Fatalf("Flags %v can't be used at the same time", strings.Join([]string{MetricsEnableInfluxDBFlag.Name, MetricsEnableInfluxDBV2Flag.Name}, ", "))
50+
}
51+
var (
52+
endpoint = cfg.InfluxDBEndpoint
53+
database = cfg.InfluxDBDatabase
54+
username = cfg.InfluxDBUsername
55+
password = cfg.InfluxDBPassword
56+
57+
token = cfg.InfluxDBToken
58+
bucket = cfg.InfluxDBBucket
59+
organization = cfg.InfluxDBOrganization
60+
tagsMap = SplitTagsFlag(cfg.InfluxDBTags)
61+
)
62+
if enableExport {
63+
log.Info("Enabling metrics export to InfluxDB")
64+
go influxdb.InfluxDBWithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, database, username, password, "geth.", tagsMap)
65+
} else if enableExportV2 {
66+
log.Info("Enabling metrics export to InfluxDB (v2)")
67+
go influxdb.InfluxDBV2WithTags(metrics.DefaultRegistry, 10*time.Second, endpoint, token, bucket, organization, "geth.", tagsMap)
68+
}
69+
70+
// Expvar exporter.
71+
if cfg.HTTP != "" {
72+
address := net.JoinHostPort(cfg.HTTP, fmt.Sprintf("%d", cfg.Port))
73+
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
74+
exp.Setup(address)
75+
} else if cfg.HTTP == "" && cfg.Port != 0 {
76+
log.Warn(fmt.Sprintf("--%s specified without --%s, metrics server will not start.", MetricsPortFlag.Name, MetricsHTTPFlag.Name))
77+
}
78+
79+
// Enable system metrics collection.
80+
go metrics.CollectProcessMetrics(3 * time.Second)
81+
}

cmd/utils/metrics_tamago.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2026 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+
//go:build tamago
18+
// +build tamago
19+
20+
// Package utils contains internal helper functions for go-ethereum commands.
21+
package utils
22+
23+
// SetupMetrics configures the metrics system.
24+
func SetupMetrics(*metrics.Config) {
25+
}

metrics/cpu_enabled.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
//go:build !ios && !js && !wasip1 && !tinygo
18-
// +build !ios,!js,!wasip1,!tinygo
17+
//go:build !ios && !js && !wasip1 && !tinygo && !tamago
18+
// +build !ios,!js,!wasip1,!tinygo,!tamago
1919

2020
package metrics
2121

metrics/cputime_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
//go:build !windows && !js && !wasip1 && !tinygo
18-
// +build !windows,!js,!wasip1,!tinygo
17+
//go:build !windows && !js && !wasip1 && !tinygo && !tamago
18+
// +build !windows,!js,!wasip1,!tinygo,!tamago
1919

2020
package metrics
2121

metrics/metrics.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package metrics
99
import (
1010
"runtime/metrics"
1111
"runtime/pprof"
12-
"time"
1312
)
1413

1514
var (
@@ -103,102 +102,3 @@ func readRuntimeStats(v *runtimeStats) {
103102
}
104103
}
105104
}
106-
107-
// CollectProcessMetrics periodically collects various metrics about the running process.
108-
func CollectProcessMetrics(refresh time.Duration) {
109-
// Short circuit if the metrics system is disabled
110-
if !metricsEnabled {
111-
return
112-
}
113-
114-
// Create the various data collectors
115-
var (
116-
cpustats = make([]CPUStats, 2)
117-
diskstats = make([]DiskStats, 2)
118-
rstats = make([]runtimeStats, 2)
119-
)
120-
121-
// This scale factor is used for the runtime's time metrics. It's useful to convert to
122-
// ns here because the runtime gives times in float seconds, but runtimeHistogram can
123-
// only provide integers for the minimum and maximum values.
124-
const secondsToNs = float64(time.Second)
125-
126-
// Define the various metrics to collect
127-
var (
128-
cpuSysLoad = GetOrRegisterGauge("system/cpu/sysload", DefaultRegistry)
129-
cpuSysWait = GetOrRegisterGauge("system/cpu/syswait", DefaultRegistry)
130-
cpuProcLoad = GetOrRegisterGauge("system/cpu/procload", DefaultRegistry)
131-
cpuSysLoadTotal = GetOrRegisterCounterFloat64("system/cpu/sysload/total", DefaultRegistry)
132-
cpuSysWaitTotal = GetOrRegisterCounterFloat64("system/cpu/syswait/total", DefaultRegistry)
133-
cpuProcLoadTotal = GetOrRegisterCounterFloat64("system/cpu/procload/total", DefaultRegistry)
134-
cpuThreads = GetOrRegisterGauge("system/cpu/threads", DefaultRegistry)
135-
cpuGoroutines = GetOrRegisterGauge("system/cpu/goroutines", DefaultRegistry)
136-
cpuSchedLatency = getOrRegisterRuntimeHistogram("system/cpu/schedlatency", secondsToNs, nil)
137-
memPauses = getOrRegisterRuntimeHistogram("system/memory/pauses", secondsToNs, nil)
138-
memAllocs = GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
139-
memFrees = GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
140-
memTotal = GetOrRegisterGauge("system/memory/held", DefaultRegistry)
141-
heapUsed = GetOrRegisterGauge("system/memory/used", DefaultRegistry)
142-
heapObjects = GetOrRegisterGauge("system/memory/objects", DefaultRegistry)
143-
diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
144-
diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
145-
diskReadBytesCounter = GetOrRegisterCounter("system/disk/readbytes", DefaultRegistry)
146-
diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
147-
diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
148-
diskWriteBytesCounter = GetOrRegisterCounter("system/disk/writebytes", DefaultRegistry)
149-
)
150-
151-
var lastCollectTime time.Time
152-
153-
// Iterate loading the different stats and updating the meters.
154-
now, prev := 0, 1
155-
for ; ; now, prev = prev, now {
156-
// Gather CPU times.
157-
ReadCPUStats(&cpustats[now])
158-
collectTime := time.Now()
159-
secondsSinceLastCollect := collectTime.Sub(lastCollectTime).Seconds()
160-
lastCollectTime = collectTime
161-
if secondsSinceLastCollect > 0 {
162-
sysLoad := cpustats[now].GlobalTime - cpustats[prev].GlobalTime
163-
sysWait := cpustats[now].GlobalWait - cpustats[prev].GlobalWait
164-
procLoad := cpustats[now].LocalTime - cpustats[prev].LocalTime
165-
// Convert to integer percentage.
166-
cpuSysLoad.Update(int64(sysLoad / secondsSinceLastCollect * 100))
167-
cpuSysWait.Update(int64(sysWait / secondsSinceLastCollect * 100))
168-
cpuProcLoad.Update(int64(procLoad / secondsSinceLastCollect * 100))
169-
// increment counters (ms)
170-
cpuSysLoadTotal.Inc(sysLoad)
171-
cpuSysWaitTotal.Inc(sysWait)
172-
cpuProcLoadTotal.Inc(procLoad)
173-
}
174-
175-
// Threads
176-
cpuThreads.Update(int64(threadCreateProfile.Count()))
177-
178-
// Go runtime metrics
179-
readRuntimeStats(&rstats[now])
180-
181-
cpuGoroutines.Update(int64(rstats[now].Goroutines))
182-
cpuSchedLatency.update(rstats[now].SchedLatency)
183-
memPauses.update(rstats[now].GCPauses)
184-
185-
memAllocs.Mark(int64(rstats[now].GCAllocBytes - rstats[prev].GCAllocBytes))
186-
memFrees.Mark(int64(rstats[now].GCFreedBytes - rstats[prev].GCFreedBytes))
187-
188-
memTotal.Update(int64(rstats[now].MemTotal))
189-
heapUsed.Update(int64(rstats[now].MemTotal - rstats[now].HeapUnused - rstats[now].HeapFree - rstats[now].HeapReleased))
190-
heapObjects.Update(int64(rstats[now].HeapObjects))
191-
192-
// Disk
193-
if ReadDiskStats(&diskstats[now]) == nil {
194-
diskReads.Mark(diskstats[now].ReadCount - diskstats[prev].ReadCount)
195-
diskReadBytes.Mark(diskstats[now].ReadBytes - diskstats[prev].ReadBytes)
196-
diskWrites.Mark(diskstats[now].WriteCount - diskstats[prev].WriteCount)
197-
diskWriteBytes.Mark(diskstats[now].WriteBytes - diskstats[prev].WriteBytes)
198-
diskReadBytesCounter.Inc(diskstats[now].ReadBytes - diskstats[prev].ReadBytes)
199-
diskWriteBytesCounter.Inc(diskstats[now].WriteBytes - diskstats[prev].WriteBytes)
200-
}
201-
202-
time.Sleep(refresh)
203-
}
204-
}

0 commit comments

Comments
 (0)