Skip to content

Commit

Permalink
Add consensus tracers (#2237)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Nov 17, 2022
1 parent 32b1835 commit 2340273
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
14 changes: 12 additions & 2 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,11 @@ func (m *manager) createAvalancheChain(

handler.SetBootstrapper(bootstrapper)

var consensus avcon.Consensus = &avcon.Topological{}
if m.TracingEnabled {
consensus = avcon.Trace(consensus, m.Tracer)
}

// create engine gear
engineConfig := aveng.Config{
Ctx: bootstrapperConfig.Ctx,
Expand All @@ -748,7 +753,7 @@ func (m *manager) createAvalancheChain(
Sender: bootstrapperConfig.Sender,
Validators: vdrs,
Params: consensusParams,
Consensus: &avcon.Topological{},
Consensus: consensus,
}
engine, err := aveng.New(engineConfig)
if err != nil {
Expand Down Expand Up @@ -969,6 +974,11 @@ func (m *manager) createSnowmanChain(
return nil, fmt.Errorf("couldn't initialize snow base message handler: %w", err)
}

var consensus smcon.Consensus = &smcon.Topological{}
if m.TracingEnabled {
consensus = smcon.Trace(consensus, m.Tracer)
}

// Create engine, bootstrapper and state-syncer in this order,
// to make sure start callbacks are duly initialized
engineConfig := smeng.Config{
Expand All @@ -978,7 +988,7 @@ func (m *manager) createSnowmanChain(
Sender: commonCfg.Sender,
Validators: vdrs,
Params: consensusParams,
Consensus: &smcon.Topological{},
Consensus: consensus,
}
engine, err := smeng.New(engineConfig)
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions snow/consensus/avalanche/traced_consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package avalanche

import (
"context"

"go.opentelemetry.io/otel/attribute"

oteltrace "go.opentelemetry.io/otel/trace"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/trace"
)

var _ Consensus = (*tracedConsensus)(nil)

type tracedConsensus struct {
Consensus
tracer trace.Tracer
}

func Trace(consensus Consensus, tracer trace.Tracer) Consensus {
return &tracedConsensus{
Consensus: consensus,
tracer: tracer,
}
}

func (c *tracedConsensus) Add(ctx context.Context, vtx Vertex) error {
ctx, span := c.tracer.Start(ctx, "tracedConsensus.Add", oteltrace.WithAttributes(
attribute.Stringer("vtxID", vtx.ID()),
))
defer span.End()

return c.Consensus.Add(ctx, vtx)
}

func (c *tracedConsensus) RecordPoll(ctx context.Context, votes ids.UniqueBag) error {
var allVotes ids.BitSet64
for _, vote := range votes {
allVotes.Union(vote)
}

ctx, span := c.tracer.Start(ctx, "tracedConsensus.RecordPoll", oteltrace.WithAttributes(
attribute.Int("numVotes", allVotes.Len()),
attribute.Int("numVtxIDs", len(votes)),
))
defer span.End()

return c.Consensus.RecordPoll(ctx, votes)
}
49 changes: 49 additions & 0 deletions snow/consensus/snowman/traced_consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package snowman

import (
"context"

"go.opentelemetry.io/otel/attribute"

oteltrace "go.opentelemetry.io/otel/trace"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/trace"
)

var _ Consensus = (*tracedConsensus)(nil)

type tracedConsensus struct {
Consensus
tracer trace.Tracer
}

func Trace(consensus Consensus, tracer trace.Tracer) Consensus {
return &tracedConsensus{
Consensus: consensus,
tracer: tracer,
}
}

func (c *tracedConsensus) Add(ctx context.Context, blk Block) error {
ctx, span := c.tracer.Start(ctx, "tracedConsensus.Add", oteltrace.WithAttributes(
attribute.Stringer("blkID", blk.ID()),
attribute.Int64("height", int64(blk.Height())),
))
defer span.End()

return c.Consensus.Add(ctx, blk)
}

func (c *tracedConsensus) RecordPoll(ctx context.Context, votes ids.Bag) error {
ctx, span := c.tracer.Start(ctx, "tracedConsensus.RecordPoll", oteltrace.WithAttributes(
attribute.Int("numVotes", votes.Len()),
attribute.Int("numBlkIDs", len(votes.List())),
))
defer span.End()

return c.Consensus.RecordPoll(ctx, votes)
}

0 comments on commit 2340273

Please sign in to comment.