Skip to content

Commit

Permalink
chore(sew): measure latency (#105) (#107)
Browse files Browse the repository at this point in the history
Measure the latency of certain functions. We need more context about fcn
execution.

Related to debugging
[issue](#101)
  • Loading branch information
Lazar955 authored Nov 19, 2024
1 parent 8695ef9 commit 3f1daf5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Improvements
* [#105](https://github.com/babylonlabs-io/vigilante/pull/105) Measure latency

## v0.16.0

* [#94](https://github.com/babylonlabs-io/vigilante/pull/94) adds gosec and fixes gosec issues
Expand Down
15 changes: 15 additions & 0 deletions btcstaking-tracker/stakingeventwatcher/stakingeventwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func (sew *StakingEventWatcher) handleNewBlocks(blockNotifier *notifier.BlockEpo
// checkBabylonDelegations iterates over all active babylon delegations, and reports not already
// tracked delegations to the unbondingDelegationChan
func (sew *StakingEventWatcher) checkBabylonDelegations(status btcstakingtypes.BTCDelegationStatus, addDel func(del Delegation)) error {
defer sew.latency(fmt.Sprintf("checkBabylonDelegations: %s", status))()

var i = uint64(0)
for {
delegations, err := sew.babylonNodeAdapter.DelegationsByStatus(status, i, sew.cfg.NewDelegationsBatchSize)
Expand Down Expand Up @@ -610,6 +612,8 @@ func (sew *StakingEventWatcher) activateBtcDelegation(
ctx, cancel := sew.quitContext()
defer cancel()

defer sew.latency("activateBtcDelegation")()

if err := sew.pendingTracker.UpdateActivation(stakingTxHash, true); err != nil {
sew.logger.Debugf("skipping tx %s is not in pending tracker, err: %v", stakingTxHash, err)
}
Expand Down Expand Up @@ -647,3 +651,14 @@ func (sew *StakingEventWatcher) activateBtcDelegation(
}),
)
}

func (sew *StakingEventWatcher) latency(method string) func() {
startTime := time.Now()
return func() {
duration := time.Since(startTime)
sew.logger.Debug("execution time",
zap.String("method", method),
zap.Duration("latency", duration))
sew.metrics.MethodExecutionLatency.WithLabelValues(method).Observe(duration.Seconds())
}
}
6 changes: 6 additions & 0 deletions metrics/btcstaking_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type UnbondingWatcherMetrics struct {
DetectedNonUnbondingTransactionsCounter prometheus.Counter
FailedReportedActivateDelegations prometheus.Counter
ReportedActivateDelegationsCounter prometheus.Counter
MethodExecutionLatency *prometheus.HistogramVec
}

func newUnbondingWatcherMetrics(registry *prometheus.Registry) *UnbondingWatcherMetrics {
Expand Down Expand Up @@ -66,6 +67,11 @@ func newUnbondingWatcherMetrics(registry *prometheus.Registry) *UnbondingWatcher
Name: "unbonding_watcher_reported_activate_delegations",
Help: "The total number of unbonding transactions successfully reported to Babylon node",
}),
MethodExecutionLatency: registerer.NewHistogramVec(prometheus.HistogramOpts{
Name: "unbonding_watcher_method_latency_seconds",
Help: "Latency in seconds",
Buckets: []float64{.001, .002, .005, .01, .025, .05, .1},
}, []string{"method"}),
}

return uwMetrics
Expand Down

0 comments on commit 3f1daf5

Please sign in to comment.