Skip to content

Commit 18c93de

Browse files
committed
chore(metrics/prometheus): add files needed for coreth and subnet-evm
- replace `Gatherer` function with `NewGatherer` to export `gatherer` and return a concrete `*Gatherer` type - fix slice out of range bug in metrics.ResettingTimer case - change behavior to return an error if metric type is not supported - define local narrower registry interface - rework switch for metric families to be simpler
1 parent d08d0f0 commit 18c93de

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package prometheus
2+
3+
type Registry interface {
4+
// Call the given function for each registered metric.
5+
Each(func(string, any))
6+
// Get the metric by the given name or nil if none is registered.
7+
Get(string) any
8+
}
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 interface{}) {
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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/require"
11+
12+
"github.com/ava-labs/libevm/metrics"
13+
)
14+
15+
func TestGatherer(t *testing.T) {
16+
registry := metrics.NewRegistry()
17+
18+
counter := metrics.NewCounter()
19+
counter.Inc(12345)
20+
21+
err := registry.Register("test/counter", counter)
22+
require.NoError(t, err)
23+
24+
gauge := metrics.NewGauge()
25+
gauge.Update(23456)
26+
27+
err = registry.Register("test/gauge", gauge)
28+
require.NoError(t, err)
29+
30+
gaugeFloat64 := metrics.NewGaugeFloat64()
31+
gaugeFloat64.Update(34567.89)
32+
33+
err = registry.Register("test/gauge_float64", gaugeFloat64)
34+
require.NoError(t, err)
35+
36+
sample := metrics.NewUniformSample(1028)
37+
histogram := metrics.NewHistogram(sample)
38+
39+
err = registry.Register("test/histogram", histogram)
40+
require.NoError(t, err)
41+
42+
meter := metrics.NewMeter()
43+
defer meter.Stop()
44+
meter.Mark(9999999)
45+
46+
err = registry.Register("test/meter", meter)
47+
require.NoError(t, err)
48+
49+
timer := metrics.NewTimer()
50+
defer timer.Stop()
51+
timer.Update(20 * time.Millisecond)
52+
timer.Update(21 * time.Millisecond)
53+
timer.Update(22 * time.Millisecond)
54+
timer.Update(120 * time.Millisecond)
55+
timer.Update(23 * time.Millisecond)
56+
timer.Update(24 * time.Millisecond)
57+
58+
err = registry.Register("test/timer", timer)
59+
require.NoError(t, err)
60+
61+
resettingTimer := metrics.NewResettingTimer()
62+
resettingTimer.Update(10 * time.Millisecond)
63+
resettingTimer.Update(11 * time.Millisecond)
64+
resettingTimer.Update(12 * time.Millisecond)
65+
resettingTimer.Update(120 * time.Millisecond)
66+
resettingTimer.Update(13 * time.Millisecond)
67+
resettingTimer.Update(14 * time.Millisecond)
68+
69+
err = registry.Register("test/resetting_timer", resettingTimer)
70+
require.NoError(t, err)
71+
72+
err = registry.Register("test/resetting_timer_snapshot", resettingTimer.Snapshot())
73+
require.NoError(t, err)
74+
75+
emptyResettingTimer := metrics.NewResettingTimer()
76+
77+
err = registry.Register("test/empty_resetting_timer", emptyResettingTimer)
78+
require.NoError(t, err)
79+
80+
err = registry.Register("test/empty_resetting_timer_snapshot", emptyResettingTimer.Snapshot())
81+
require.NoError(t, err)
82+
83+
g := NewGatherer(registry)
84+
85+
_, err = g.Gather()
86+
require.NoError(t, err)
87+
}

0 commit comments

Comments
 (0)