Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cosmos-vstorage): split 'size_delta' counter into 'size_increase' and 'size_decrease' #11063

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions golang/cosmos/x/vstorage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,19 @@ func NewKeeper(storeKey storetypes.StoreKey) Keeper {
}
}

var MetricKeysSizeDelta = []string{"store", "size_delta"}
var MetricKeyStoreAllocated = []string{"store", "allocated"}
var MetricKeyStoreReleased = []string{"store", "released"}
const MetricLabelStoreKey = "storeKey"

func NewMetricsLabels(k Keeper) []metrics.Label {
return []metrics.Label{
func ReportStoreSizeMetrics(k Keeper, sizeDelta float32) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this a method on the Keeper?

Suggested change
func ReportStoreSizeMetrics(k Keeper, sizeDelta float32) {
// ReportStoreSizeMetrics exports sizeDelta metrics when
// Cosmos telemetry is enabled.
func (k Keeper) ReportStoreSizeMetrics(sizeDelta float32) {

metricsLabel := []metrics.Label{
telemetry.NewLabel(MetricLabelStoreKey, k.storeKey.Name()),
}
if sizeDelta >= 0 {
telemetry.IncrCounterWithLabels(MetricKeyStoreAllocated, sizeDelta, metricsLabel)
} else {
telemetry.IncrCounterWithLabels(MetricKeyStoreReleased, -sizeDelta, metricsLabel)
}
}

// ExportStorage fetches all storage
Expand Down Expand Up @@ -228,7 +234,7 @@ func (k Keeper) RemoveEntriesWithPrefix(ctx sdk.Context, pathPrefix string) {
for _, key := range keys {
rawValue := store.Get(key)
sizeDelta := float32(-len(key) - len(rawValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Delete(key)
}

Expand Down Expand Up @@ -386,19 +392,19 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
if !k.HasChildren(ctx, path) {
// We have no children, can delete.
sizeDelta := float32(-len(encodedKey) - len(oldRawValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Delete(encodedKey)
} else {
// We have children, mark as an empty placeholder without deleting.
sizeDelta := float32(len(types.EncodedNoDataValue) - len(oldRawValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Set(encodedKey, types.EncodedNoDataValue)
}
} else {
// Update the value.
newRawValue := bytes.Join([][]byte{types.EncodedDataPrefix, []byte(entry.StringValue())}, []byte{})
sizeDelta := float32(len(newRawValue) - len(oldRawValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Set(encodedKey, newRawValue)
}

Expand All @@ -414,7 +420,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
}
encodedAncestor := types.PathToEncodedKey(ancestor)
sizeDelta := float32(-len(encodedAncestor) - len(types.EncodedNoDataValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Delete(encodedAncestor)
}
} else {
Expand All @@ -427,7 +433,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
}
encodedAncestor := types.PathToEncodedKey(ancestor)
sizeDelta := float32(len(encodedAncestor) + len(types.EncodedNoDataValue))
telemetry.IncrCounterWithLabels(MetricKeysSizeDelta, sizeDelta, NewMetricsLabels(k))
ReportStoreSizeMetrics(k, sizeDelta)
store.Set(encodedAncestor, types.EncodedNoDataValue)
}
}
Expand Down
Loading