Skip to content

Commit

Permalink
Add feeder metrics (#1543)
Browse files Browse the repository at this point in the history
Co-authored-by: Ömer Faruk Irmak <[email protected]>
  • Loading branch information
joshklop and omerfirmak authored Dec 12, 2023
1 parent 9154861 commit 29b59bb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
17 changes: 17 additions & 0 deletions clients/feeder/event_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package feeder

import "time"

type EventListener interface {
OnResponse(urlPath string, status int, took time.Duration)
}

type SelectiveListener struct {
OnResponseCb func(urlPath string, status int, took time.Duration)
}

func (l *SelectiveListener) OnResponse(urlPath string, status int, took time.Duration) {
if l.OnResponseCb != nil {
l.OnResponseCb(urlPath, status, took)
}
}
9 changes: 9 additions & 0 deletions clients/feeder/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type Client struct {
minWait time.Duration
log utils.SimpleLogger
userAgent string
listener EventListener
}

func (c *Client) WithListener(l EventListener) *Client {
c.listener = l
return c
}

func (c *Client) WithBackoff(b Backoff) *Client {
Expand Down Expand Up @@ -185,6 +191,7 @@ func NewClient(clientURL string) *Client {
maxWait: 4 * time.Second,
minWait: time.Second,
log: utils.NewNopZapLogger(),
listener: &SelectiveListener{},
}
}

Expand Down Expand Up @@ -225,8 +232,10 @@ func (c *Client) get(ctx context.Context, queryURL string) (io.ReadCloser, error
req.Header.Set("User-Agent", c.userAgent)
}

reqTimer := time.Now()
res, err = c.client.Do(req)
if err == nil {
c.listener.OnResponse(req.URL.Path, res.StatusCode, time.Since(reqTimer))
if res.StatusCode == http.StatusOK {
return res.Body, nil
} else {
Expand Down
15 changes: 15 additions & 0 deletions clients/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"strconv"
"testing"
"time"

"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -624,3 +625,17 @@ func TestBlockTrace(t *testing.T) {
require.Len(t, trace.Traces, 2)
})
}

func TestEventListener(t *testing.T) {
isCalled := false
client := feeder.NewTestClient(t, utils.Integration).WithListener(&feeder.SelectiveListener{
OnResponseCb: func(urlPath string, status int, _ time.Duration) {
isCalled = true
require.Equal(t, 200, status)
require.Equal(t, "/get_block", urlPath)
},
})
_, err := client.Block(context.Background(), "0")
require.NoError(t, err)
require.True(t, isCalled)
}
17 changes: 17 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package node

import (
"math"
"strconv"
"time"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/jsonrpc"
Expand Down Expand Up @@ -228,3 +230,18 @@ func makeL1Metrics() l1.EventListener {
},
}
}

func makeFeederMetrics() feeder.EventListener {
requestLatencies := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "feeder",
Subsystem: "client",
Name: "request_latency",
}, []string{"method", "status"})
prometheus.MustRegister(requestLatencies)
return &feeder.SelectiveListener{
OnResponseCb: func(urlPath string, status int, took time.Duration) {
statusString := strconv.FormatInt(int64(status), 10)
requestLatencies.WithLabelValues(urlPath, statusString).Observe(took.Seconds())
},
}
}
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
jsonrpcServer.WithListener(rpcMetrics)
jsonrpcServerLegacy.WithListener(legacyRPCMetrics)
synchronizer.WithListener(makeSyncMetrics(synchronizer, chain))
client.WithListener(makeFeederMetrics())
services = append(services, makeMetrics(cfg.MetricsHost, cfg.MetricsPort))
}
if cfg.GRPC {
Expand Down

0 comments on commit 29b59bb

Please sign in to comment.