@cryptotaxi247 / kubo / commits / 77696a47f

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 <brian@perfmode.com>

Brian Tiger Chow committed Nov 18, 2014 at 22:05 UTC 77696a47f7ec4a06e1b4a92ac4bf3333581194e1
2 files changed +19 -6
exchange/bitswap/bitswap.go
+13 -6
@@ -17,9 +17,10 @@ import (
17 strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
18 peer "github.com/jbenet/go-ipfs/peer"
19 u "github.com/jbenet/go-ipfs/util"
20 + "github.com/jbenet/go-ipfs/util/eventlog"
21 )
22
22 -var log = u.Logger("bitswap")
23 +var log = eventlog.Logger("bitswap")
24
25 // New initializes a BitSwap instance that communicates over the
26 // provided BitSwapNetwork. This function registers the returned instance as
@@ -80,15 +81,21 @@ type bitswap struct {
81 //
82 // TODO ensure only one active request per key
83 func (bs *bitswap) GetBlock(parent context.Context, k u.Key) (*blocks.Block, error) {
83 - log.Debugf("Get Block %v", k)
84 - now := time.Now()
85 - defer func() {
86 - log.Debugf("GetBlock took %f secs", time.Now().Sub(now).Seconds())
87 - }()
84 +
85 + // make sure to derive a new |ctx| and pass it to children. It's correct to
86 + // listen on |parent| here, but incorrect to pass |parent| to new async
87 + // functions. This is difficult to enforce. May this comment keep you safe.
88
89 ctx, cancelFunc := context.WithCancel(parent)
90 defer cancelFunc()
91
92 + ctx = eventlog.ContextWithMetadata(ctx, eventlog.Uuid("BitswapGetBlockRequest"))
93 + log.Event(ctx, "BitswapGetBlockRequestBegin", &k)
94 +
95 + defer func() {
96 + log.Event(ctx, "BitSwapGetBlockRequestEnd", &k)
97 + }()
98 +
99 bs.wantlist.Add(k)
100 promise := bs.notifications.Subscribe(ctx, k)
101
util/key.go
+6
@@ -63,6 +63,12 @@ func (k *Key) MarshalJSON() ([]byte, error) {
63 return json.Marshal(b58.Encode([]byte(*k)))
64 }
65
66 +func (k *Key) Loggable() map[string]interface{} {
67 + return map[string]interface{}{
68 + "key": k.String(),
69 + }
70 +}
71 +
72 // KeyFromDsKey returns a Datastore key
73 func KeyFromDsKey(dsk ds.Key) Key {
74 return Key(dsk.BaseNamespace())