Skip to content

Commit

Permalink
Merge pull request #14 from samcm/feat/new-td-method
Browse files Browse the repository at this point in the history
feat(execution): Fetch total difficulty from the Head block rather th…
  • Loading branch information
samcm committed May 17, 2022
2 parents 2d12614 + d154cb7 commit fce073c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 60 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
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/onrik/ethrpc v1.0.0 // 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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onrik/ethrpc v1.0.0 h1:caG0U/Y1op32mntqmDnwvzue4Tg37cSA2EU8PMIMSjM=
github.com/onrik/ethrpc v1.0.0/go.mod h1:RoqOlDiBBs1qYamkcYhxMgkPijxu5R8t55mgUiy4le8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
Expand Down
30 changes: 17 additions & 13 deletions pkg/exporter/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
)
Expand All @@ -24,27 +25,30 @@ type Node interface {
}

type node struct {
name string
url string
client *ethclient.Client
internalApi api.ExecutionClient
log logrus.FieldLogger
metrics Metrics
name string
url string
client *ethclient.Client
internalApi api.ExecutionClient
ethrpcClient *ethrpc.EthRPC
log logrus.FieldLogger
metrics Metrics
}

// NewExecutionNode returns a new execution node.
func NewExecutionNode(ctx context.Context, log logrus.FieldLogger, namespace string, nodeName string, url string, enabledModules []string) (Node, error) {
internalApi := api.NewExecutionClient(ctx, log, url)
client, _ := ethclient.Dial(url)
metrics := NewMetrics(client, internalApi, log, nodeName, namespace, enabledModules)
ethrpcClient := ethrpc.New(url)
metrics := NewMetrics(client, internalApi, ethrpcClient, log, nodeName, namespace, enabledModules)

node := &node{
name: nodeName,
url: url,
log: log,
internalApi: internalApi,
client: client,
metrics: metrics,
name: nodeName,
url: url,
log: log,
ethrpcClient: ethrpcClient,
internalApi: internalApi,
client: client,
metrics: metrics,
}

return node, nil
Expand Down
11 changes: 7 additions & 4 deletions pkg/exporter/execution/jobs/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api/types"
Expand All @@ -18,6 +19,7 @@ type Admin struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
ethrpcClient *ethrpc.EthRPC
log logrus.FieldLogger
NodeInfo prometheus.GaugeVec
Port prometheus.GaugeVec
Expand All @@ -39,14 +41,15 @@ func (t *Admin) RequiredModules() []string {
}

// NewAdmin returns a new Admin instance.
func NewAdmin(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, namespace string, constLabels map[string]string) Admin {
func NewAdmin(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) Admin {
namespace = namespace + "_admin"
constLabels["module"] = NameAdmin

return Admin{
client: client,
api: internalApi,
log: log.WithField("module", NameAdmin),
client: client,
api: internalApi,
ethrpcClient: ethRpcClient,
log: log.WithField("module", NameAdmin),
NodeInfo: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
64 changes: 45 additions & 19 deletions pkg/exporter/execution/jobs/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
Expand All @@ -16,17 +17,20 @@ import (
// BlockMetrics exposes metrics on the head/safest block.
type BlockMetrics struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
log logrus.FieldLogger
client *ethclient.Client
api api.ExecutionClient
ethRpcClient *ethrpc.EthRPC
log logrus.FieldLogger

MostRecentBlockNumber prometheus.GaugeVec

HeadGasUsed prometheus.Gauge
HeadGasLimit prometheus.Gauge
HeadBaseFeePerGas prometheus.Gauge
HeadBlockSize prometheus.Gauge
HeadTransactionCount prometheus.Gauge
HeadGasUsed prometheus.Gauge
HeadGasLimit prometheus.Gauge
HeadBaseFeePerGas prometheus.Gauge
HeadBlockSize prometheus.Gauge
HeadTransactionCount prometheus.Gauge
HeadTotalDifficulty prometheus.Gauge
HeadTotalDifficultyTrillions prometheus.Gauge

SafeGasUsed prometheus.Counter
SafeGasLimit prometheus.Counter
Expand All @@ -52,13 +56,14 @@ func (b *BlockMetrics) RequiredModules() []string {
}

// NewBlockMetrics returns a new Block metrics instance.
func NewBlockMetrics(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, namespace string, constLabels map[string]string) BlockMetrics {
func NewBlockMetrics(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) BlockMetrics {
constLabels["module"] = NameBlock
namespace = namespace + "_" + NameBlock
return BlockMetrics{
client: client,
api: internalApi,
log: log.WithField("module", NameBlock),
client: client,
api: internalApi,
ethRpcClient: ethRpcClient,
log: log.WithField("module", NameBlock),

MostRecentBlockNumber: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -112,6 +117,22 @@ func NewBlockMetrics(client *ethclient.Client, internalApi api.ExecutionClient,
ConstLabels: constLabels,
},
),
HeadTotalDifficulty: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "head_total_difficulty",
Help: "The total difficulty of the head block.",
ConstLabels: constLabels,
},
),
HeadTotalDifficultyTrillions: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "head_total_difficulty_trillions",
Help: "The total difficulty of the head block (in trillions).",
ConstLabels: constLabels,
},
),

SafeGasUsed: prometheus.NewCounter(
prometheus.CounterOpts{
Expand Down Expand Up @@ -197,17 +218,22 @@ func (b *BlockMetrics) getHeadBlockStats(ctx context.Context) error {
b.currentHeadBlockNumber = mostRecentBlockNumber
b.MostRecentBlockNumber.WithLabelValues("head").Set(float64(mostRecentBlockNumber))

block, err := b.client.BlockByNumber(ctx, big.NewInt(int64(mostRecentBlockNumber)))
block, err := b.ethRpcClient.EthGetBlockByNumber(int(mostRecentBlockNumber), true)
if err != nil {
return err
}

b.HeadGasUsed.Set(float64(block.GasUsed()))
b.HeadGasLimit.Set(float64(block.GasLimit()))
b.HeadBaseFeePerGas.Set(float64(block.BaseFee().Int64()))
b.HeadBlockSize.Set(float64(block.Size()))
b.HeadTransactionCount.Set(float64(len(block.Transactions())))

b.HeadGasUsed.Set(float64(block.GasUsed))
b.HeadGasLimit.Set(float64(block.GasLimit))
b.HeadBlockSize.Set(float64(block.Size))
b.HeadTransactionCount.Set(float64(len(block.Transactions)))
// b.HeadBaseFeePerGas.Set(float64(block.BaseFee().Int64())) TODO(sam.calder-mason): Fix me

b.HeadTotalDifficulty.Set(float64(block.TotalDifficulty.Uint64()))
// Since we can't represent a big.Int as a float64, and the TD on mainnet is beyond float64, we'll divide the number by a trillion
trillion := big.NewInt(1e12)
divided := new(big.Int).Quo(&block.TotalDifficulty, trillion)
b.HeadTotalDifficultyTrillions.Set(float64(divided.Uint64()))
return nil
}

Expand Down
23 changes: 13 additions & 10 deletions pkg/exporter/execution/jobs/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
Expand All @@ -13,12 +14,13 @@ import (
// GeneralMetrics exposes metrics that otherwise don't fit in to a specific module.
type GeneralMetrics struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
log logrus.FieldLogger
GasPrice prometheus.Gauge
NetworkID prometheus.Gauge
ChainID prometheus.Gauge
client *ethclient.Client
api api.ExecutionClient
ethRpcClient *ethrpc.EthRPC
log logrus.FieldLogger
GasPrice prometheus.Gauge
NetworkID prometheus.Gauge
ChainID prometheus.Gauge
}

const (
Expand All @@ -34,12 +36,13 @@ func (g *GeneralMetrics) RequiredModules() []string {
}

// NewGeneralMetrics returns a new General metrics instance.
func NewGeneralMetrics(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, namespace string, constLabels map[string]string) GeneralMetrics {
func NewGeneralMetrics(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) GeneralMetrics {
constLabels["module"] = NameGeneral
return GeneralMetrics{
client: client,
api: internalApi,
log: log.WithField("module", NameGeneral),
client: client,
api: internalApi,
ethRpcClient: ethRpcClient,
log: log.WithField("module", NameGeneral),
GasPrice: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
11 changes: 7 additions & 4 deletions pkg/exporter/execution/jobs/syncstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
Expand All @@ -15,6 +16,7 @@ type SyncStatus struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
ethRpcClient *ethrpc.EthRPC
log logrus.FieldLogger
Percentage prometheus.Gauge
CurrentBlock prometheus.Gauge
Expand Down Expand Up @@ -51,13 +53,14 @@ func (s *syncingStatus) Percent() float64 {
}

// NewSyncStatus returns a new SyncStatus instance.
func NewSyncStatus(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, namespace string, constLabels map[string]string) SyncStatus {
func NewSyncStatus(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) SyncStatus {
constLabels["module"] = NameSyncStatus
namespace = namespace + "_sync"
return SyncStatus{
client: client,
api: internalApi,
log: log.WithField("module", NameSyncStatus),
client: client,
api: internalApi,
ethRpcClient: ethRpcClient,
log: log.WithField("module", NameSyncStatus),
Percentage: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
11 changes: 7 additions & 4 deletions pkg/exporter/execution/jobs/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/sirupsen/logrus"
Expand All @@ -15,6 +16,7 @@ type TXPool struct {
MetricExporter
client *ethclient.Client
api api.ExecutionClient
ethRpcClient *ethrpc.EthRPC
log logrus.FieldLogger
Transactions prometheus.GaugeVec
}
Expand All @@ -32,13 +34,14 @@ func (t *TXPool) RequiredModules() []string {
}

// NewTXPool creates a new TXPool instance.
func NewTXPool(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, namespace string, constLabels map[string]string) TXPool {
func NewTXPool(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, namespace string, constLabels map[string]string) TXPool {
constLabels["module"] = NameTxPool
namespace = namespace + "_txpool"
return TXPool{
client: client,
api: internalApi,
log: log.WithField("module", NameGeneral),
client: client,
api: internalApi,
ethRpcClient: ethRpcClient,
log: log.WithField("module", NameGeneral),
Transactions: *prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
15 changes: 9 additions & 6 deletions pkg/exporter/execution/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/ethereum/go-ethereum/ethclient"
"github.com/onrik/ethrpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/api"
"github.com/samcm/ethereum-metrics-exporter/pkg/exporter/execution/jobs"
Expand All @@ -28,18 +29,18 @@ type metrics struct {
}

// NewMetrics creates a new execution Metrics instance
func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, log logrus.FieldLogger, nodeName, namespace string, enabledModules []string) Metrics {
func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, ethRpcClient *ethrpc.EthRPC, log logrus.FieldLogger, nodeName, namespace string, enabledModules []string) Metrics {
constLabels := make(prometheus.Labels)
constLabels["ethereum_role"] = "execution"
constLabels["node_name"] = nodeName

m := &metrics{
log: log,
generalMetrics: jobs.NewGeneralMetrics(client, internalApi, log, namespace, constLabels),
syncMetrics: jobs.NewSyncStatus(client, internalApi, log, namespace, constLabels),
txpoolMetrics: jobs.NewTXPool(client, internalApi, log, namespace, constLabels),
adminMetrics: jobs.NewAdmin(client, internalApi, log, namespace, constLabels),
blockMetrics: jobs.NewBlockMetrics(client, internalApi, log, namespace, constLabels),
generalMetrics: jobs.NewGeneralMetrics(client, internalApi, ethRpcClient, log, namespace, constLabels),
syncMetrics: jobs.NewSyncStatus(client, internalApi, ethRpcClient, log, namespace, constLabels),
txpoolMetrics: jobs.NewTXPool(client, internalApi, ethRpcClient, log, namespace, constLabels),
adminMetrics: jobs.NewAdmin(client, internalApi, ethRpcClient, log, namespace, constLabels),
blockMetrics: jobs.NewBlockMetrics(client, internalApi, ethRpcClient, log, namespace, constLabels),

enabledJobs: make(map[string]bool),
}
Expand Down Expand Up @@ -75,6 +76,8 @@ func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, log l
prometheus.MustRegister(m.blockMetrics.HeadGasUsed)
prometheus.MustRegister(m.blockMetrics.HeadTransactionCount)
prometheus.MustRegister(m.blockMetrics.HeadBaseFeePerGas)
prometheus.MustRegister(m.blockMetrics.HeadTotalDifficulty)
prometheus.MustRegister(m.blockMetrics.HeadTotalDifficultyTrillions)

prometheus.MustRegister(m.blockMetrics.SafeBaseFeePerGas)
prometheus.MustRegister(m.blockMetrics.SafeBlockSize)
Expand Down

0 comments on commit fce073c

Please sign in to comment.