Skip to content

Commit

Permalink
Add webhost with metrics endpoint to benchmark tool
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-ssvlabs committed Sep 6, 2024
1 parent cee012f commit 35a031d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
5 changes: 5 additions & 0 deletions configs/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ type Infrastructure struct {
Metrics InfrastructureMetrics `mapstructure:"metrics"`
}

type Server struct {
Port uint16 `mapstructure:"port"`
}

type Benchmark struct {
Consensus Consensus `mapstructure:"consensus"`
Execution Execution `mapstructure:"execution"`
SSV SSV `mapstructure:"ssv"`
Infrastructure Infrastructure `mapstructure:"infrastructure"`
Server Server `mapstructure:"server"`
Duration time.Duration `mapstructure:"duration"`
Network string `mapstructure:"network"`
}
Expand Down
3 changes: 3 additions & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
benchmark:
duration: 15m
network: mainnet
server:
port: 8080

consensus:
address:
metrics:
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aquasecurity/table v1.8.0
github.com/grafana/loki-client-go v0.0.0-20230116142646-e7494d0ef70c
github.com/mackerelio/go-osstat v0.2.5
github.com/prometheus/client_golang v1.20.0
github.com/prometheus/common v0.57.0
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
Expand Down Expand Up @@ -34,6 +35,7 @@ require (
github.com/huandu/go-clone v1.6.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -45,7 +47,6 @@ require (
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/pk910/dynamic-ssz v0.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/prometheus/prometheus v0.35.0 // indirect
Expand Down
23 changes: 20 additions & 3 deletions internal/benchmark/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ import (
"github.com/ssvlabsinfra/ssv-pulse/configs"
"github.com/ssvlabsinfra/ssv-pulse/internal/benchmark/report"
"github.com/ssvlabsinfra/ssv-pulse/internal/platform/lifecycle"
"github.com/ssvlabsinfra/ssv-pulse/internal/platform/server/host"
"github.com/ssvlabsinfra/ssv-pulse/internal/platform/server/route"
)

const (
durationFlag = "duration"
durationFlag = "duration"
defaultExecutionDuration = time.Minute * 15

serverPortFlag = "port"
defaultServerPort = 8080

consensusAddrFlag = "consensus-addr"
consensusMetricClientFlag = "consensus-metric-client-enabled"
Expand All @@ -33,8 +39,7 @@ const (
infraMetricCPUFlag = "infra-metric-cpu-enabled"
infraMetricMemoryFlag = "infra-metric-memory-enabled"

networkFlag = "network"
defaultExecutionDuration = time.Minute * 15
networkFlag = "network"
)

func init() {
Expand All @@ -59,6 +64,14 @@ var CMD = &cobra.Command{

go benchmarkService.Start(ctx)

slog.With("port", configs.Values.Benchmark.Server.Port).Info("running web host")
host := host.New(configs.Values.Benchmark.Server.Port,
route.
NewRouter().
WithMetrics().
Router())
host.Run()

lifecycle.ListenForApplicationShutDown(ctx, func() {
cancel()
slog.Warn("terminating the application")
Expand All @@ -68,6 +81,7 @@ var CMD = &cobra.Command{

func addFlags(cobraCMD *cobra.Command) {
cobraCMD.Flags().Duration(durationFlag, defaultExecutionDuration, "Duration for which the application will run to gather metrics, e.g. '5m'")
cobraCMD.Flags().Uint16(serverPortFlag, defaultServerPort, "Web server port with metrics endpoint exposed, e.g. '8080'")
cobraCMD.Flags().String(consensusAddrFlag, "", "Consensus client address (beacon node API) with scheme (HTTP/HTTPS) and port, e.g. https://lighthouse:5052")
cobraCMD.Flags().Bool(consensusMetricClientFlag, true, "Enable consensus client metric")
cobraCMD.Flags().Bool(consensusMetricLatencyFlag, true, "Enable consensus latency metric")
Expand All @@ -91,6 +105,9 @@ func bindFlags(cmd *cobra.Command) error {
if err := viper.BindPFlag("benchmark.duration", cmd.Flags().Lookup(durationFlag)); err != nil {
return err
}
if err := viper.BindPFlag("benchmark.server.port", cmd.Flags().Lookup(serverPortFlag)); err != nil {
return err
}
if err := viper.BindPFlag("benchmark.consensus.address", cmd.Flags().Lookup(consensusAddrFlag)); err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions internal/platform/server/host/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package host

import (
"context"
"fmt"
"log/slog"
"net/http"
)

const hostName = "0.0.0.0"

type WebHost struct {
server http.Server
}

func New(port uint16, handler http.Handler) *WebHost {
return &WebHost{
server: http.Server{
Addr: fmt.Sprintf("%s:%d", hostName, port),
Handler: handler,
},
}
}

func (h *WebHost) Run() {
go func() {
if err := h.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
const errMsg = "error running web host"
slog.With("error", err.Error()).Error(errMsg)
panic(errMsg)
}
}()
}

func (h *WebHost) Terminate(ctx context.Context) error {
return h.server.Shutdown(ctx)
}
27 changes: 27 additions & 0 deletions internal/platform/server/route/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package route

import (
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

type Router struct {
router *http.ServeMux
}

func NewRouter() *Router {
router := http.NewServeMux()
return &Router{
router: router,
}
}

func (r *Router) WithMetrics() *Router {
r.router.Handle("/metrics", promhttp.Handler())
return r
}

func (r *Router) Router() *http.ServeMux {
return r.router
}

0 comments on commit 35a031d

Please sign in to comment.