From b89e0786091a683049a62c7eac83588557934fe7 Mon Sep 17 00:00:00 2001 From: Kanishka Date: Mon, 9 Sep 2024 18:57:43 +0530 Subject: [PATCH] Add l1 metrics (#1678) Co-authored-by: Kirill --- l1/eth_subscriber.go | 17 +++++++++++++++-- l1/event_listener.go | 10 ++++++++++ node/metrics.go | 9 +++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/l1/eth_subscriber.go b/l1/eth_subscriber.go index 38e7d377db..6ddb213b1e 100644 --- a/l1/eth_subscriber.go +++ b/l1/eth_subscriber.go @@ -20,6 +20,7 @@ type EthSubscriber struct { ethClient *ethclient.Client client *rpc.Client filterer *contract.StarknetFilterer + listener EventListener } var _ Subscriber = (*EthSubscriber)(nil) @@ -42,6 +43,7 @@ func NewEthSubscriber(ethClientAddress string, coreContractAddress common.Addres ethClient: ethClient, client: client, filterer: filterer, + listener: SelectiveListener{}, }, nil } @@ -50,14 +52,25 @@ func (s *EthSubscriber) WatchLogStateUpdate(ctx context.Context, sink chan<- *co } func (s *EthSubscriber) ChainID(ctx context.Context) (*big.Int, error) { - return s.ethClient.ChainID(ctx) + reqTimer := time.Now() + chainID, err := s.ethClient.ChainID(ctx) + if err != nil { + return nil, fmt.Errorf("get chain ID: %w", err) + } + s.listener.OnL1Call("eth_chainId", time.Since(reqTimer)) + + return chainID, nil } func (s *EthSubscriber) FinalisedHeight(ctx context.Context) (uint64, error) { + const method = "eth_getBlockByNumber" + reqTimer := time.Now() + var raw json.RawMessage - if err := s.client.CallContext(ctx, &raw, "eth_getBlockByNumber", "finalized", false); err != nil { //nolint:misspell + if err := s.client.CallContext(ctx, &raw, method, "finalized", false); err != nil { //nolint:misspell return 0, fmt.Errorf("get finalised Ethereum block: %w", err) } + s.listener.OnL1Call(method, time.Since(reqTimer)) var head *types.Header if err := json.Unmarshal(raw, &head); err != nil { diff --git a/l1/event_listener.go b/l1/event_listener.go index f5e9a10a32..077fde4cb1 100644 --- a/l1/event_listener.go +++ b/l1/event_listener.go @@ -1,15 +1,19 @@ package l1 import ( + "time" + "github.com/NethermindEth/juno/core" ) type EventListener interface { OnNewL1Head(head *core.L1Head) + OnL1Call(method string, took time.Duration) } type SelectiveListener struct { OnNewL1HeadCb func(head *core.L1Head) + OnL1CallCb func(method string, took time.Duration) } func (l SelectiveListener) OnNewL1Head(head *core.L1Head) { @@ -17,3 +21,9 @@ func (l SelectiveListener) OnNewL1Head(head *core.L1Head) { l.OnNewL1HeadCb(head) } } + +func (l SelectiveListener) OnL1Call(method string, took time.Duration) { + if l.OnL1CallCb != nil { + l.OnL1CallCb(method, took) + } +} diff --git a/node/metrics.go b/node/metrics.go index a10ae75a3b..9ac489b5fe 100644 --- a/node/metrics.go +++ b/node/metrics.go @@ -226,11 +226,20 @@ func makeL1Metrics() l1.EventListener { Name: "height", }) prometheus.MustRegister(l1Height) + requestLatencies := prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "l1", + Subsystem: "client", + Name: "request_latency", + }, []string{"method"}) + prometheus.MustRegister(requestLatencies) return l1.SelectiveListener{ OnNewL1HeadCb: func(head *core.L1Head) { l1Height.Set(float64(head.BlockNumber)) }, + OnL1CallCb: func(method string, took time.Duration) { + requestLatencies.WithLabelValues(method).Observe(took.Seconds()) + }, } }