chore(v2): add example of setting store metrics (#22819)

This commit is contained in:
Marko 2024-12-10 13:07:52 -08:00 committed by GitHub
parent 57b4d3003b
commit 19fbd0b855
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 2 deletions

View File

@ -167,11 +167,19 @@ func NewSimApp[T transaction.Tx](
return nil, fmt.Errorf("store builder did not return a db")
}
/**** Store Metrics ****/
/*
// In order to set store metrics uncomment the below
storeMetrics, err := metrics.NewMetrics([][]string{{"module", "store"}})
if err != nil {
return nil, err
}
app.store.SetMetrics(storeMetrics)
*/
/**** Module Options ****/
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
app.RegisterUpgradeHandlers()
if err = app.LoadLatest(); err != nil {
return nil, err
}

View File

@ -45,3 +45,14 @@ func NewMetrics(labels [][]string) (Metrics, error) {
func (m Metrics) MeasureSince(start time.Time, keys ...string) {
metrics.MeasureSinceWithLabels(keys, start.UTC(), m.Labels)
}
// NoOpMetrics is a no-op implementation of the StoreMetrics interface
type NoOpMetrics struct{}
// NewNoOpMetrics returns a new instance of the NoOpMetrics
func NewNoOpMetrics() NoOpMetrics {
return NoOpMetrics{}
}
// MeasureSince is a no-op implementation of the StoreMetrics interface to avoid time.Now() calls
func (m NoOpMetrics) MeasureSince(start time.Time, keys ...string) {}

View File

@ -12,6 +12,7 @@ import (
"cosmossdk.io/store/v2/commitment/mem"
"cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/internal"
"cosmossdk.io/store/v2/metrics"
"cosmossdk.io/store/v2/pruning"
)
@ -127,5 +128,5 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
}
pm := pruning.NewManager(sc, storeOpts.SCPruningOption)
return New(opts.SCRawDB, opts.Logger, sc, pm, nil, nil)
return New(opts.SCRawDB, opts.Logger, sc, pm, nil, metrics.NoOpMetrics{})
}