From 29b59bb85e55853ded451e1b9213af9145bc7b8f Mon Sep 17 00:00:00 2001 From: Josh Klopfenstein Date: Tue, 12 Dec 2023 16:40:22 -0600 Subject: [PATCH] Add feeder metrics (#1543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ă–mer Faruk Irmak --- clients/feeder/event_listener.go | 17 +++++++++++++++++ clients/feeder/feeder.go | 9 +++++++++ clients/feeder/feeder_test.go | 15 +++++++++++++++ node/metrics.go | 17 +++++++++++++++++ node/node.go | 1 + 5 files changed, 59 insertions(+) create mode 100644 clients/feeder/event_listener.go diff --git a/clients/feeder/event_listener.go b/clients/feeder/event_listener.go new file mode 100644 index 0000000000..d5298ee0de --- /dev/null +++ b/clients/feeder/event_listener.go @@ -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) + } +} diff --git a/clients/feeder/feeder.go b/clients/feeder/feeder.go index 58033c13e9..8bfb297745 100644 --- a/clients/feeder/feeder.go +++ b/clients/feeder/feeder.go @@ -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 { @@ -185,6 +191,7 @@ func NewClient(clientURL string) *Client { maxWait: 4 * time.Second, minWait: time.Second, log: utils.NewNopZapLogger(), + listener: &SelectiveListener{}, } } @@ -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 { diff --git a/clients/feeder/feeder_test.go b/clients/feeder/feeder_test.go index cc88032e1d..087fde339d 100644 --- a/clients/feeder/feeder_test.go +++ b/clients/feeder/feeder_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "strconv" "testing" + "time" "github.com/NethermindEth/juno/clients/feeder" "github.com/NethermindEth/juno/core/felt" @@ -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) +} diff --git a/node/metrics.go b/node/metrics.go index 6afdf2bdb8..bbd99ca243 100644 --- a/node/metrics.go +++ b/node/metrics.go @@ -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" @@ -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()) + }, + } +} diff --git a/node/node.go b/node/node.go index 1a22e8acbc..3835dbbd5d 100644 --- a/node/node.go +++ b/node/node.go @@ -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 {