Skip to content

Commit

Permalink
chore(v2): add example of setting store metrics (#22819)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Dec 10, 2024
1 parent 57b4d30 commit 19fbd0b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions store/v2/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
3 changes: 2 additions & 1 deletion store/v2/root/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{})
}

0 comments on commit 19fbd0b

Please sign in to comment.