Skip to content

Commit

Permalink
feat(execution): Add most recent execution block stats
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed May 16, 2022
1 parent 41db84c commit 70ff83b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/exporter/execution/jobs/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jobs

import (
"context"
"math/big"
"time"

"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -20,6 +21,11 @@ type GeneralMetrics struct {
GasPrice prometheus.Gauge
NetworkID prometheus.Gauge
ChainID prometheus.Gauge
GasUsed prometheus.Gauge
GasLimit prometheus.Gauge
BaseFeePerGas prometheus.Gauge
BlockSize prometheus.Gauge
TransactionCount prometheus.Counter
}

const (
Expand Down Expand Up @@ -73,6 +79,46 @@ func NewGeneralMetrics(client *ethclient.Client, internalApi api.ExecutionClient
ConstLabels: constLabels,
},
),
GasUsed: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "gas_used",
Help: "The gas used in the most recent block.",
ConstLabels: constLabels,
},
),
GasLimit: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "gas_limit",
Help: "The gas limit of the most recent block.",
ConstLabels: constLabels,
},
),
BaseFeePerGas: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "base_fee_per_gas",
Help: "The base fee per gas in the most recent block.",
ConstLabels: constLabels,
},
),
BlockSize: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "block_size_bytes",
Help: "The size of the most recent block (in bytes).",
ConstLabels: constLabels,
},
),
TransactionCount: prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Name: "transaction_count",
Help: "The number of transactions in the most recent block.",
ConstLabels: constLabels,
},
),
}
}

Expand Down Expand Up @@ -104,6 +150,10 @@ func (g *GeneralMetrics) tick(ctx context.Context) {
if _, err := g.GetChainID(ctx); err != nil {
g.log.WithError(err).Error("failed to get chain id")
}

if err := g.GetMostRecentBlockStats(ctx); err != nil {
g.log.WithError(err).Error("failed to get most recent block stats")
}
}

func (g *GeneralMetrics) GetMostRecentBlockNumber(ctx context.Context) (uint64, error) {
Expand Down Expand Up @@ -149,3 +199,23 @@ func (g *GeneralMetrics) GetChainID(ctx context.Context) (uint64, error) {

return chainID.Uint64(), nil
}

func (g *GeneralMetrics) GetMostRecentBlockStats(ctx context.Context) error {
mostRecentBlockNumber, err := g.client.BlockNumber(ctx)
if err != nil {
return err
}

block, err := g.client.BlockByNumber(ctx, big.NewInt(int64(mostRecentBlockNumber)))
if err != nil {
return err
}

g.GasUsed.Set(float64(block.GasUsed()))
g.GasLimit.Set(float64(block.GasLimit()))
g.BaseFeePerGas.Set(float64(block.BaseFee().Int64()))
g.BlockSize.Set(float64(block.Size()))
g.TransactionCount.Add(float64(len(block.Transactions())))

return nil
}
5 changes: 5 additions & 0 deletions pkg/exporter/execution/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func NewMetrics(client *ethclient.Client, internalApi api.ExecutionClient, log l
prometheus.MustRegister(m.generalMetrics.GasPrice)
prometheus.MustRegister(m.generalMetrics.MostRecentBlockNumber)
prometheus.MustRegister(m.generalMetrics.ChainID)
prometheus.MustRegister(m.generalMetrics.GasLimit)
prometheus.MustRegister(m.generalMetrics.GasUsed)
prometheus.MustRegister(m.generalMetrics.TransactionCount)
prometheus.MustRegister(m.generalMetrics.BaseFeePerGas)
prometheus.MustRegister(m.generalMetrics.BlockSize)
}

if able := jobs.ExporterCanRun(enabledModules, m.txpoolMetrics.RequiredModules()); able {
Expand Down

0 comments on commit 70ff83b

Please sign in to comment.