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

fixup(golang/.../vstorage/keeper): report absolute values for metrics increase/decrease #11101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
36 changes: 18 additions & 18 deletions golang/cosmos/x/vstorage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,24 @@ func NewKeeper(storeKey storetypes.StoreKey) Keeper {
}
}

// size_increase and 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.
var MetricKeyStoreSizeIncrease = []string{"store", "size_increase"}
var MetricKeyStoreSizeDecrese = []string{"store", "size_decrease"}
var MetricKeyStoreSizeDecrease = []string{"store", "size_decrease"}
const MetricLabelStoreKey = "storeKey"

func ReportStoreSizeMetrics(k Keeper, sizeDelta float32) {
// reportStoreSizeMetrics exports store size increase/decrease metrics
// when Cosmos telemetry is enabled.
func (k Keeper) reportStoreSizeMetrics(increase int, decrease int) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using uint for increase and increase forces unnecessary type conversions later in the code, so I am going to stick to int here.

x/vstorage/keeper/keeper.go:242:31: cannot use len(key) + len(rawValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:399:32: cannot use len(encodedKey) + len(oldRawValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:403:29: cannot use len(types.EncodedNoDataValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:403:60: cannot use len(oldRawValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:409:28: cannot use len(newRawValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:409:46: cannot use len(oldRawValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:424:32: cannot use len(encodedAncestor) + len(types.EncodedNoDataValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics
x/vstorage/keeper/keeper.go:436:29: cannot use len(encodedAncestor) + len(types.EncodedNoDataValue) (value of type int) as uint value in argument to k.reportStoreSizeMetrics

metricsLabel := []metrics.Label{
telemetry.NewLabel(MetricLabelStoreKey, k.storeKey.Name()),
}
if sizeDelta >= 0 {
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeIncrease, sizeDelta, metricsLabel)
} else {
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeDecrese, -sizeDelta, metricsLabel)
if increase > 0 {
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeIncrease, float32(increase), metricsLabel)
}
if decrease > 0 {
telemetry.IncrCounterWithLabels(MetricKeyStoreSizeDecrease, float32(decrease), metricsLabel)
}
}

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

for _, key := range keys {
rawValue := store.Get(key)
sizeDelta := float32(-len(key) - len(rawValue))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(0, len(key) + len(rawValue))
store.Delete(key)
}

Expand Down Expand Up @@ -391,20 +396,17 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
if !entry.HasValue() {
if !k.HasChildren(ctx, path) {
// We have no children, can delete.
sizeDelta := float32(-len(encodedKey) - len(oldRawValue))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(0, len(encodedKey) + len(oldRawValue))
store.Delete(encodedKey)
} else {
// We have children, mark as an empty placeholder without deleting.
sizeDelta := float32(len(types.EncodedNoDataValue) - len(oldRawValue))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(len(types.EncodedNoDataValue), len(oldRawValue))
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))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(len(newRawValue), len(oldRawValue))
store.Set(encodedKey, newRawValue)
}

Expand All @@ -419,8 +421,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
break
}
encodedAncestor := types.PathToEncodedKey(ancestor)
sizeDelta := float32(-len(encodedAncestor) - len(types.EncodedNoDataValue))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(0, len(encodedAncestor) + len(types.EncodedNoDataValue))
store.Delete(encodedAncestor)
}
} else {
Expand All @@ -432,8 +433,7 @@ func (k Keeper) SetStorage(ctx sdk.Context, entry agoric.KVEntry) {
break
}
encodedAncestor := types.PathToEncodedKey(ancestor)
sizeDelta := float32(len(encodedAncestor) + len(types.EncodedNoDataValue))
ReportStoreSizeMetrics(k, sizeDelta)
k.reportStoreSizeMetrics(len(encodedAncestor) + len(types.EncodedNoDataValue), 0)
store.Set(encodedAncestor, types.EncodedNoDataValue)
}
}
Expand Down
Loading