Skip to content

Commit be38349

Browse files
committed
feat(issue-253): initial metrics implementation
1 parent 1904bfe commit be38349

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Please start by reading our [code of conduct](CODE_OF_CONDUCT.md).
66

77
## Set up
88

9-
Install a few dev dependencies for `make lint`: https://github.com/flashbots/mev-boost/blob/go122/.github/workflows/lint.yml#L29-L37
9+
Install a few dev dependencies for `make lint`: https://github.com/flashbots/mev-boost/blob/develop/.github/workflows/lint.yml#L29-L37
1010

1111
Look at the [README for instructions to install the dependencies and build `mev-boost`](README.md#installing)
1212

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ sequenceDiagram
345345
mev_boost-->>consensus: submitBlindedBlock response
346346
```
347347

348+
# Metrics
349+
350+
Optionally the `-prometheus-port=9000` flag can be passed to expose prometheus metrics on specified port
351+
348352
# Maintainers
349353

350354
- [@metachris](https://github.com/metachris)

cli/flags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var flags = []cli.Flag{
3434
timeoutGetPayloadFlag,
3535
timeoutRegValFlag,
3636
maxRetriesFlag,
37+
prometheusListenAddr,
3738
}
3839

3940
var (
@@ -83,6 +84,12 @@ var (
8384
Usage: "disables adding the version to every log entry",
8485
Category: LoggingCategory,
8586
}
87+
prometheusListenAddr = &cli.IntFlag{
88+
Name: "prometheus-port",
89+
Sources: cli.EnvVars("PROMETHEUS_PORT"),
90+
Usage: "when set to a valid http port, will export runtime metrics to a prometheus server on that port",
91+
Category: LoggingCategory,
92+
}
8693
// Genesis Flags
8794
customGenesisForkFlag = &cli.StringFlag{
8895
Name: "genesis-fork-version",

cli/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"flag"
77
"fmt"
8+
"math"
89
"os"
910
"strings"
1011
"time"
@@ -13,6 +14,8 @@ import (
1314
"github.com/flashbots/mev-boost/common"
1415
"github.com/flashbots/mev-boost/config"
1516
"github.com/flashbots/mev-boost/server"
17+
"github.com/prometheus/client_golang/prometheus"
18+
"github.com/prometheus/client_golang/prometheus/collectors"
1619
"github.com/sirupsen/logrus"
1720
"github.com/urfave/cli/v3"
1821
)
@@ -69,6 +72,13 @@ func start(_ context.Context, cmd *cli.Command) error {
6972
relays, monitors, minBid, relayCheck = setupRelays(cmd)
7073
listenAddr = cmd.String(addrFlag.Name)
7174
)
75+
prometheusRegistry := prometheus.NewRegistry()
76+
if err := prometheusRegistry.Register(collectors.NewGoCollector()); err != nil {
77+
log.WithError(err).Error("Failed to register metrics for GoCollector")
78+
}
79+
if err := prometheusRegistry.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})); err != nil {
80+
log.WithError(err).Error("Failed to register ProcessCollector")
81+
}
7282

7383
opts := server.BoostServiceOpts{
7484
Log: log,
@@ -83,6 +93,8 @@ func start(_ context.Context, cmd *cli.Command) error {
8393
RequestTimeoutGetPayload: time.Duration(cmd.Int(timeoutGetPayloadFlag.Name)) * time.Millisecond,
8494
RequestTimeoutRegVal: time.Duration(cmd.Int(timeoutRegValFlag.Name)) * time.Millisecond,
8595
RequestMaxRetries: int(cmd.Int(maxRetriesFlag.Name)),
96+
PrometheusListenAddr: int(cmd.Int(prometheusListenAddr.Name)),
97+
PrometheusRegistry: prometheusRegistry,
8698
}
8799
service, err := server.NewBoostService(opts)
88100
if err != nil {
@@ -93,6 +105,15 @@ func start(_ context.Context, cmd *cli.Command) error {
93105
log.Error("no relay passed the health-check!")
94106
}
95107

108+
if opts.PrometheusListenAddr > 0 && opts.PrometheusListenAddr <= math.MaxUint16 {
109+
go func() {
110+
log.Infof("Metric Server Listening on %d", opts.PrometheusListenAddr)
111+
if err := service.StartMetricsServer(); err != nil {
112+
log.WithError(err).Error("metrics server exited with error")
113+
}
114+
}()
115+
}
116+
96117
log.Infof("Listening on %v", listenAddr)
97118
return service.StartHTTPServer()
98119
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/google/uuid v1.6.0
1010
github.com/gorilla/mux v1.8.1
1111
github.com/holiman/uint256 v1.3.1
12+
github.com/prometheus/client_golang v1.16.0
1213
github.com/prysmaticlabs/go-bitfield v0.0.0-20240328144219-a1caa50c3a1e
1314
github.com/sirupsen/logrus v1.9.3
1415
github.com/stretchr/testify v1.9.0
@@ -44,7 +45,6 @@ require (
4445
github.com/mattn/go-isatty v0.0.20 // indirect
4546
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4647
github.com/mmcloughlin/addchain v0.4.0 // indirect
47-
github.com/prometheus/client_golang v1.16.0 // indirect
4848
github.com/prometheus/client_model v0.3.0 // indirect
4949
github.com/prometheus/common v0.42.0 // indirect
5050
github.com/prometheus/procfs v0.10.1 // indirect

server/service.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"github.com/flashbots/mev-boost/server/types"
2828
"github.com/google/uuid"
2929
"github.com/gorilla/mux"
30+
"github.com/prometheus/client_golang/prometheus"
31+
"github.com/prometheus/client_golang/prometheus/promhttp"
3032
"github.com/sirupsen/logrus"
3133
)
3234

@@ -37,6 +39,7 @@ var (
3739
errInvalidPubkey = errors.New("invalid pubkey")
3840
errNoSuccessfulRelayResponse = errors.New("no successful relay response")
3941
errServerAlreadyRunning = errors.New("server already running")
42+
errNilPrometheusRegistry = errors.New("nil prometheus registry")
4043
)
4144

4245
var (
@@ -69,6 +72,8 @@ type BoostServiceOpts struct {
6972
RequestTimeoutGetPayload time.Duration
7073
RequestTimeoutRegVal time.Duration
7174
RequestMaxRetries int
75+
PrometheusListenAddr int
76+
PrometheusRegistry *prometheus.Registry
7277
}
7378

7479
// BoostService - the mev-boost service
@@ -93,6 +98,9 @@ type BoostService struct {
9398

9499
slotUID *slotUID
95100
slotUIDLock sync.Mutex
101+
102+
prometheusListenAddr int
103+
prometheusRegistry *prometheus.Registry
96104
}
97105

98106
// NewBoostService created a new BoostService
@@ -130,7 +138,9 @@ func NewBoostService(opts BoostServiceOpts) (*BoostService, error) {
130138
Timeout: opts.RequestTimeoutRegVal,
131139
CheckRedirect: httpClientDisallowRedirects,
132140
},
133-
requestMaxRetries: opts.RequestMaxRetries,
141+
requestMaxRetries: opts.RequestMaxRetries,
142+
prometheusListenAddr: opts.PrometheusListenAddr,
143+
prometheusRegistry: opts.PrometheusRegistry,
134144
}, nil
135145
}
136146

@@ -194,6 +204,22 @@ func (m *BoostService) StartHTTPServer() error {
194204
return err
195205
}
196206

207+
// StartMetricsServer starts the HTTP server for exporting open-metrics
208+
func (m *BoostService) StartMetricsServer() error {
209+
if m.prometheusRegistry == nil {
210+
return errNilPrometheusRegistry
211+
}
212+
serveMux := http.NewServeMux()
213+
serveMux.Handle("/metrics", promhttp.HandlerFor(m.prometheusRegistry, promhttp.HandlerOpts{
214+
ErrorLog: m.log,
215+
EnableOpenMetrics: true,
216+
}))
217+
return http.ListenAndServe(
218+
fmt.Sprintf(":%d", m.prometheusListenAddr),
219+
serveMux,
220+
)
221+
}
222+
197223
func (m *BoostService) startBidCacheCleanupTask() {
198224
for {
199225
time.Sleep(1 * time.Minute)

0 commit comments

Comments
 (0)