Skip to content

Commit

Permalink
metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Sep 27, 2024
1 parent 88a707e commit 75429d3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
37 changes: 33 additions & 4 deletions cmd/httpserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package main

import (
"log"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
"time"

"github.com/VictoriaMetrics/metrics"
"github.com/flashbots/orderflow-proxy/common"
"github.com/flashbots/orderflow-proxy/proxy"
"github.com/google/uuid"
Expand All @@ -17,7 +20,7 @@ var flags []cli.Flag = []cli.Flag{
&cli.StringFlag{
Name: "metrics-addr",
Value: "127.0.0.1:8090",
Usage: "address to listen on for Prometheus metrics",
Usage: "address to listen on for Prometheus metrics (metrics are served on $metrics-addr/metrics)",
},
&cli.BoolFlag{
Name: "log-json",
Expand All @@ -42,7 +45,7 @@ var flags []cli.Flag = []cli.Flag{
&cli.BoolFlag{
Name: "pprof",
Value: false,
Usage: "enable pprof debug endpoint",
Usage: "enable pprof debug endpoint (pprof is served on $metrics-addr/debug/pprof/*)",
},
&cli.StringFlag{
Name: "listen-addr",
Expand Down Expand Up @@ -77,8 +80,6 @@ func main() {
Usage: "Serve API, and metrics",
Flags: flags,
Action: func(cCtx *cli.Context) error {
_ = cCtx.String("metrics-addr")
_ = cCtx.Bool("pprof")
logJSON := cCtx.Bool("log-json")
logDebug := cCtx.Bool("log-debug")
logUID := cCtx.Bool("log-uid")
Expand All @@ -99,6 +100,34 @@ func main() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)

// metrics server
go func() {
metricsAddr := cCtx.String("metrics-addr")
usePprof := cCtx.Bool("pprof")
metricsMux := http.NewServeMux()
metricsMux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
metrics.WritePrometheus(w, true)
})
if usePprof {
metricsMux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
metricsMux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
metricsMux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
metricsMux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
metricsMux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}

metricsServer := &http.Server{
Addr: metricsAddr,
ReadHeaderTimeout: 5 * time.Second,
Handler: metricsMux,
}

err := metricsServer.ListenAndServe()
if err != nil {
log.Error("Failed to start metrics server", "err", err)
}
}()

builderEndpoint := cCtx.String("builder-endpoint")
listedAddr := cCtx.String("listen-addr")
certDuration := cCtx.Duration("cert-duration")
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
)

require (
github.com/VictoriaMetrics/metrics v1.35.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
Expand All @@ -42,6 +43,8 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
github.com/valyala/histogram v1.2.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/VictoriaMetrics/metrics v1.35.1 h1:o84wtBKQbzLdDy14XeskkCZih6anG+veZ1SwJHFGwrU=
github.com/VictoriaMetrics/metrics v1.35.1/go.mod h1:r7hveu6xMdUACXvB8TYdAj8WEsKzWB0EkpJN+RDtOf8=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -82,6 +84,10 @@ github.com/supranational/blst v0.3.13 h1:AYeSxdOMacwu7FBmpfloBz5pbFXDmJL33RuwnKt
github.com/supranational/blst v0.3.13/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
go.opentelemetry.io/contrib/instrumentation/runtime v0.46.1 h1:m9ReioVPIffxjJlGNRd0d5poy+9oTro3D+YbiEzUDOc=
Expand Down
11 changes: 11 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package metrics

Check failure on line 1 in metrics/metrics.go

View workflow job for this annotation

GitHub Actions / Lint

at least one file in a package should have a package comment (ST1000)

import "github.com/VictoriaMetrics/metrics"

var (
totalRequestReceived = metrics.NewCounter("requests_total")
)

func IncRequestsReceived() {
totalRequestReceived.Inc()
}
2 changes: 2 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/flashbots/orderflow-proxy/metrics"
)

type Config struct {
Expand Down Expand Up @@ -38,6 +39,7 @@ func New(config Config) (*Proxy, error) {
}

func (prx *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
metrics.IncRequestsReceived()
body, err := io.ReadAll(r.Body)
if err != nil {
return
Expand Down

0 comments on commit 75429d3

Please sign in to comment.