Skip to content

Commit

Permalink
feat: enable metrics collecting on eth rpc calls (#108)
Browse files Browse the repository at this point in the history
* enable metrics collecting on eth rpc calls

* add testing

* fix lint
  • Loading branch information
hunter-bera authored Jul 8, 2024
1 parent c7f8039 commit 221eb3c
Show file tree
Hide file tree
Showing 21 changed files with 1,555 additions and 47 deletions.
11 changes: 10 additions & 1 deletion baseapp/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ func (ab *AppBuilder) RegisterDB(db ethdb.KeyValueStore) {
func (ab *AppBuilder) RegisterMetrics(cfg *telemetry.Config) error {
var err error
ab.metrics, err = telemetry.NewMetrics(cfg)
return err
if err != nil {
return err
}

// Enable metrics on eth client only if it is an instance of ChainProviderImpl.
chainProvider, ok := ab.ethClient.(*eth.ChainProviderImpl)
if ok {
chainProvider.EnableMetrics(ab.metrics)
}
return nil
}

// RegisterHTTPServer registers the http server.
Expand Down
8 changes: 8 additions & 0 deletions client/eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)

//go:generate mockery --name Client
type Client interface {
DialContext(ctx context.Context, rawurl string) error
Close() error
Health() bool
ClientID() string
Reader
Writer
}
Expand Down Expand Up @@ -99,6 +101,7 @@ type Writer interface {
type ExtendedEthClient struct {
*ethclient.Client
rpcTimeout time.Duration
clientID string
}

func NewExtendedEthClient(c *ethclient.Client, rpcTimeout time.Duration) *ExtendedEthClient {
Expand All @@ -120,6 +123,7 @@ func (c *ExtendedEthClient) DialContext(ctx context.Context, rawurl string) erro
var err error
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.rpcTimeout)
c.Client, err = ethclient.DialContext(ctxWithTimeout, rawurl)
c.clientID = trimProtocolAndPort(rawurl)
cancel()
return err
}
Expand All @@ -140,6 +144,10 @@ func (c *ExtendedEthClient) Health() bool {
return err == nil
}

func (c *ExtendedEthClient) ClientID() string {
return c.clientID
}

// ==================================================================
// Client Usage Methods
// ==================================================================
Expand Down
Loading

0 comments on commit 221eb3c

Please sign in to comment.