Skip to content

Commit

Permalink
Add l1 metrics (#1678)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill <[email protected]>
  • Loading branch information
kanishkatn and kirugan authored Sep 9, 2024
1 parent 8a96b78 commit b89e078
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
17 changes: 15 additions & 2 deletions l1/eth_subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type EthSubscriber struct {
ethClient *ethclient.Client
client *rpc.Client
filterer *contract.StarknetFilterer
listener EventListener
}

var _ Subscriber = (*EthSubscriber)(nil)
Expand All @@ -42,6 +43,7 @@ func NewEthSubscriber(ethClientAddress string, coreContractAddress common.Addres
ethClient: ethClient,
client: client,
filterer: filterer,
listener: SelectiveListener{},
}, nil
}

Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions l1/event_listener.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
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) {
if l.OnNewL1HeadCb != nil {
l.OnNewL1HeadCb(head)
}
}

func (l SelectiveListener) OnL1Call(method string, took time.Duration) {
if l.OnL1CallCb != nil {
l.OnL1CallCb(method, took)
}
}
9 changes: 9 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}
}

Expand Down

0 comments on commit b89e078

Please sign in to comment.