From 77696a47f7ec4a06e1b4a92ac4bf3333581194e1 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Tue, 18 Nov 2014 22:05:53 -0800 Subject: [PATCH] events(bitswap) try the new event logger in the bitswap GetBlock method @jbenet @whyrusleeping Let me know if you want to direct the eventlog output to _both_ the file and stderr. Right now it goes to file. Perhaps this is just a minor bip in the larger discussion around log levels. https://github.com/jbenet/go-ipfs/issues/292 License: MIT Signed-off-by: Brian Tiger Chow --- exchange/bitswap/bitswap.go | 19 +++++++++++++------ util/key.go | 6 ++++++ 2 files changed, 19 insertions(+), 6 deletions(-) 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())