Commit 6a033e05 authored by Madhur Shrimal's avatar Madhur Shrimal

refactor to make it extensible

parent ec1c0357
...@@ -30,6 +30,7 @@ type Config struct { ...@@ -30,6 +30,7 @@ type Config struct {
AllowNonFinalized bool AllowNonFinalized bool
From common.Address From common.Address
SignerFnFactory opcrypto.SignerFactory SignerFnFactory opcrypto.SignerFactory
metricsEnabled bool
} }
// CLIConfig is a well typed config that is parsed from the CLI params. // CLIConfig is a well typed config that is parsed from the CLI params.
......
...@@ -13,8 +13,6 @@ import ( ...@@ -13,8 +13,6 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
...@@ -80,16 +78,19 @@ func Main(version string, cliCtx *cli.Context) error { ...@@ -80,16 +78,19 @@ func Main(version string, cliCtx *cli.Context) error {
}() }()
} }
registry := opmetrics.NewRegistry()
metricsCfg := cfg.MetricsConfig metricsCfg := cfg.MetricsConfig
if metricsCfg.Enabled { if metricsCfg.Enabled {
metricsRegistry := opmetrics.InitProposerMetricsRegistry(registry, "")
l2OutputSubmitter.mr = metricsRegistry
l.Info("starting metrics server", "addr", metricsCfg.ListenAddr, "port", metricsCfg.ListenPort) l.Info("starting metrics server", "addr", metricsCfg.ListenAddr, "port", metricsCfg.ListenPort)
go func() { go func() {
if err := opmetrics.ListenAndServe(ctx, l2OutputSubmitter.metricsRegistry, metricsCfg.ListenAddr, metricsCfg.ListenPort); err != nil { if err := opmetrics.ListenAndServe(ctx, registry, metricsCfg.ListenAddr, metricsCfg.ListenPort); err != nil {
l.Error("error starting metrics server", err) l.Error("error starting metrics server", err)
} }
}() }()
addr := l2OutputSubmitter.from addr := l2OutputSubmitter.from
opmetrics.LaunchBalanceMetrics(ctx, l, l2OutputSubmitter.metricsRegistry, "", l2OutputSubmitter.l1Client, addr) opmetrics.LaunchBalanceMetrics(ctx, l, registry, "", l2OutputSubmitter.l1Client, addr)
} }
rpcCfg := cfg.RPCConfig rpcCfg := cfg.RPCConfig
...@@ -114,11 +115,12 @@ func Main(version string, cliCtx *cli.Context) error { ...@@ -114,11 +115,12 @@ func Main(version string, cliCtx *cli.Context) error {
// L2OutputSubmitter is responsible for proposing outputs // L2OutputSubmitter is responsible for proposing outputs
type L2OutputSubmitter struct { type L2OutputSubmitter struct {
txMgr txmgr.TxManager txMgr txmgr.TxManager
wg sync.WaitGroup wg sync.WaitGroup
done chan struct{} done chan struct{}
log log.Logger log log.Logger
metricsRegistry *prometheus.Registry mr *opmetrics.ProposerMetricsRegistry
metricsEnabled bool
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
...@@ -185,6 +187,7 @@ func NewL2OutputSubmitterFromCLIConfig(cfg CLIConfig, l log.Logger) (*L2OutputSu ...@@ -185,6 +187,7 @@ func NewL2OutputSubmitterFromCLIConfig(cfg CLIConfig, l log.Logger) (*L2OutputSu
AllowNonFinalized: cfg.AllowNonFinalized, AllowNonFinalized: cfg.AllowNonFinalized,
From: fromAddress, From: fromAddress,
SignerFnFactory: signer, SignerFnFactory: signer,
metricsEnabled: cfg.MetricsConfig.Enabled,
} }
return NewL2OutputSubmitter(proposerCfg, l) return NewL2OutputSubmitter(proposerCfg, l)
...@@ -225,12 +228,12 @@ func NewL2OutputSubmitter(cfg Config, l log.Logger) (*L2OutputSubmitter, error) ...@@ -225,12 +228,12 @@ func NewL2OutputSubmitter(cfg Config, l log.Logger) (*L2OutputSubmitter, error)
rawL2ooContract := bind.NewBoundContract(cfg.L2OutputOracleAddr, parsed, cfg.L1Client, cfg.L1Client, cfg.L1Client) rawL2ooContract := bind.NewBoundContract(cfg.L2OutputOracleAddr, parsed, cfg.L1Client, cfg.L1Client, cfg.L1Client)
return &L2OutputSubmitter{ return &L2OutputSubmitter{
txMgr: txmgr.NewSimpleTxManager("proposer", l, cfg.TxManagerConfig, cfg.L1Client), txMgr: txmgr.NewSimpleTxManager("proposer", l, cfg.TxManagerConfig, cfg.L1Client),
done: make(chan struct{}), done: make(chan struct{}),
log: l, log: l,
metricsRegistry: opmetrics.NewRegistry(), ctx: ctx,
ctx: ctx, cancel: cancel,
cancel: cancel, metricsEnabled: cfg.metricsEnabled,
l1Client: cfg.L1Client, l1Client: cfg.L1Client,
rollupClient: cfg.RollupClient, rollupClient: cfg.RollupClient,
...@@ -378,8 +381,10 @@ func (l *L2OutputSubmitter) SendTransaction(ctx context.Context, tx *types.Trans ...@@ -378,8 +381,10 @@ func (l *L2OutputSubmitter) SendTransaction(ctx context.Context, tx *types.Trans
return err return err
} }
// Emit the proposed block Number if l.metricsEnabled {
opmetrics.EmitBlockNumber(l.metricsRegistry, "", receipt.BlockNumber) // Emit the proposed block Number
opmetrics.EmitBlockNumber(l.mr.BlockNumberGauge, "", receipt.BlockNumber)
}
// The transaction was successfully submitted // The transaction was successfully submitted
l.log.Info("proposer tx successfully published", "tx_hash", receipt.TxHash) l.log.Info("proposer tx successfully published", "tx_hash", receipt.TxHash)
......
...@@ -3,15 +3,23 @@ package metrics ...@@ -3,15 +3,23 @@ package metrics
import ( import (
"math/big" "math/big"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
) )
func EmitBlockNumber(r *prometheus.Registry, ns string, blockNumber *big.Int) { type ProposerMetricsRegistry struct {
promauto.With(r).NewGauge(prometheus.GaugeOpts{ BlockNumberGauge prometheus.Gauge
}
func InitProposerMetricsRegistry(r *prometheus.Registry, ns string) *ProposerMetricsRegistry {
blockNumberGauge := promauto.With(r).NewGauge(prometheus.GaugeOpts{
Namespace: ns, Namespace: ns,
Name: "latest_block_number", Name: "latest_block_number",
Help: "Latest L2 proposed block number", Help: "Latest L2 proposed block number",
}).Set(float64(blockNumber.Uint64())) })
return &ProposerMetricsRegistry{BlockNumberGauge: blockNumberGauge}
}
func EmitBlockNumber(gauge prometheus.Gauge, ns string, blockNumber *big.Int) {
gauge.Set(float64(blockNumber.Uint64()))
} }
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