Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 469767e

Browse files
committed
Migrate subnet-evm specific files back to metrics/prometheus
- Bring over refactoring and fixes done in ava-labs/libevm#103 - Bring over test refactoring done in ava-labs/libevm#103
1 parent dd1a167 commit 469767e

File tree

4 files changed

+296
-2
lines changed

4 files changed

+296
-2
lines changed

metrics/prometheus/interfaces.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// (c) 2025 Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
package prometheus
4+
5+
type Registry interface {
6+
// Call the given function for each registered metric.
7+
Each(func(string, any))
8+
// Get the metric by the given name or nil if none is registered.
9+
Get(string) any
10+
}

metrics/prometheus/prometheus.go

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// (c) 2025 Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package prometheus
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"sort"
10+
"strings"
11+
12+
"github.com/prometheus/client_golang/prometheus"
13+
14+
"github.com/ava-labs/libevm/metrics"
15+
16+
dto "github.com/prometheus/client_model/go"
17+
)
18+
19+
type Gatherer struct {
20+
registry Registry
21+
}
22+
23+
var _ prometheus.Gatherer = (*Gatherer)(nil)
24+
25+
// NewGatherer returns a gatherer using the given registry.
26+
// Note this gatherer implements the [prometheus.Gatherer] interface.
27+
func NewGatherer(registry Registry) *Gatherer {
28+
return &Gatherer{
29+
registry: registry,
30+
}
31+
}
32+
33+
func (g *Gatherer) Gather() (mfs []*dto.MetricFamily, err error) {
34+
// Gather and pre-sort the metrics to avoid random listings
35+
var names []string
36+
g.registry.Each(func(name string, i any) {
37+
names = append(names, name)
38+
})
39+
sort.Strings(names)
40+
41+
mfs = make([]*dto.MetricFamily, 0, len(names))
42+
for _, name := range names {
43+
mf, err := metricFamily(g.registry, name)
44+
if errors.Is(err, errMetricSkip) {
45+
continue
46+
}
47+
mfs = append(mfs, mf)
48+
}
49+
50+
return mfs, nil
51+
}
52+
53+
var (
54+
errMetricSkip = errors.New("metric skipped")
55+
)
56+
57+
func ptrTo[T any](x T) *T { return &x }
58+
59+
func metricFamily(registry Registry, name string) (mf *dto.MetricFamily, err error) {
60+
metric := registry.Get(name)
61+
name = strings.ReplaceAll(name, "/", "_")
62+
63+
switch m := metric.(type) {
64+
case metrics.Counter:
65+
return &dto.MetricFamily{
66+
Name: &name,
67+
Type: dto.MetricType_COUNTER.Enum(),
68+
Metric: []*dto.Metric{{
69+
Counter: &dto.Counter{
70+
Value: ptrTo(float64(m.Snapshot().Count())),
71+
},
72+
}},
73+
}, nil
74+
case metrics.CounterFloat64:
75+
return &dto.MetricFamily{
76+
Name: &name,
77+
Type: dto.MetricType_COUNTER.Enum(),
78+
Metric: []*dto.Metric{{
79+
Counter: &dto.Counter{
80+
Value: ptrTo(m.Snapshot().Count()),
81+
},
82+
}},
83+
}, nil
84+
case metrics.Gauge:
85+
return &dto.MetricFamily{
86+
Name: &name,
87+
Type: dto.MetricType_GAUGE.Enum(),
88+
Metric: []*dto.Metric{{
89+
Gauge: &dto.Gauge{
90+
Value: ptrTo(float64(m.Snapshot().Value())),
91+
},
92+
}},
93+
}, nil
94+
case metrics.GaugeFloat64:
95+
return &dto.MetricFamily{
96+
Name: &name,
97+
Type: dto.MetricType_GAUGE.Enum(),
98+
Metric: []*dto.Metric{{
99+
Gauge: &dto.Gauge{
100+
Value: ptrTo(m.Snapshot().Value()),
101+
},
102+
}},
103+
}, nil
104+
case metrics.Histogram:
105+
snapshot := m.Snapshot()
106+
107+
quantiles := []float64{.5, .75, .95, .99, .999, .9999}
108+
thresholds := snapshot.Percentiles(quantiles)
109+
dtoQuantiles := make([]*dto.Quantile, len(quantiles))
110+
for i := range thresholds {
111+
dtoQuantiles[i] = &dto.Quantile{
112+
Quantile: ptrTo(quantiles[i]),
113+
Value: ptrTo(thresholds[i]),
114+
}
115+
}
116+
117+
return &dto.MetricFamily{
118+
Name: &name,
119+
Type: dto.MetricType_SUMMARY.Enum(),
120+
Metric: []*dto.Metric{{
121+
Summary: &dto.Summary{
122+
SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec
123+
SampleSum: ptrTo(float64(snapshot.Sum())),
124+
Quantile: dtoQuantiles,
125+
},
126+
}},
127+
}, nil
128+
case metrics.Meter:
129+
return &dto.MetricFamily{
130+
Name: &name,
131+
Type: dto.MetricType_GAUGE.Enum(),
132+
Metric: []*dto.Metric{{
133+
Gauge: &dto.Gauge{
134+
Value: ptrTo(float64(m.Snapshot().Count())),
135+
},
136+
}},
137+
}, nil
138+
case metrics.Timer:
139+
snapshot := m.Snapshot()
140+
141+
quantiles := []float64{.5, .75, .95, .99, .999, .9999}
142+
thresholds := snapshot.Percentiles(quantiles)
143+
dtoQuantiles := make([]*dto.Quantile, len(quantiles))
144+
for i := range thresholds {
145+
dtoQuantiles[i] = &dto.Quantile{
146+
Quantile: ptrTo(quantiles[i]),
147+
Value: ptrTo(thresholds[i]),
148+
}
149+
}
150+
151+
return &dto.MetricFamily{
152+
Name: &name,
153+
Type: dto.MetricType_SUMMARY.Enum(),
154+
Metric: []*dto.Metric{{
155+
Summary: &dto.Summary{
156+
SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec
157+
SampleSum: ptrTo(float64(snapshot.Sum())),
158+
Quantile: dtoQuantiles,
159+
},
160+
}},
161+
}, nil
162+
case metrics.ResettingTimer:
163+
snapshot := m.Snapshot()
164+
if snapshot.Count() == 0 {
165+
return nil, fmt.Errorf("%w: resetting timer metric count is zero", errMetricSkip)
166+
}
167+
168+
pvShortPercent := []float64{50, 95, 99}
169+
thresholds := snapshot.Percentiles(pvShortPercent)
170+
dtoQuantiles := make([]*dto.Quantile, len(pvShortPercent))
171+
for i := range pvShortPercent {
172+
dtoQuantiles[i] = &dto.Quantile{
173+
Quantile: ptrTo(pvShortPercent[i]),
174+
Value: ptrTo(thresholds[i]),
175+
}
176+
}
177+
178+
return &dto.MetricFamily{
179+
Name: &name,
180+
Type: dto.MetricType_SUMMARY.Enum(),
181+
Metric: []*dto.Metric{{
182+
Summary: &dto.Summary{
183+
SampleCount: ptrTo(uint64(snapshot.Count())), //nolint:gosec
184+
// TODO: do we need to specify SampleSum here? and if so
185+
// what should that be?
186+
Quantile: dtoQuantiles,
187+
},
188+
}},
189+
}, nil
190+
default:
191+
return nil, fmt.Errorf("metric type is not supported: %T", metric)
192+
}
193+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// (c) 2025 Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package prometheus
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/ava-labs/libevm/metrics"
14+
)
15+
16+
func TestGatherer_Gather(t *testing.T) {
17+
metricsEnabled := metrics.Enabled
18+
if !metricsEnabled {
19+
metrics.Enabled = true
20+
t.Cleanup(func() {
21+
metrics.Enabled = false
22+
})
23+
}
24+
25+
registry := metrics.NewRegistry()
26+
register := func(t *testing.T, name string, collector any) {
27+
t.Helper()
28+
err := registry.Register(name, collector)
29+
require.NoError(t, err)
30+
}
31+
32+
counter := metrics.NewCounter()
33+
counter.Inc(12345)
34+
register(t, "test/counter", counter)
35+
36+
gauge := metrics.NewGauge()
37+
gauge.Update(23456)
38+
register(t, "test/gauge", gauge)
39+
40+
gaugeFloat64 := metrics.NewGaugeFloat64()
41+
gaugeFloat64.Update(34567.89)
42+
register(t, "test/gauge_float64", gaugeFloat64)
43+
44+
sample := metrics.NewUniformSample(1028)
45+
histogram := metrics.NewHistogram(sample)
46+
register(t, "test/histogram", histogram)
47+
48+
meter := metrics.NewMeter()
49+
t.Cleanup(meter.Stop)
50+
meter.Mark(9999999)
51+
register(t, "test/meter", meter)
52+
53+
timer := metrics.NewTimer()
54+
t.Cleanup(timer.Stop)
55+
timer.Update(20 * time.Millisecond)
56+
timer.Update(21 * time.Millisecond)
57+
timer.Update(22 * time.Millisecond)
58+
timer.Update(120 * time.Millisecond)
59+
timer.Update(23 * time.Millisecond)
60+
timer.Update(24 * time.Millisecond)
61+
register(t, "test/timer", timer)
62+
63+
resettingTimer := metrics.NewResettingTimer()
64+
register(t, "test/resetting_timer", resettingTimer)
65+
resettingTimer.Update(time.Second) // must be after register call
66+
67+
emptyResettingTimer := metrics.NewResettingTimer()
68+
register(t, "test/empty_resetting_timer", emptyResettingTimer)
69+
70+
emptyResettingTimer.Update(time.Second) // no effect because of snapshot below
71+
register(t, "test/empty_resetting_timer_snapshot", emptyResettingTimer.Snapshot())
72+
73+
g := NewGatherer(registry)
74+
75+
families, err := g.Gather()
76+
require.NoError(t, err)
77+
familyStrings := make([]string, len(families))
78+
for i := range families {
79+
familyStrings[i] = families[i].String()
80+
}
81+
want := []string{
82+
`name:"test_counter" type:COUNTER metric:<counter:<value:12345 > > `,
83+
`name:"test_gauge" type:GAUGE metric:<gauge:<value:23456 > > `,
84+
`name:"test_gauge_float64" type:GAUGE metric:<gauge:<value:34567.89 > > `,
85+
`name:"test_histogram" type:SUMMARY metric:<summary:<sample_count:0 sample_sum:0 quantile:<quantile:0.5 value:0 > quantile:<quantile:0.75 value:0 > quantile:<quantile:0.95 value:0 > quantile:<quantile:0.99 value:0 > quantile:<quantile:0.999 value:0 > quantile:<quantile:0.9999 value:0 > > > `,
86+
`name:"test_meter" type:GAUGE metric:<gauge:<value:9.999999e+06 > > `,
87+
`name:"test_resetting_timer" type:SUMMARY metric:<summary:<sample_count:1 quantile:<quantile:50 value:1e+09 > quantile:<quantile:95 value:1e+09 > quantile:<quantile:99 value:1e+09 > > > `,
88+
`name:"test_timer" type:SUMMARY metric:<summary:<sample_count:6 sample_sum:2.3e+08 quantile:<quantile:0.5 value:2.25e+07 > quantile:<quantile:0.75 value:4.8e+07 > quantile:<quantile:0.95 value:1.2e+08 > quantile:<quantile:0.99 value:1.2e+08 > quantile:<quantile:0.999 value:1.2e+08 > quantile:<quantile:0.9999 value:1.2e+08 > > > `,
89+
}
90+
assert.Equal(t, want, familyStrings)
91+
}

plugin/evm/vm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ava-labs/coreth/core/types"
3535
"github.com/ava-labs/coreth/eth"
3636
"github.com/ava-labs/coreth/eth/ethconfig"
37+
corethprometheus "github.com/ava-labs/coreth/metrics/prometheus"
3738
"github.com/ava-labs/coreth/miner"
3839
"github.com/ava-labs/coreth/node"
3940
"github.com/ava-labs/coreth/params"
@@ -45,7 +46,6 @@ import (
4546
"github.com/ava-labs/coreth/triedb/hashdb"
4647
"github.com/ava-labs/coreth/utils"
4748
"github.com/ava-labs/libevm/metrics"
48-
libevmPrometheus "github.com/ava-labs/libevm/metrics/prometheus"
4949

5050
warpcontract "github.com/ava-labs/coreth/precompile/contracts/warp"
5151
"github.com/ava-labs/coreth/rpc"
@@ -636,7 +636,7 @@ func (vm *VM) initializeMetrics() error {
636636
return nil
637637
}
638638

639-
gatherer := libevmPrometheus.NewGatherer(metrics.DefaultRegistry)
639+
gatherer := corethprometheus.NewGatherer(metrics.DefaultRegistry)
640640
if err := vm.ctx.Metrics.Register(ethMetricsPrefix, gatherer); err != nil {
641641
return err
642642
}

0 commit comments

Comments
 (0)