Skip to content

Commit

Permalink
add metrics for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Feb 16, 2024
1 parent c799f9b commit d14422c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
31 changes: 29 additions & 2 deletions tools/walletextension/cache/RistrettoCache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"github.com/ethereum/go-ethereum/log"
"time"

"github.com/dgraph-io/ristretto"
Expand All @@ -15,10 +16,11 @@ const (

type RistrettoCache struct {
cache *ristretto.Cache
quit chan struct{}
}

// NewRistrettoCache returns a new RistrettoCache.
func NewRistrettoCache() (*RistrettoCache, error) {
func NewRistrettoCache(logger log.Logger) (*RistrettoCache, error) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: numCounters,
MaxCost: maxCost,
Expand All @@ -28,7 +30,16 @@ func NewRistrettoCache() (*RistrettoCache, error) {
if err != nil {
return nil, err
}
return &RistrettoCache{cache}, nil

c := &RistrettoCache{
cache: cache,
quit: make(chan struct{}),
}

// Start the metrics logging
go c.startMetricsLogging(logger)

return c, nil
}

// Set adds the key and value to the cache.
Expand All @@ -52,3 +63,19 @@ func (c *RistrettoCache) Get(key string) (value map[string]interface{}, ok bool)

return value, true
}

// startMetricsLogging starts logging cache metrics every hour.
func (c *RistrettoCache) startMetricsLogging(logger log.Logger) {
ticker := time.NewTicker(1 * time.Hour)
for {
select {
case <-ticker.C:
metrics := c.cache.Metrics
logger.Info("Cache metrics: Hits: %d, Misses: %d, Cost Added: %d\n",
metrics.Hits(), metrics.Misses(), metrics.CostAdded())
case <-c.quit:
ticker.Stop()
return
}
}
}
5 changes: 3 additions & 2 deletions tools/walletextension/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/log"
"time"

"github.com/ten-protocol/go-ten/tools/walletextension/common"
Expand Down Expand Up @@ -39,8 +40,8 @@ type Cache interface {
Get(key string) (value map[string]interface{}, ok bool)
}

func NewCache() (Cache, error) {
return NewRistrettoCache() // TODO: Fix signatures..
func NewCache(logger log.Logger) (Cache, error) {
return NewRistrettoCache(logger)
}

// IsCacheable checks if the given RPC request is cacheable and returns the cache key and TTL
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func New(
}
newTenClient := obsclient.NewObsClient(rpcClient)
newFileLogger := common.NewFileLogger()
newGatewayCache, err := cache.NewCache()
newGatewayCache, err := cache.NewCache(logger)
if err != nil {
logger.Error(fmt.Errorf("could not create cache. Cause: %w", err).Error())
panic(err)
Expand Down

0 comments on commit d14422c

Please sign in to comment.