Skip to content

Commit

Permalink
Add an aggregate metric for the theoretical write capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeler committed Jan 24, 2022
1 parent 7d34b9f commit 77a66ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion bolt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ func (b *BoltStore) StoreLog(log *raft.Log) error {

// StoreLogs is used to store a set of raft logs
func (b *BoltStore) StoreLogs(logs []*raft.Log) error {
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, time.Now())
now := time.Now()
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, now)
tx, err := b.conn.Begin(true)
if err != nil {
return err
Expand All @@ -194,6 +195,15 @@ func (b *BoltStore) StoreLogs(logs []*raft.Log) error {

metrics.AddSample([]string{"raft", "boltdb", "logsPerBatch"}, float32(len(logs)))
metrics.AddSample([]string{"raft", "boltdb", "logBatchSize"}, float32(batchSize))
// Both the deferral and the inline function are important for this metrics
// accuracy. Deferral allows us to calculate the metric after the tx.Commit
// has finished and thus account for all the processing of the operation.
// The inlined function ensures that we do not calculate the time.Since(now)
// at the time of deferral but rather when the go runtime executes the
// deferred function.
defer func() {
metrics.AddSample([]string{"raft", "boltdb", "writeCapacity"}, (float32(1000000000) / float32(time.Since(now).Nanoseconds())) * float32(len(logs)))
}()

return tx.Commit()
}
Expand Down
14 changes: 13 additions & 1 deletion v2/bolt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func (b *BoltStore) StoreLog(log *raft.Log) error {

// StoreLogs is used to store a set of raft logs
func (b *BoltStore) StoreLogs(logs []*raft.Log) error {
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, time.Now())
now := time.Now()
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, now)

tx, err := b.conn.Begin(true)
if err != nil {
return err
Expand All @@ -203,6 +205,16 @@ func (b *BoltStore) StoreLogs(logs []*raft.Log) error {

metrics.AddSample([]string{"raft", "boltdb", "logsPerBatch"}, float32(len(logs)))
metrics.AddSample([]string{"raft", "boltdb", "logBatchSize"}, float32(batchSize))
// Both the deferral and the inline function are important for this metrics
// accuracy. Deferral allows us to calculate the metric after the tx.Commit
// has finished and thus account for all the processing of the operation.
// The inlined function ensures that we do not calculate the time.Since(now)
// at the time of deferral but rather when the go runtime executes the
// deferred function.
defer func() {
metrics.AddSample([]string{"raft", "boltdb", "writeCapacity"}, (float32(1000000000) / float32(time.Since(now).Nanoseconds())) * float32(len(logs)))
}()


return tx.Commit()
}
Expand Down

0 comments on commit 77a66ca

Please sign in to comment.