From 83aa427f7460de32ffd29b350821f75a1c2016b6 Mon Sep 17 00:00:00 2001 From: egonspace Date: Tue, 11 May 2021 19:22:50 +0900 Subject: [PATCH] feat: add prometheus metrics for caches reverting telemetry metrics --- baseapp/options.go | 13 +++++- baseapp/options_test.go | 22 ++++++++++ server/config/config.go | 4 ++ server/config/toml.go | 4 ++ server/start.go | 2 + simapp/sim_test.go | 3 +- simapp/simd/cmd/root.go | 20 +++++---- store/cache/cache.go | 30 +++++++------ store/cache/cache_test.go | 6 +-- store/cache/metrics.go | 84 +++++++++++++++++++++++++++++++++++++ store/cache/metrics_test.go | 16 +++++++ store/iavl/metrics.go | 84 +++++++++++++++++++++++++++++++++++++ store/iavl/metrics_test.go | 16 +++++++ store/iavl/store.go | 32 +++++++++----- store/store.go | 4 +- 15 files changed, 300 insertions(+), 40 deletions(-) create mode 100644 baseapp/options_test.go create mode 100644 store/cache/metrics.go create mode 100644 store/cache/metrics_test.go create mode 100644 store/iavl/metrics.go create mode 100644 store/iavl/metrics_test.go diff --git a/baseapp/options.go b/baseapp/options.go index d732a2992c..44c30b261f 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -4,6 +4,7 @@ import ( "fmt" "io" + "github.com/line/lbm-sdk/v2/store/cache" "github.com/line/lbm-sdk/v2/store/iavl" tmdb "github.com/line/tm-db/v2" @@ -65,12 +66,12 @@ func SetInterBlockCache(cache sdk.MultiStorePersistentCache) func(*BaseApp) { } // SetIAVLCacheManager provides a BaseApp option function that sets the iavl CacheManager -func SetIAVLCacheManager(size int) func(*BaseApp) { +func SetIAVLCacheManager(size int, provider iavl.MetricsProvider) func(*BaseApp) { return func(app *BaseApp) { if size == 0 { app.cms.SetIAVLCacheManager(iavl.NewCacheManagerNoCache()) } else { - app.cms.SetIAVLCacheManager(iavl.NewCacheManagerSingleton(size)) + app.cms.SetIAVLCacheManager(iavl.NewCacheManagerSingleton(size, provider)) } } } @@ -245,3 +246,11 @@ func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) { app.grpcQueryRouter.SetInterfaceRegistry(registry) app.msgServiceRouter.SetInterfaceRegistry(registry) } + +func MetricsProvider(prometheus bool) (cache.MetricsProvider, iavl.MetricsProvider) { + namespace := "app" + if prometheus { + return cache.PrometheusMetricsProvider(namespace), iavl.PrometheusMetricsProvider(namespace) + } + return cache.NopMetricsProvider(), iavl.NopMetricsProvider() +} diff --git a/baseapp/options_test.go b/baseapp/options_test.go new file mode 100644 index 0000000000..ffb5bb64cb --- /dev/null +++ b/baseapp/options_test.go @@ -0,0 +1,22 @@ +package baseapp + +import ( + "testing" + + "github.com/go-kit/kit/metrics/discard" + "github.com/stretchr/testify/require" +) + +func TestMetricsProvider(t *testing.T) { + p1, p2 := MetricsProvider(true) + c1 := p1() + c2 := p2() + require.NotEqual(t, c1.InterBlockCacheHits, discard.NewCounter()) + require.NotEqual(t, c2.IAVLCacheHits, discard.NewGauge()) + + p1, p2 = MetricsProvider(false) + c1 = p1() + c2 = p2() + require.Equal(t, c1.InterBlockCacheHits, discard.NewCounter()) + require.Equal(t, c2.IAVLCacheHits, discard.NewGauge()) +} diff --git a/server/config/config.go b/server/config/config.go index c99d730aef..94472ef424 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -73,6 +73,10 @@ type BaseConfig struct { // IAVL cache size; bytes size unit IAVLCacheSize int `mapstructure:"iavl-cache-size"` + + // When true, Prometheus metrics are served under /metrics on prometheus_listen_addr in config.toml. + // It works when tendermint's prometheus option (config.toml) is set to true. + Prometheus bool `mapstructure:"prometheus"` } // APIConfig defines the API listener configuration. diff --git a/server/config/toml.go b/server/config/toml.go index 14a18df118..d1fa97e0a7 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -76,6 +76,10 @@ iavl-cache-size = {{ .BaseConfig.IAVLCacheSize }} # ["message.sender", "message.recipient"] index-events = {{ .BaseConfig.IndexEvents }} +# When true, Prometheus metrics are served under /metrics on prometheus_listen_addr in config.toml. +# It works when tendermint's prometheus option (config.toml) is set to true. +prometheus = {{ .BaseConfig.Prometheus }} + ############################################################################### ### Telemetry Configuration ### ############################################################################### diff --git a/server/start.go b/server/start.go index f4f7262b94..45826b313d 100644 --- a/server/start.go +++ b/server/start.go @@ -160,6 +160,8 @@ which accepts a path for the resulting pprof file. cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval") cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep") + cmd.Flags().Bool(FlagPrometheus, false, "Enable prometheus metric for app") + // add support for all Ostracon-specific command line options ostcmd.AddNodeFlags(cmd) return cmd diff --git a/simapp/sim_test.go b/simapp/sim_test.go index 963aa66c57..b3eb2ec42f 100644 --- a/simapp/sim_test.go +++ b/simapp/sim_test.go @@ -54,7 +54,8 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) { // interBlockCacheOpt returns a BaseApp option function that sets the persistent // inter-block write-through cache. func interBlockCacheOpt() func(*baseapp.BaseApp) { - return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize)) + return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager( + cache.DefaultCommitKVStoreCacheSize, cache.NopMetricsProvider())) } func TestFullAppSimulation(t *testing.T) { diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index afa8837714..b763b9a2d1 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -6,6 +6,13 @@ import ( "os" "path/filepath" + ostcli "github.com/line/ostracon/libs/cli" + "github.com/line/ostracon/libs/log" + tmdb "github.com/line/tm-db/v2" + "github.com/spf13/cast" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/line/lbm-sdk/v2/baseapp" "github.com/line/lbm-sdk/v2/client" "github.com/line/lbm-sdk/v2/client/debug" @@ -26,11 +33,6 @@ import ( banktypes "github.com/line/lbm-sdk/v2/x/bank/types" "github.com/line/lbm-sdk/v2/x/crisis" genutilcli "github.com/line/lbm-sdk/v2/x/genutil/client/cli" - ostcli "github.com/line/ostracon/libs/cli" - "github.com/line/ostracon/libs/log" - tmdb "github.com/line/tm-db/v2" - "github.com/spf13/cast" - "github.com/spf13/cobra" ) // NewRootCmd creates a new root command for simd. It is called once in the @@ -156,9 +158,11 @@ type appCreator struct { func (a appCreator) newApp(logger log.Logger, db tmdb.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application { var cache sdk.MultiStorePersistentCache + ibCacheMetricsProvider, iavlCacheMetricsProvider := + baseapp.MetricsProvider(cast.ToBool(viper.GetBool(server.FlagPrometheus))) if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { cache = store.NewCommitKVStoreCacheManager( - cast.ToInt(appOpts.Get(server.FlagInterBlockCacheSize))) + cast.ToInt(appOpts.Get(server.FlagInterBlockCacheSize)), ibCacheMetricsProvider) } skipUpgradeHeights := make(map[int64]bool) @@ -192,10 +196,10 @@ func (a appCreator) newApp(logger log.Logger, db tmdb.DB, traceStore io.Writer, baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))), baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))), baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))), - baseapp.SetIAVLCacheManager(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), baseapp.SetInterBlockCache(cache), - baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))), + baseapp.SetIAVLCacheManager(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize)), iavlCacheMetricsProvider), baseapp.SetTrace(cast.ToBool(appOpts.Get(server.FlagTrace))), + baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(server.FlagIndexEvents))), baseapp.SetSnapshotStore(snapshotStore), baseapp.SetSnapshotInterval(cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval))), baseapp.SetSnapshotKeepRecent(cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent))), diff --git a/store/cache/cache.go b/store/cache/cache.go index 1a99df070d..e3e958756c 100644 --- a/store/cache/cache.go +++ b/store/cache/cache.go @@ -6,7 +6,6 @@ import ( "github.com/VictoriaMetrics/fastcache" "github.com/line/lbm-sdk/v2/store/cachekv" "github.com/line/lbm-sdk/v2/store/types" - "github.com/line/lbm-sdk/v2/telemetry" ) const ( @@ -27,8 +26,9 @@ type ( // CommitKVStore and below is completely irrelevant to this layer. CommitKVStoreCache struct { types.CommitKVStore - cache *fastcache.Cache - prefix []byte + cache *fastcache.Cache + prefix []byte + metrics *Metrics } // CommitKVStoreCacheManager maintains a mapping from a StoreKey to a @@ -36,9 +36,10 @@ type ( // in an inter-block (persistent) manner and typically provided by a // CommitMultiStore. CommitKVStoreCacheManager struct { - mutex sync.Mutex - cache *fastcache.Cache - caches map[string]types.CommitKVStore + mutex sync.Mutex + cache *fastcache.Cache + caches map[string]types.CommitKVStore + metrics *Metrics // All cache stores use the unique prefix that has one byte length // Contract: The number of all cache stores cannot exceed 127(max byte) @@ -47,15 +48,17 @@ type ( } ) -func NewCommitKVStoreCache(store types.CommitKVStore, prefix []byte, cache *fastcache.Cache) *CommitKVStoreCache { +func NewCommitKVStoreCache(store types.CommitKVStore, prefix []byte, cache *fastcache.Cache, + metrics *Metrics) *CommitKVStoreCache { return &CommitKVStoreCache{ CommitKVStore: store, prefix: prefix, cache: cache, + metrics: metrics, } } -func NewCommitKVStoreCacheManager(cacheSize int) *CommitKVStoreCacheManager { +func NewCommitKVStoreCacheManager(cacheSize int, provider MetricsProvider) *CommitKVStoreCacheManager { if cacheSize <= 0 { // This function was called because it intended to use the inter block cache, creating a cache of minimal size. cacheSize = DefaultCommitKVStoreCacheSize @@ -63,6 +66,7 @@ func NewCommitKVStoreCacheManager(cacheSize int) *CommitKVStoreCacheManager { return &CommitKVStoreCacheManager{ cache: fastcache.New(cacheSize), caches: make(map[string]types.CommitKVStore), + metrics: provider(), prefixMap: make(map[string][]byte), prefixOrder: 0, } @@ -81,7 +85,7 @@ func (cmgr *CommitKVStoreCacheManager) GetStoreCache(key types.StoreKey, store t if cmgr.prefixOrder <= 0 { panic("The number of cache stores exceed the maximum(127)") } - cmgr.caches[key.Name()] = NewCommitKVStoreCache(store, cmgr.prefixMap[key.Name()], cmgr.cache) + cmgr.caches[key.Name()] = NewCommitKVStoreCache(store, cmgr.prefixMap[key.Name()], cmgr.cache, cmgr.metrics) } cmgr.mutex.Unlock() } @@ -123,18 +127,18 @@ func (ckv *CommitKVStoreCache) Get(key []byte) []byte { valueI := ckv.cache.Get(nil, prefixedKey) if valueI != nil { // cache hit - telemetry.IncrCounter(1, "store", "inter-block-cache", "hits") + ckv.metrics.InterBlockCacheHits.Add(1) return valueI } // cache miss; write to cache - telemetry.IncrCounter(1, "store", "inter-block-cache", "misses") + ckv.metrics.InterBlockCacheMisses.Add(1) value := ckv.CommitKVStore.Get(key) ckv.cache.Set(prefixedKey, value) stats := fastcache.Stats{} ckv.cache.UpdateStats(&stats) - telemetry.SetGauge(float32(stats.EntriesCount), "store", "inter-block-cache", "entries") - telemetry.SetGauge(float32(stats.BytesSize), "store", "inter-block-cache", "bytes") + ckv.metrics.InterBlockCacheEntries.Set(float64(stats.EntriesCount)) + ckv.metrics.InterBlockCacheBytes.Set(float64(stats.BytesSize)) return value } diff --git a/store/cache/cache_test.go b/store/cache/cache_test.go index 39c9dccd3d..e5c0108727 100644 --- a/store/cache/cache_test.go +++ b/store/cache/cache_test.go @@ -16,7 +16,7 @@ import ( func TestGetOrSetStoreCache(t *testing.T) { db := memdb.NewDB() - mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize) + mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize, cache.NopMetricsProvider()) sKey := types.NewKVStoreKey("test") tree, err := iavl.NewMutableTree(db, 100) @@ -30,7 +30,7 @@ func TestGetOrSetStoreCache(t *testing.T) { func TestUnwrap(t *testing.T) { db := memdb.NewDB() - mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize) + mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize, cache.NopMetricsProvider()) sKey := types.NewKVStoreKey("test") tree, err := iavl.NewMutableTree(db, 100) @@ -44,7 +44,7 @@ func TestUnwrap(t *testing.T) { func TestStoreCache(t *testing.T) { db := memdb.NewDB() - mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize) + mngr := cache.NewCommitKVStoreCacheManager(cache.DefaultCommitKVStoreCacheSize, cache.NopMetricsProvider()) sKey := types.NewKVStoreKey("test") tree, err := iavl.NewMutableTree(db, 100) diff --git a/store/cache/metrics.go b/store/cache/metrics.go new file mode 100644 index 0000000000..a857e35abd --- /dev/null +++ b/store/cache/metrics.go @@ -0,0 +1,84 @@ +package cache + +import ( + "github.com/go-kit/kit/metrics" + "github.com/go-kit/kit/metrics/discard" + "github.com/go-kit/kit/metrics/prometheus" + stdprometheus "github.com/prometheus/client_golang/prometheus" +) + +const ( + // MetricsSubsystem is a subsystem shared by all metrics exposed by this + // package. + MetricsSubsystem = "inter_block_cache" +) + +// Metrics contains metrics exposed by this package. +type Metrics struct { + InterBlockCacheHits metrics.Counter + InterBlockCacheMisses metrics.Counter + InterBlockCacheEntries metrics.Gauge + InterBlockCacheBytes metrics.Gauge +} + +// PrometheusMetrics returns Metrics build using Prometheus client library. +// Optionally, labels can be provided along with their values ("foo", +// "fooValue"). +func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { + labels := []string{} + for i := 0; i < len(labelsAndValues); i += 2 { + labels = append(labels, labelsAndValues[i]) + } + return &Metrics{ + InterBlockCacheHits: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "hits", + Help: "Cache hits of the inter block cache", + }, labels).With(labelsAndValues...), + InterBlockCacheMisses: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "misses", + Help: "Cache misses of the inter block cache", + }, labels).With(labelsAndValues...), + InterBlockCacheEntries: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "entries", + Help: "Cache entry count of the inter block cache", + }, labels).With(labelsAndValues...), + InterBlockCacheBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "bytes_size", + Help: "Cache bytes size of the inter block cache", + }, labels).With(labelsAndValues...), + } +} + +// NopMetrics returns no-op Metrics. +func NopMetrics() *Metrics { + return &Metrics{ + InterBlockCacheHits: discard.NewCounter(), + InterBlockCacheMisses: discard.NewCounter(), + InterBlockCacheEntries: discard.NewGauge(), + InterBlockCacheBytes: discard.NewGauge(), + } +} + +type MetricsProvider func() *Metrics + +// PrometheusMetricsProvider returns PrometheusMetrics for each store +func PrometheusMetricsProvider(namespace string, labelsAndValues ...string) func() *Metrics { + return func() *Metrics { + return PrometheusMetrics(namespace, labelsAndValues...) + } +} + +// NopMetricsProvider returns NopMetrics for each store +func NopMetricsProvider() func() *Metrics { + return func() *Metrics { + return NopMetrics() + } +} diff --git a/store/cache/metrics_test.go b/store/cache/metrics_test.go new file mode 100644 index 0000000000..07ad0f3c04 --- /dev/null +++ b/store/cache/metrics_test.go @@ -0,0 +1,16 @@ +package cache + +import ( + "testing" + + "github.com/go-kit/kit/metrics/discard" + "github.com/stretchr/testify/require" +) + +func TestPrometheusMetrics(t *testing.T) { + metrics := PrometheusMetrics("test") + require.NotEqual(t, metrics.InterBlockCacheHits, discard.NewCounter()) + require.NotEqual(t, metrics.InterBlockCacheMisses, discard.NewCounter()) + require.NotEqual(t, metrics.InterBlockCacheEntries, discard.NewGauge()) + require.NotEqual(t, metrics.InterBlockCacheBytes, discard.NewGauge()) +} diff --git a/store/iavl/metrics.go b/store/iavl/metrics.go new file mode 100644 index 0000000000..d594e0f443 --- /dev/null +++ b/store/iavl/metrics.go @@ -0,0 +1,84 @@ +package iavl + +import ( + "github.com/go-kit/kit/metrics" + "github.com/go-kit/kit/metrics/discard" + "github.com/go-kit/kit/metrics/prometheus" + stdprometheus "github.com/prometheus/client_golang/prometheus" +) + +const ( + // MetricsSubsystem is a subsystem shared by all metrics exposed by this + // package. + MetricsSubsystem = "iavl_cache" +) + +// Metrics contains metrics exposed by this package. +type Metrics struct { + IAVLCacheHits metrics.Gauge + IAVLCacheMisses metrics.Gauge + IAVLCacheEntries metrics.Gauge + IAVLCacheBytes metrics.Gauge +} + +// PrometheusMetrics returns Metrics build using Prometheus client library. +// Optionally, labels can be provided along with their values ("foo", +// "fooValue"). +func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { + labels := []string{} + for i := 0; i < len(labelsAndValues); i += 2 { + labels = append(labels, labelsAndValues[i]) + } + return &Metrics{ + IAVLCacheHits: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "hits", + Help: "Cache hit count of the iavl cache", + }, labels).With(labelsAndValues...), + IAVLCacheMisses: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "misses", + Help: "Cache miss count of the iavl cache", + }, labels).With(labelsAndValues...), + IAVLCacheEntries: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "entries", + Help: "Cache entry count of the iavl cache", + }, labels).With(labelsAndValues...), + IAVLCacheBytes: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "bytes_size", + Help: "Cache bytes size of the iavl cache", + }, labels).With(labelsAndValues...), + } +} + +// NopMetrics returns no-op Metrics. +func NopMetrics() *Metrics { + return &Metrics{ + IAVLCacheHits: discard.NewGauge(), + IAVLCacheMisses: discard.NewGauge(), + IAVLCacheEntries: discard.NewGauge(), + IAVLCacheBytes: discard.NewGauge(), + } +} + +type MetricsProvider func() *Metrics + +// PrometheusMetricsProvider returns PrometheusMetrics for each store +func PrometheusMetricsProvider(namespace string, labelsAndValues ...string) func() *Metrics { + return func() *Metrics { + return PrometheusMetrics(namespace, labelsAndValues...) + } +} + +// NopMetricsProvider returns NopMetrics for each store +func NopMetricsProvider() func() *Metrics { + return func() *Metrics { + return NopMetrics() + } +} diff --git a/store/iavl/metrics_test.go b/store/iavl/metrics_test.go new file mode 100644 index 0000000000..7f273ae09d --- /dev/null +++ b/store/iavl/metrics_test.go @@ -0,0 +1,16 @@ +package iavl + +import ( + "testing" + + "github.com/go-kit/kit/metrics/discard" + "github.com/stretchr/testify/require" +) + +func TestPrometheusMetrics(t *testing.T) { + metrics := PrometheusMetrics("test") + require.NotEqual(t, metrics.IAVLCacheHits, discard.NewGauge()) + require.NotEqual(t, metrics.IAVLCacheMisses, discard.NewGauge()) + require.NotEqual(t, metrics.IAVLCacheEntries, discard.NewGauge()) + require.NotEqual(t, metrics.IAVLCacheBytes, discard.NewGauge()) +} diff --git a/store/iavl/store.go b/store/iavl/store.go index 11349e51f4..b95c26655d 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -40,21 +40,24 @@ var ( // Store Implements types.KVStore and CommitKVStore. type Store struct { - tree Tree - cache *fastcache.Cache + tree Tree + cache *fastcache.Cache + metrics *Metrics } type CacheManagerSingleton struct { - cache *fastcache.Cache + cache *fastcache.Cache + metrics *Metrics } func (cms *CacheManagerSingleton) GetCache() *fastcache.Cache { return cms.cache } -func NewCacheManagerSingleton(cacheSize int) types.CacheManager { +func NewCacheManagerSingleton(cacheSize int, provider MetricsProvider) types.CacheManager { return &CacheManagerSingleton{ - cache: fastcache.New(cacheSize), + cache: fastcache.New(cacheSize), + metrics: provider(), } } @@ -115,9 +118,16 @@ func LoadStoreWithInitialVersion(db tmdb.DB, cacheManager types.CacheManager, id return nil, err } + var metrics *Metrics + if cms, ok := cacheManager.(*CacheManagerSingleton); ok { + metrics = cms.metrics + } else { + metrics = NopMetrics() + } return &Store{ - tree: tree, - cache: cache, + tree: tree, + cache: cache, + metrics: metrics, }, nil } @@ -223,10 +233,10 @@ func (st *Store) Get(key []byte) []byte { if st.cache != nil { stats := fastcache.Stats{} st.cache.UpdateStats(&stats) - telemetry.SetGauge(float32(stats.GetCalls-stats.Misses), "store", "iavl-cache", "hits") - telemetry.SetGauge(float32(stats.Misses), "store", "iavl-cache", "missess") - telemetry.SetGauge(float32(stats.EntriesCount), "store", "iavl-cache", "entries") - telemetry.SetGauge(float32(stats.BytesSize), "store", "iavl-cache", "bytes") + st.metrics.IAVLCacheHits.Set(float64(stats.GetCalls - stats.Misses)) + st.metrics.IAVLCacheMisses.Set(float64(stats.Misses)) + st.metrics.IAVLCacheEntries.Set(float64(stats.EntriesCount)) + st.metrics.IAVLCacheBytes.Set(float64(stats.BytesSize)) } return value } diff --git a/store/store.go b/store/store.go index f055ca0674..0f621e9822 100644 --- a/store/store.go +++ b/store/store.go @@ -12,6 +12,6 @@ func NewCommitMultiStore(db tmdb.DB) types.CommitMultiStore { return rootmulti.NewStore(db) } -func NewCommitKVStoreCacheManager(cacheSize int) types.MultiStorePersistentCache { - return cache.NewCommitKVStoreCacheManager(cacheSize) +func NewCommitKVStoreCacheManager(cacheSize int, metricsProvider cache.MetricsProvider) types.MultiStorePersistentCache { + return cache.NewCommitKVStoreCacheManager(cacheSize, metricsProvider) }