diff --git a/options.go b/options.go index 16c8e2b9..0c087b71 100644 --- a/options.go +++ b/options.go @@ -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) + } }, } } diff --git a/polyfill.go b/polyfill.go index 31722c80..3ca47b1b 100644 --- a/polyfill.go +++ b/polyfill.go @@ -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))...) diff --git a/sendOnlyTracer.go b/sendOnlyTracer.go new file mode 100644 index 00000000..1a12403f --- /dev/null +++ b/sendOnlyTracer.go @@ -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) {}