Commit 11123565 authored by Ben Wilson's avatar Ben Wilson Committed by GitHub

Add op-exporter feature to collect baseFee (#3306)

Co-authored-by: default avatarMatthew Slipper <me@matthewslipper.com>
parent b2898008
......@@ -6,6 +6,18 @@ import (
//Define the metrics we wish to expose
var (
gasBaseFeeMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "op_baseFee",
Help: "Gas base fee."},
[]string{"network", "layer"},
)
gasUsedMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "op_gasUsed",
Help: "Gas Used."},
[]string{"network", "layer"},
)
gasPrice = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "op_gasPrice",
......@@ -38,5 +50,6 @@ func init() {
prometheus.MustRegister(blockNumber)
prometheus.MustRegister(healthySequencer)
prometheus.MustRegister(opExporterVersion)
prometheus.MustRegister(gasBaseFeeMetric)
prometheus.MustRegister(gasUsedMetric)
}
......@@ -53,6 +53,14 @@ var (
"k8s.enable",
"Enable kubernetes info lookup.",
).Default("false").Bool()
enableRollUpGasPrices = kingpin.Flag(
"rollUpGasPrices.enable",
"Enable rollUpGasPrices info lookup.",
).Default("false").Bool()
enableGasBaseFee = kingpin.Flag(
"gaseBaseFee.enable",
"Enable gaseBaseFee info lookup.",
).Default("false").Bool()
)
type healthCheck struct {
......@@ -64,6 +72,11 @@ type healthCheck struct {
version *string
}
type getBlockByNumberResponse struct {
BaseFeePerGas string `json:"baseFeePerGas"`
GasUsed string `json:"gasUsed"`
}
func healthHandler(health *healthCheck) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
health.mu.RLock()
......@@ -105,8 +118,16 @@ func main() {
</body>
</html>`))
})
go getRollupGasPrices()
go getBlockNumber(&health)
if *enableRollUpGasPrices {
go getRollupGasPrices()
}
if *enableGasBaseFee {
go getBaseFee()
}
if *enableK8sQuery {
client, err := k8sClient.Newk8sClient()
if err != nil {
......@@ -203,6 +224,33 @@ func getBlockNumber(health *healthCheck) {
}
}
func getBaseFee() {
rpcClient := jsonrpc.NewClientWithOpts(*rpcProvider, &jsonrpc.RPCClientOpts{})
var getBlockByNumbeResponse *getBlockByNumberResponse
for {
if err := rpcClient.CallFor(&getBlockByNumbeResponse, "eth_getBlockByNumber", "latest", false); err != nil {
log.Errorln("Error calling eth_getBlockByNumber", err)
} else {
log.Infoln("Got baseFee response: ", *getBlockByNumbeResponse)
baseFeePerGas, err := hexutil.DecodeUint64(getBlockByNumbeResponse.BaseFeePerGas)
if err != nil {
log.Warnln("Error converting baseFeePerGas " + getBlockByNumbeResponse.BaseFeePerGas)
}
gasBaseFeeMetric.WithLabelValues(
*networkLabel, "layer2").Set(float64(baseFeePerGas))
gasUsed, err := hexutil.DecodeUint64(getBlockByNumbeResponse.GasUsed)
if err != nil {
log.Warnln("Error converting gasUsed " + getBlockByNumbeResponse.GasUsed)
}
gasUsedMetric.WithLabelValues(
*networkLabel, "layer2").Set(float64(gasUsed))
}
time.Sleep(time.Duration(*sequencerPollingSeconds) * time.Second)
}
}
func getRollupGasPrices() {
rpcClient := jsonrpc.NewClientWithOpts(*rpcProvider, &jsonrpc.RPCClientOpts{})
var rollupGasPrices *GetRollupGasPrices
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment