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

Add fine grained metrics+logging for handling, processing, and grab l… #1301

Merged
merged 5 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 40 additions & 11 deletions snow/networking/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ func (h *handler) handleSyncMsg(ctx context.Context, msg Message) error {
op = msg.Op()
body = msg.Message()
startTime = h.clock.Time()
// Check if the chain is in normal operation at the start of message execution (may change during execution)
isNormalOp = h.ctx.State.Get().State == snow.NormalOp
)
h.ctx.Log.Debug("forwarding sync message to consensus",
zap.Stringer("nodeID", nodeID),
Expand All @@ -435,23 +437,30 @@ func (h *handler) handleSyncMsg(ctx context.Context, msg Message) error {
)
h.resourceTracker.StartProcessing(nodeID, startTime)
h.ctx.Lock.Lock()
lockAcquiredTime := h.clock.Time()
defer func() {
h.ctx.Lock.Unlock()

var (
endTime = h.clock.Time()
histogram = h.metrics.messages[op]
processingTime = endTime.Sub(startTime)
endTime = h.clock.Time()
messageHistograms = h.metrics.messages[op]
acquireLockTime = lockAcquiredTime.Sub(startTime)
processingTime = endTime.Sub(lockAcquiredTime)
handlingTime = endTime.Sub(startTime)
)
h.resourceTracker.StopProcessing(nodeID, endTime)
histogram.Observe(float64(processingTime))
messageHistograms.handlingTime.Observe(float64(handlingTime))
messageHistograms.acquireLockTime.Observe(float64(acquireLockTime))
messageHistograms.processingTime.Observe(float64(processingTime))
msg.OnFinishedHandling()
h.ctx.Log.Debug("finished handling sync message",
zap.Stringer("messageOp", op),
)
if processingTime > syncProcessingTimeWarnLimit && h.ctx.State.Get().State == snow.NormalOp {
if handlingTime > syncProcessingTimeWarnLimit && isNormalOp {
h.ctx.Log.Warn("handling sync message took longer than expected",
zap.Duration("handlingTime", handlingTime),
zap.Duration("processingTime", processingTime),
zap.Duration("acquireLockTime", acquireLockTime),
zap.Stringer("nodeID", nodeID),
zap.Stringer("messageOp", op),
zap.Any("message", body),
Expand Down Expand Up @@ -756,11 +765,14 @@ func (h *handler) executeAsyncMsg(ctx context.Context, msg Message) error {
h.resourceTracker.StartProcessing(nodeID, startTime)
defer func() {
var (
endTime = h.clock.Time()
histogram = h.metrics.messages[op]
endTime = h.clock.Time()
messageHistograms = h.metrics.messages[op]
)
h.resourceTracker.StopProcessing(nodeID, endTime)
histogram.Observe(float64(endTime.Sub(startTime)))
// Processing an async message does not grab a lock, so handling/processing
// times are identical and we skip observing [acquireLockTime].
messageHistograms.handlingTime.Observe(float64(endTime.Sub(startTime)))
messageHistograms.processingTime.Observe(float64(endTime.Sub(startTime)))
msg.OnFinishedHandling()
h.ctx.Log.Debug("finished handling async message",
zap.Stringer("messageOp", op),
Expand Down Expand Up @@ -835,6 +847,8 @@ func (h *handler) handleChanMsg(msg message.InboundMessage) error {
op = msg.Op()
body = msg.Message()
startTime = h.clock.Time()
// Check if the chain is in normal operation at the start of message execution (may change during execution)
isNormalOp = h.ctx.State.Get().State == snow.NormalOp
)
h.ctx.Log.Debug("forwarding chan message to consensus",
zap.Stringer("messageOp", op),
Expand All @@ -844,18 +858,33 @@ func (h *handler) handleChanMsg(msg message.InboundMessage) error {
zap.Any("message", body),
)
h.ctx.Lock.Lock()
lockAcquiredTime := h.clock.Time()
defer func() {
h.ctx.Lock.Unlock()

var (
endTime = h.clock.Time()
histogram = h.metrics.messages[op]
endTime = h.clock.Time()
messageHistograms = h.metrics.messages[op]
acquireLockTime = lockAcquiredTime.Sub(startTime)
processingTime = endTime.Sub(lockAcquiredTime)
handlingTime = endTime.Sub(startTime)
)
histogram.Observe(float64(endTime.Sub(startTime)))
messageHistograms.handlingTime.Observe(float64(handlingTime))
messageHistograms.acquireLockTime.Observe(float64(acquireLockTime))
messageHistograms.processingTime.Observe(float64(processingTime))
msg.OnFinishedHandling()
h.ctx.Log.Debug("finished handling chan message",
zap.Stringer("messageOp", op),
)
if handlingTime > syncProcessingTimeWarnLimit && isNormalOp {
h.ctx.Log.Warn("handling chan message took longer than expected",
zap.Duration("handlingTime", handlingTime),
zap.Duration("processingTime", processingTime),
zap.Duration("acquireLockTime", acquireLockTime),
zap.Stringer("messageOp", op),
zap.Any("message", body),
)
}
}()

state := h.ctx.State.Get()
Expand Down
41 changes: 32 additions & 9 deletions snow/networking/handler/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import (
type metrics struct {
expired prometheus.Counter
asyncExpired prometheus.Counter
messages map[message.Op]metric.Averager
messages map[message.Op]*messageProcessing
}

type messageProcessing struct {
handlingTime metric.Averager
processingTime metric.Averager
acquireLockTime metric.Averager
}

func newMetrics(namespace string, reg prometheus.Registerer) (*metrics, error) {
Expand All @@ -37,16 +43,33 @@ func newMetrics(namespace string, reg prometheus.Registerer) (*metrics, error) {
reg.Register(asyncExpired),
)

messages := make(map[message.Op]metric.Averager, len(message.ConsensusOps))
messages := make(map[message.Op]*messageProcessing, len(message.ConsensusOps))
for _, op := range message.ConsensusOps {
opStr := op.String()
messages[op] = metric.NewAveragerWithErrs(
namespace,
opStr,
fmt.Sprintf("time (in ns) of processing a %s", opStr),
reg,
&errs,
)
messageProcessing := &messageProcessing{
handlingTime: metric.NewAveragerWithErrs(
namespace,
opStr,
fmt.Sprintf("time (in ns) of handling a %s", opStr),
reg,
&errs,
),
processingTime: metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_processing", opStr),
fmt.Sprintf("time (in ns) of processing a %s", opStr),
reg,
&errs,
),
acquireLockTime: metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_lock", opStr),
fmt.Sprintf("time (in ns) of acquiring a lock to process a %s", opStr),
reg,
&errs,
),
}
messages[op] = messageProcessing
}

return &metrics{
Expand Down