Skip to content

Commit

Permalink
Add gateway client metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored and omerfirmak committed Dec 29, 2023
1 parent bd82092 commit 14cbd4d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
17 changes: 17 additions & 0 deletions clients/gateway/event_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gateway

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)
}
}
17 changes: 15 additions & 2 deletions clients/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
type Client struct {
url string
client *http.Client
listener EventListener
log utils.SimpleLogger
userAgent string
apiKey string
Expand All @@ -53,6 +54,11 @@ func (c *Client) WithAPIKey(key string) *Client {
return c
}

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

// NewTestClient returns a client and a function to close a test server.
func NewTestClient(t *testing.T) *Client {
srv := newTestServer(t)
Expand Down Expand Up @@ -100,7 +106,8 @@ func NewClient(gatewayURL string, log utils.SimpleLogger) *Client {
client: &http.Client{
Timeout: time.Minute,
},
log: log,
listener: &SelectiveListener{},
log: log,
}
}

Expand Down Expand Up @@ -153,7 +160,13 @@ func (c *Client) doPost(ctx context.Context, url string, data any) (*http.Respon
if c.apiKey != "" {
req.Header.Set("X-Throttling-Bypass", c.apiKey)
}
return c.client.Do(req)
reqTimer := time.Now()
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
c.listener.OnResponse(req.URL.Path, resp.StatusCode, time.Since(reqTimer))
return resp, nil
}

type ErrorCode string
Expand Down
16 changes: 16 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/clients/gateway"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/jsonrpc"
Expand Down Expand Up @@ -245,3 +246,18 @@ func makeFeederMetrics() feeder.EventListener {
},
}
}

func makeGatewayMetrics() gateway.EventListener {
requestLatencies := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "gateway",
Subsystem: "client",
Name: "request_latency",
}, []string{"method", "status"})
prometheus.MustRegister(requestLatencies)
return &gateway.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 @@ -181,6 +181,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
jsonrpcServerLegacy.WithListener(legacyRPCMetrics)
synchronizer.WithListener(makeSyncMetrics(synchronizer, chain))
client.WithListener(makeFeederMetrics())
gatewayClient.WithListener(makeGatewayMetrics())
services = append(services, makeMetrics(cfg.MetricsHost, cfg.MetricsPort))
}
if cfg.GRPC {
Expand Down

0 comments on commit 14cbd4d

Please sign in to comment.