Skip to content

Commit

Permalink
feat(cmd): Run serve from root command and support url args
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed May 5, 2022
1 parent d642526 commit 337e769
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
34 changes: 29 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ import (
var rootCmd = &cobra.Command{
Use: "ethereum-metrics-exporter",
Short: "A tool to report the sync status of ethereum nodes",
Run: func(cmd *cobra.Command, args []string) {
initCommon()

err := ethClient.Serve(ctx, metricsPort)
if err != nil {
logr.Fatal(err)
}
},
}

var (
cfgFile string
config *exporter.Config
ethClient exporter.Ethereum
ctx context.Context
logr logrus.FieldLogger
cfgFile string
config *exporter.Config
ethClient exporter.Ethereum
ctx context.Context
logr logrus.FieldLogger
executionUrl string
consensusUrl string
)

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -42,6 +52,10 @@ func init() {

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ethereum-metrics-exporter.yaml)")

rootCmd.PersistentFlags().IntVarP(&metricsPort, "metrics-port", "", 9090, "Port to serve Prometheus metrics on")
rootCmd.PersistentFlags().StringVarP(&executionUrl, "execution-url", "", "", "(optional) URL to the execution node")
rootCmd.PersistentFlags().StringVarP(&consensusUrl, "consensus-url", "", "", "(optional) URL to the consensus node")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down Expand Up @@ -77,6 +91,16 @@ func initCommon() {
logr.Fatal(err)
}

if executionUrl != "" {
config.Execution.Enabled = true
config.Execution.URL = executionUrl
}

if consensusUrl != "" {
config.Consensus.Enabled = true
config.Consensus.URL = consensusUrl
}

ethClient = exporter.NewEthereum(log, config)
if err := ethClient.Init(ctx); err != nil {
logrus.Fatal(err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ var serveCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(serveCmd)

serveCmd.Flags().BoolVarP(&exitWhenSynced, "exit-when-synced", "", false, "Exit the program when both clients are synced")
serveCmd.Flags().IntVarP(&metricsPort, "metrics-port", "", 9090, "Port to serve Prometheus metrics on")
}
2 changes: 1 addition & 1 deletion example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pollingFrequencySeconds: 5

consensus:
enabled: true
url: "http://192.168.0.128:5052"
url: "http://192.168.0.128:5053"
name: "consensus-client"
execution:
enabled: true
Expand Down
30 changes: 27 additions & 3 deletions pkg/exporter/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"context"
"errors"

eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/http"
Expand All @@ -11,12 +12,13 @@ import (

type Node interface {
Name() string
URL() string
SyncStatus(ctx context.Context) (*SyncStatus, error)
}

type node struct {
name string
URL string
url string
client eth2client.Service
log logrus.FieldLogger
metrics Metrics
Expand All @@ -33,7 +35,7 @@ func NewConsensusNode(ctx context.Context, log logrus.FieldLogger, name string,

return &node{
name: name,
URL: url,
url: url,
client: client,
log: log,
metrics: metrics,
Expand All @@ -44,8 +46,30 @@ func (c *node) Name() string {
return c.name
}

func (c *node) URL() string {
return c.url
}

func (c *node) refreshClient(ctx context.Context) error {
client, err := http.New(ctx,
http.WithAddress(c.url),
http.WithLogLevel(zerolog.WarnLevel),
)
if err != nil {
return err
}

c.client = client
return nil
}

func (c *node) SyncStatus(ctx context.Context) (*SyncStatus, error) {
provider := c.client.(eth2client.NodeSyncingProvider)
provider, isProvider := c.client.(eth2client.NodeSyncingProvider)
if !isProvider {
c.refreshClient(ctx)
return nil, errors.New("client does not implement eth2client.NodeSyncingProvider")
}

status, err := provider.NodeSyncing(ctx)
if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion pkg/exporter/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func (e *ethereum) ticker(ctx context.Context) {

func (e *ethereum) Serve(ctx context.Context, port int) error {
go e.ticker(ctx)
e.log.Info(fmt.Sprintf("Starting metrics server on :%v", port))
e.log.
WithField("consensus_url", e.consensus.URL()).
WithField("execution_url", e.execution.URL()).
Info(fmt.Sprintf("Starting metrics server on :%v", port))

http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
Expand Down
9 changes: 7 additions & 2 deletions pkg/exporter/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (

type Node interface {
Name() string
URL() string
SyncStatus(ctx context.Context) (*SyncStatus, error)
}

type node struct {
name string
URL string
url string
client ethclient.Client
log logrus.FieldLogger
metrics Metrics
Expand All @@ -27,7 +28,7 @@ func NewExecutionNode(ctx context.Context, log logrus.FieldLogger, name string,

return &node{
name: name,
URL: url,
url: url,
client: *client,
log: log,
metrics: metrics,
Expand All @@ -38,6 +39,10 @@ func (e *node) Name() string {
return e.name
}

func (e *node) URL() string {
return e.url
}

func (e *node) SyncStatus(ctx context.Context) (*SyncStatus, error) {
status, err := e.client.SyncProgress(ctx)
if err != nil {
Expand Down

0 comments on commit 337e769

Please sign in to comment.