Skip to content

Commit

Permalink
feat(initial): Basic sync status metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed May 3, 2022
0 parents commit 9db88ad
Show file tree
Hide file tree
Showing 21 changed files with 1,664 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# syntax=docker/dockerfile:1
FROM golang:1.18 AS builder
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /bin/app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add jq curl
COPY --from=builder /bin/app /bin/app
ENTRYPOINT ["/bin/app"]
Empty file added LICENSE
Empty file.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## README

WIP
88 changes: 88 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"context"
"io/ioutil"
"os"

"github.com/samcm/ethereum-metrics-exporter/pkg/exporter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ethereum-metrics-exporter",
Short: "A tool to report the sync status of ethereum nodes",
}

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

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}

}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

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

// 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")
}

func loadConfigFromFile(file string) (*exporter.Config, error) {
if file == "" {
return exporter.DefaultConfig(), nil
}

var config exporter.Config
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

if err := yaml.Unmarshal(yamlFile, &config); err != nil {
return nil, err
}

return &config, nil
}

func initCommon() {
ctx = context.Background()
log := logrus.New()
log.SetFormatter(&logrus.JSONFormatter{})
logr = log

log.WithField("cfgFile", cfgFile).Info("Loading config")
config, err := loadConfigFromFile(cfgFile)
if err != nil {
logr.Fatal(err)
}

ethClient = exporter.NewEthereum(log, config)
if err := ethClient.Init(ctx); err != nil {
logrus.Fatal(err)
}
}
35 changes: 35 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"
)

var (
exitWhenSynced bool
metricsPort int
)

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Run a metrics server and poll the configured clients.",
Run: func(cmd *cobra.Command, args []string) {
initCommon()

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

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")
}
40 changes: 40 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Outputs the sync status of the ethereum nodes",
Run: func(cmd *cobra.Command, args []string) {
initCommon()

status, err := ethClient.GetSyncStatus(ctx)
if err != nil {
logrus.Fatal(err)
}

logr.Info(status)
},
}

func init() {
rootCmd.AddCommand(statusCmd)
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// statusCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
10 changes: 10 additions & 0 deletions example_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pollingFrequencySeconds: 5

consensus:
enabled: true
url: "http://192.168.0.128:5052"
name: "consensus-client"
execution:
enabled: true
url: "http://192.168.0.128:8545"
name: "execution-client"
59 changes: 59 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module github.com/samcm/ethereum-metrics-exporter

go 1.17

// replace github.com/samcm/ethereum-metrics-exporter/pkg/exporter => ./pkg/ethereum-metrics-exporter

require (
github.com/spf13/cobra v1.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/attestantio/go-eth2-client v0.11.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/go-ethereum v1.10.17 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/ferranbt/fastssz v0.0.0-20220103083642-bc5fefefa28b // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-yaml v1.9.5 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.11 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
github.com/r3labs/sse/v2 v2.7.4 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 9db88ad

Please sign in to comment.