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

feat(store/v2): basic metrics #18529

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 42 additions & 0 deletions store/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package metrics

import (
"time"

"github.com/hashicorp/go-metrics"
)

var _ StoreMetrics = Metrics{}

// StoreMetrics defines the set of supported metric APIs for the store package.
type StoreMetrics interface {
MeasureSince(start time.Time, keys ...string)
}

// Metrics defines a default StoreMetrics implementation.
type Metrics struct {
Labels []metrics.Label
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

// NewMetrics returns a new instance of the Metrics with labels set by the node
// operator.
func NewMetrics(labels [][]string) Metrics {
m := Metrics{}

if numGlobalLabels := len(labels); numGlobalLabels > 0 {
parsedGlobalLabels := make([]metrics.Label, numGlobalLabels)
for i, label := range labels {
parsedGlobalLabels[i] = metrics.Label{Name: label[0], Value: label[1]}
}

m.Labels = parsedGlobalLabels
}

return m
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

// MeasureSince provides a wrapper functionality for emitting a time measure
// metric with global labels (if any).
func (m Metrics) MeasureSince(start time.Time, keys ...string) {
metrics.MeasureSinceWithLabels(keys, start.UTC(), m.Labels)
}
32 changes: 32 additions & 0 deletions store/root/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"io"
"slices"
"time"

"github.com/cockroachdb/errors"

"cosmossdk.io/log"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/kv/branch"
"cosmossdk.io/store/v2/kv/trace"
"cosmossdk.io/store/v2/metrics"
"cosmossdk.io/store/v2/pruning"
)

Expand Down Expand Up @@ -57,13 +59,17 @@ type Store struct {

// pruningManager manages pruning of the SS and SC backends
pruningManager *pruning.Manager

// telemetry reflects a telemetry agent responsible for emitting metrics (if any)
telemetry metrics.StoreMetrics
}

func New(
logger log.Logger,
initVersion uint64,
ss store.VersionedDatabase,
sc store.Committer,
m metrics.StoreMetrics,
) (store.RootStore, error) {
rootKVStore, err := branch.New(defaultStoreKey, ss)
if err != nil {
Expand All @@ -79,6 +85,7 @@ func New(
stateCommitment: sc,
rootKVStore: rootKVStore,
pruningManager: pruningManager,
telemetry: m,
}, nil
}

Expand Down Expand Up @@ -158,6 +165,11 @@ func (s *Store) GetLatestVersion() (uint64, error) {
}

func (s *Store) Query(storeKey string, version uint64, key []byte, prove bool) (store.QueryResult, error) {
if s.telemetry != nil {
now := time.Now()
s.telemetry.MeasureSince(now, "root_store", "query")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

val, err := s.stateStore.Get(storeKey, version, key)
if err != nil {
return store.QueryResult{}, err
Expand Down Expand Up @@ -202,6 +214,11 @@ func (s *Store) GetBranchedKVStore(_ string) store.BranchedKVStore {
}

func (s *Store) LoadLatestVersion() error {
if s.telemetry != nil {
now := time.Now()
s.telemetry.MeasureSince(now, "root_store", "load_latest_version")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

lv, err := s.GetLatestVersion()
if err != nil {
return err
Expand All @@ -211,6 +228,11 @@ func (s *Store) LoadLatestVersion() error {
}

func (s *Store) LoadVersion(version uint64) error {
if s.telemetry != nil {
now := time.Now()
s.telemetry.MeasureSince(now, "root_store", "load_version")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

return s.loadVersion(version)
}

Expand Down Expand Up @@ -278,6 +300,11 @@ func (s *Store) Branch() store.BranchedRootStore {
// by constructing a CommitInfo object, which in turn creates and writes a batch
// of the current changeset to the SC tree.
func (s *Store) WorkingHash() ([]byte, error) {
if s.telemetry != nil {
now := time.Now()
s.telemetry.MeasureSince(now, "root_store", "working_hash")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

if s.workingHash == nil {
if err := s.writeSC(); err != nil {
return nil, err
Expand All @@ -302,6 +329,11 @@ func (s *Store) Write() {
//
// Note, Commit() commits SC and SC synchronously.
func (s *Store) Commit() ([]byte, error) {
if s.telemetry != nil {
now := time.Now()
s.telemetry.MeasureSince(now, "root_store", "commit")
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
}

if s.workingHash == nil {
return nil, fmt.Errorf("working hash is nil; must call WorkingHash() before Commit()")
}
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion store/root/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *RootStoreTestSuite) SetupTest() {

sc := iavl.NewIavlTree(dbm.NewMemDB(), noopLog, iavl.DefaultConfig())

rs, err := New(noopLog, 1, ss, sc)
rs, err := New(noopLog, 1, ss, sc, nil)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
s.Require().NoError(err)

rs.SetTracer(io.Discard)
Expand Down
Loading