Skip to content

Commit 19fbd0b

Browse files
authored
chore(v2): add example of setting store metrics (#22819)
1 parent 57b4d30 commit 19fbd0b

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

simapp/v2/app_di.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,19 @@ func NewSimApp[T transaction.Tx](
167167
return nil, fmt.Errorf("store builder did not return a db")
168168
}
169169

170+
/**** Store Metrics ****/
171+
/*
172+
// In order to set store metrics uncomment the below
173+
storeMetrics, err := metrics.NewMetrics([][]string{{"module", "store"}})
174+
if err != nil {
175+
return nil, err
176+
}
177+
app.store.SetMetrics(storeMetrics)
178+
*/
170179
/**** Module Options ****/
171180

172181
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
173182
app.RegisterUpgradeHandlers()
174-
175183
if err = app.LoadLatest(); err != nil {
176184
return nil, err
177185
}

store/v2/metrics/metrics.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ func NewMetrics(labels [][]string) (Metrics, error) {
4545
func (m Metrics) MeasureSince(start time.Time, keys ...string) {
4646
metrics.MeasureSinceWithLabels(keys, start.UTC(), m.Labels)
4747
}
48+
49+
// NoOpMetrics is a no-op implementation of the StoreMetrics interface
50+
type NoOpMetrics struct{}
51+
52+
// NewNoOpMetrics returns a new instance of the NoOpMetrics
53+
func NewNoOpMetrics() NoOpMetrics {
54+
return NoOpMetrics{}
55+
}
56+
57+
// MeasureSince is a no-op implementation of the StoreMetrics interface to avoid time.Now() calls
58+
func (m NoOpMetrics) MeasureSince(start time.Time, keys ...string) {}

store/v2/root/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"cosmossdk.io/store/v2/commitment/mem"
1313
"cosmossdk.io/store/v2/db"
1414
"cosmossdk.io/store/v2/internal"
15+
"cosmossdk.io/store/v2/metrics"
1516
"cosmossdk.io/store/v2/pruning"
1617
)
1718

@@ -127,5 +128,5 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
127128
}
128129

129130
pm := pruning.NewManager(sc, storeOpts.SCPruningOption)
130-
return New(opts.SCRawDB, opts.Logger, sc, pm, nil, nil)
131+
return New(opts.SCRawDB, opts.Logger, sc, pm, nil, metrics.NoOpMetrics{})
131132
}

0 commit comments

Comments
 (0)