Skip to content

Commit

Permalink
Collect latency metrics on Transaction.Commit()
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak authored and joshklop committed Oct 31, 2023
1 parent a8c29a8 commit ed598f3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
10 changes: 9 additions & 1 deletion db/event_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import "time"

type EventListener interface {
OnIO(write bool, duration time.Duration)
OnCommit(duration time.Duration)
}

type SelectiveListener struct {
OnIOCb func(write bool, duration time.Duration)
OnIOCb func(write bool, duration time.Duration)
OnCommitCb func(duration time.Duration)
}

func (l *SelectiveListener) OnIO(write bool, duration time.Duration) {
if l.OnIOCb != nil {
l.OnIOCb(write, duration)
}
}

func (l *SelectiveListener) OnCommit(duration time.Duration) {
if l.OnCommitCb != nil {
l.OnCommitCb(duration)
}
}
2 changes: 2 additions & 0 deletions db/pebble/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (l *eventListener) OnIO(write bool, _ time.Duration) {
}
}

func (l *eventListener) OnCommit(_ time.Duration) {}

func TestTransaction(t *testing.T) {
listener := eventListener{}
t.Run("new transaction can retrieve existing value", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions db/pebble/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (t *Transaction) Discard() error {

// Commit : see db.Transaction.Commit
func (t *Transaction) Commit() error {
start := time.Now()
defer func() { t.listener.OnCommit(time.Since(start)) }()
if t.batch != nil {
return utils.RunAndWrapOnError(t.Discard, t.batch.Commit(pebble.Sync))
}
Expand Down
23 changes: 22 additions & 1 deletion node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@ func makeDBMetrics() db.EventListener {
Name: "write_latency",
Buckets: latencyBuckets,
})
commitLatency := prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "db",
Name: "commit_latency",
Buckets: []float64{
5000,
10000,
20000,
30000,
40000,
50000,
100000, // 100ms
200000,
300000,
500000,
1000000,
math.Inf(0),
},
})

prometheus.MustRegister(readLatencyHistogram, writeLatencyHistogram)
prometheus.MustRegister(readLatencyHistogram, writeLatencyHistogram, commitLatency)
return &db.SelectiveListener{
OnIOCb: func(write bool, duration time.Duration) {
if write {
Expand All @@ -49,6 +67,9 @@ func makeDBMetrics() db.EventListener {
readLatencyHistogram.Observe(float64(duration.Microseconds()))
}
},
OnCommitCb: func(duration time.Duration) {
commitLatency.Observe(float64(duration.Microseconds()))
},
}
}

Expand Down

0 comments on commit ed598f3

Please sign in to comment.