From 04bc3f95fb11c93ae75c8eb0fa4411e8990c3cf7 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sun, 7 Jun 2020 21:48:23 +0000 Subject: [PATCH] [libp2p] support both local json and remote event tracer Signed-off-by: Leo Chen --- p2p/host.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/p2p/host.go b/p2p/host.go index 54b81a6b8..51d928fcb 100644 --- a/p2p/host.go +++ b/p2p/host.go @@ -86,14 +86,22 @@ func NewHost(self *Peer, key libp2p_crypto.PrivKey) (Host, error) { traceFile := os.Getenv("P2P_TRACEFILE") if len(traceFile) > 0 { - pi, err := libp2p_peer.AddrInfoFromP2pAddr(ma.StringCast(traceFile)) - if err == nil { - tracer, _ := libp2p_pubsub.NewRemoteTracer(ctx, p2pHost, *pi) + var tracer libp2p_pubsub.EventTracer + var tracerErr error + if strings.HasPrefix(traceFile, "file:") { + tracer, tracerErr = libp2p_pubsub.NewJSONTracer(strings.TrimPrefix(traceFile, "file:")) + } else { + pi, err := libp2p_peer.AddrInfoFromP2pAddr(ma.StringCast(traceFile)) + if err == nil { + tracer, tracerErr = libp2p_pubsub.NewRemoteTracer(ctx, p2pHost, *pi) + } + } + if tracerErr == nil && tracer != nil { options = append(options, libp2p_pubsub.WithEventTracer(tracer)) } else { - utils.Logger().Info(). - Str("RemoteTracer", traceFile). - Msg("can't get address from P2P_TRACEFILE") + utils.Logger().Warn(). + Str("Tracer", traceFile). + Msg("can't add event tracer from P2P_TRACEFILE") } }