Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix: forward sent messages to the tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Aug 5, 2022
1 parent 5a3d01a commit 7ac1105
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ func WithTracer(tap tracer.Tracer) Option {
return Option{
func(bs *Bitswap) {
bs.tracer = tap
// the tests use this to hot update tracers, we need to update tracers of impls if we are running
if bs.Client != nil {
if tap != nil {
tap = nopReceiveTracer{tap}
}
client.WithTracer(tap)(bs.Client)
// no need to check for server as they can't not be both running
server.WithTracer(tap)(bs.Server)
}
},
}
}
6 changes: 6 additions & 0 deletions polyfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func New(ctx context.Context, net network.BitSwapNetwork, bstore blockstore.Bloc
}
}

if bs.tracer != nil {
var tracer tracer.Tracer = nopReceiveTracer{bs.tracer}
clientOptions = append(clientOptions, client.WithTracer(tracer))
serverOptions = append(serverOptions, server.WithTracer(tracer))
}

stats := metrics.New(ctx)
bs.Server = server.New(ctx, net, bstore, stats, serverOptions...)
bs.Client = client.New(ctx, net, bstore, stats, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
Expand Down
20 changes: 20 additions & 0 deletions sendOnlyTracer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bitswap

import (
"github.com/ipfs/go-bitswap/message"
"github.com/ipfs/go-bitswap/tracer"
"github.com/libp2p/go-libp2p-core/peer"
)

type sendOnlyTracer interface {
MessageSent(peer.ID, message.BitSwapMessage)
}

var _ tracer.Tracer = nopReceiveTracer{}

// we need to only trace sends because we already trace receives in the polyfill object (to not get them traced twice)
type nopReceiveTracer struct {
sendOnlyTracer
}

func (nopReceiveTracer) MessageReceived(peer.ID, message.BitSwapMessage) {}

0 comments on commit 7ac1105

Please sign in to comment.