diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 4aaacdbfd..a4bb0ec0c 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -17,9 +17,10 @@ import ( strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy" peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" + "github.com/jbenet/go-ipfs/util/eventlog" ) -var log = u.Logger("bitswap") +var log = eventlog.Logger("bitswap") // New initializes a BitSwap instance that communicates over the // provided BitSwapNetwork. This function registers the returned instance as @@ -80,15 +81,21 @@ type bitswap struct { // // TODO ensure only one active request per key func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) { - log.Debugf("Get Block %v", k) - now := time.Now() - defer func() { - log.Debugf("GetBlock took %f secs", time.Now().Sub(now).Seconds()) - }() + + // make sure to derive a new |ctx| and pass it to children. It's correct to + // listen on |parent| here, but incorrect to pass |parent| to new async + // functions. This is difficult to enforce. May this comment keep you safe. ctx, cancelFunc := context.WithCancel(parent) defer cancelFunc() + ctx = eventlog.ContextWithMetadata(ctx, eventlog.Uuid("BitswapGetBlockRequest")) + log.Event(ctx, "BitswapGetBlockRequestBegin", &k) + + defer func() { + log.Event(ctx, "BitSwapGetBlockRequestEnd", &k) + }() + bs.wantlist.Add(k) promise := bs.notifications.Subscribe(ctx, k) diff --git a/util/key.go b/util/key.go index 7cbf09518..eca1255b5 100644 --- a/util/key.go +++ b/util/key.go @@ -63,6 +63,12 @@ func (k *Key) MarshalJSON() ([]byte, error) { return json.Marshal(b58.Encode([]byte(*k))) } +func (k *Key) Loggable() map[string]interface{} { + return map[string]interface{}{ + "key": k.String(), + } +} + // KeyFromDsKey returns a Datastore key func KeyFromDsKey(dsk ds.Key) Key { return Key(dsk.BaseNamespace())