Skip to content

Commit

Permalink
make it work with mev-boost existing test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
gsora committed Mar 15, 2024
1 parent 5f0c20f commit c58ec80
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
66 changes: 55 additions & 11 deletions server/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package server

import (
"fmt"
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand All @@ -13,6 +15,8 @@ type Metrics struct {
path string
bindAddr string

registry *prometheus.Registry

registerValidatorTime *prometheus.HistogramVec
failedRegisterValidator *prometheus.GaugeVec

Expand All @@ -23,13 +27,37 @@ type Metrics struct {
failedGetPayload *prometheus.GaugeVec
}

// RunMetrics runs a Prometheus metrics server at the given path, on the given bind address.
// NewMetricsServer returns a new Metrics instance with the given path and bind address.
func NewMetricsServer(path string, bindAddr string) (*Metrics, error) {
m := &Metrics{
path: path,
bindAddr: bindAddr,
}

if err := m.registerMetrics(); err != nil {
return nil, fmt.Errorf("can't register metric, %w", err)
}

return m, nil
}

// Run runs a Prometheus metrics server at the given path, on the given bind address.
// This function blocks forever, unless there is an error.
func (m *Metrics) Run() error {
m.registerMetrics()
if m.bindAddr == "" {
return nil // don't run metrics if no bind address is provided
}

metricsHandler := promhttp.InstrumentMetricHandler(
m.registry, promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{
Registry: m.registry,
}),
)

server := http.NewServeMux()
server.Handle(m.path, metricsHandler)

http.Handle(m.path, promhttp.Handler())
err := http.ListenAndServe(m.bindAddr, nil)
err := http.ListenAndServe(m.bindAddr, server)

if err != nil {
return err
Expand All @@ -38,42 +66,58 @@ func (m *Metrics) Run() error {
return nil
}

func (m *Metrics) registerMetrics() {
func (m *Metrics) registerMetrics() error {
const namespace string = "mev_boost"

m.registerValidatorTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
m.registry = prometheus.NewRegistry()

factory := promauto.With(m.registry)

err := m.registry.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
if err != nil {
return fmt.Errorf("register process collector, %w", err)
}

err = m.registry.Register(collectors.NewGoCollector())
if err != nil {
return fmt.Errorf("register go collector, %w", err)
}

m.registerValidatorTime = factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "register_validator",
Help: "Time elapsed for each registerValidator call, indexed by relay.",
}, []string{"relay"})

m.failedRegisterValidator = promauto.NewGaugeVec(prometheus.GaugeOpts{
m.failedRegisterValidator = factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "failed_register_validator",
Help: "Count of all the failed attepts to register a validator, indexed by relay",
}, []string{"relay"})

m.getHeaderTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
m.getHeaderTime = factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "get_header",
Help: "Time elapsed for each getHeader call, indexed by relay, validator public key and slot.",
}, []string{"relay", "validator_pubkey"})

m.failedGetHeader = promauto.NewGaugeVec(prometheus.GaugeOpts{
m.failedGetHeader = factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "failed_get_header",
Help: "Count of all the failed attepts to getHeader, indexed by relay, at the boundary of the HTTP relay call (not counting bad data, etc).",
}, []string{"relay"})

m.getPayloadTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
m.getPayloadTime = factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: "get_payload",
Help: "Time elapsed for each getPayload call, indexed by relay, hardfork, and slot.",
}, []string{"relay", "hardfork"})

m.failedGetPayload = promauto.NewGaugeVec(prometheus.GaugeOpts{
m.failedGetPayload = factory.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "failed_get_payload",
Help: "Count of all the failed attepts to getPayload, indexed by relay, at the boundary of the HTTP relay call (not counting bad data, etc).",
}, []string{"relay", "hardfork"})

return nil
}
14 changes: 10 additions & 4 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ func NewBoostService(opts BoostServiceOpts) (*BoostService, error) {
return nil, err
}

metricsServer, err := NewMetricsServer(
opts.PromPath,
opts.PromBindAddr,
)

if err != nil {
return nil, err
}

return &BoostService{
listenAddr: opts.ListenAddr,
relays: opts.Relays,
Expand All @@ -130,10 +139,7 @@ func NewBoostService(opts BoostServiceOpts) (*BoostService, error) {
bids: make(map[bidRespKey]bidResp),
slotUID: &slotUID{},

metrics: &Metrics{
path: opts.PromPath,
bindAddr: opts.PromBindAddr,
},
metrics: metricsServer,

builderSigningDomain: builderSigningDomain,
httpClientGetHeader: http.Client{
Expand Down

0 comments on commit c58ec80

Please sign in to comment.