Skip to content

Commit 09ccd43

Browse files
fixup(golang/.../vstorage/keeper): report absolute values for metrics increase/decrease (#11101)
closes: #11062 ## Description We need to report absolute values for vstorage metrics increase/decrease as requested in #11063 (review) ### Security Considerations N/A ### Scaling Considerations N/A ### Documentation Considerations `store_size_increase` and `store_size_decrease` metrics represent total writes and deletes *issued* respectively, which may differ from the total number of bytes committed/freed to/from the store due to the store's internal implementation. ### Testing Considerations `vstorage metrics` test passed: ``` 2025-03-11T16:06:25.744Z shutdown: Shutting down cleanly... 2025-03-11T16:06:25.745Z shutdown: Process terminated! make: *** [Makefile:155: scenario2-run-chain] Error 98 ✔ vstorage metrics (1m 14.8s) ℹ query vstorage path published.wallet.agoric1cuh05y5l2uhphv2995k5qhdg5crvs396ud7rv9 exits successfully ℹ metric start values: { 'store_size_decrease{storeKey="vstorage"}': 1106, 'store_size_increase{storeKey="vstorage"}': 1145, } ℹ query vstorage path published.wallet.agoric1cuh05y5l2uhphv2995k5qhdg5crvs396ud7rv9 exits successfully ℹ provisioned wallet agoric1cuh05y5l2uhphv2995k5qhdg5crvs396ud7rv9 is published ℹ metric stop values: { 'store_size_decrease{storeKey="vstorage"}': 2180, 'store_size_increase{storeKey="vstorage"}': 3585, } ─ 1 test passed Done in 654.27s. ``` ### Upgrade Considerations N/A
1 parent db4dcf4 commit 09ccd43

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

golang/cosmos/x/vstorage/keeper/keeper.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,24 @@ func NewKeeper(storeKey storetypes.StoreKey) Keeper {
119119
}
120120
}
121121

122+
// size_increase and size_decrease metrics represent total writes and deletes *issued*
123+
// respectively, which may differ from the total number of bytes committed/freed
124+
// to/from the store due to the store's internal implementation.
122125
var MetricKeyStoreSizeIncrease = []string{"store", "size_increase"}
123-
var MetricKeyStoreSizeDecrese = []string{"store", "size_decrease"}
126+
var MetricKeyStoreSizeDecrease = []string{"store", "size_decrease"}
124127
const MetricLabelStoreKey = "storeKey"
125128

126-
func ReportStoreSizeMetrics(k Keeper, sizeDelta float32) {
129+
// reportStoreSizeMetrics exports store size increase/decrease metrics
130+
// when Cosmos telemetry is enabled.
131+
func (k Keeper) reportStoreSizeMetrics(increase int, decrease int) {
127132
metricsLabel := []metrics.Label{
128133
telemetry.NewLabel(MetricLabelStoreKey, k.storeKey.Name()),
129134
}
130-
if sizeDelta >= 0 {
131-
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeIncrease, sizeDelta, metricsLabel)
132-
} else {
133-
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeDecrese, -sizeDelta, metricsLabel)
135+
if increase > 0 {
136+
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeIncrease, float32(increase), metricsLabel)
137+
}
138+
if decrease > 0 {
139+
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeDecrease, float32(decrease), metricsLabel)
134140
}
135141
}
136142

@@ -233,8 +239,7 @@ func (k Keeper) RemoveEntriesWithPrefix(ctx sdk.Context, pathPrefix string) {
233239

234240
for _, key := range keys {
235241
rawValue := store.Get(key)
236-
sizeDelta := float32(-len(key) - len(rawValue))
237-
ReportStoreSizeMetrics(k, sizeDelta)
242+
k.reportStoreSizeMetrics(0, len(key) + len(rawValue))
238243
store.Delete(key)
239244
}
240245

@@ -391,20 +396,17 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
391396
if !entry.HasValue() {
392397
if !k.HasChildren(ctx, path) {
393398
// We have no children, can delete.
394-
sizeDelta := float32(-len(encodedKey) - len(oldRawValue))
395-
ReportStoreSizeMetrics(k, sizeDelta)
399+
k.reportStoreSizeMetrics(0, len(encodedKey) + len(oldRawValue))
396400
store.Delete(encodedKey)
397401
} else {
398402
// We have children, mark as an empty placeholder without deleting.
399-
sizeDelta := float32(len(types.EncodedNoDataValue) - len(oldRawValue))
400-
ReportStoreSizeMetrics(k, sizeDelta)
403+
k.reportStoreSizeMetrics(len(types.EncodedNoDataValue), len(oldRawValue))
401404
store.Set(encodedKey, types.EncodedNoDataValue)
402405
}
403406
} else {
404407
// Update the value.
405408
newRawValue := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(entry.StringValue())}, []byte{})
406-
sizeDelta := float32(len(newRawValue) - len(oldRawValue))
407-
ReportStoreSizeMetrics(k, sizeDelta)
409+
k.reportStoreSizeMetrics(len(newRawValue), len(oldRawValue))
408410
store.Set(encodedKey, newRawValue)
409411
}
410412

@@ -419,8 +421,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
419421
break
420422
}
421423
encodedAncestor := types.PathToEncodedKey(ancestor)
422-
sizeDelta := float32(-len(encodedAncestor) - len(types.EncodedNoDataValue))
423-
ReportStoreSizeMetrics(k, sizeDelta)
424+
k.reportStoreSizeMetrics(0, len(encodedAncestor) + len(types.EncodedNoDataValue))
424425
store.Delete(encodedAncestor)
425426
}
426427
} else {
@@ -432,8 +433,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
432433
break
433434
}
434435
encodedAncestor := types.PathToEncodedKey(ancestor)
435-
sizeDelta := float32(len(encodedAncestor) + len(types.EncodedNoDataValue))
436-
ReportStoreSizeMetrics(k, sizeDelta)
436+
k.reportStoreSizeMetrics(len(encodedAncestor) + len(types.EncodedNoDataValue), 0)
437437
store.Set(encodedAncestor, types.EncodedNoDataValue)
438438
}
439439
}

0 commit comments

Comments
 (0)