Skip to content

Commit b15619e

Browse files
authored
Reduce unnecessary logging work (#826)
1 parent 0a0b298 commit b15619e

File tree

5 files changed

+15
-24
lines changed

5 files changed

+15
-24
lines changed

bitswap/client/client.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/libp2p/go-libp2p/core/routing"
3636
"go.opentelemetry.io/otel/attribute"
3737
"go.opentelemetry.io/otel/trace"
38+
"go.uber.org/zap/zapcore"
3839
)
3940

4041
var log = logging.Logger("bitswap/client")
@@ -379,8 +380,10 @@ func (bs *Client) receiveBlocksFrom(ctx context.Context, from peer.ID, blks []bl
379380
}
380381

381382
wanted, notWanted := bs.sim.SplitWantedUnwanted(blks)
382-
for _, b := range notWanted {
383-
log.Debugf("[recv] block not in wantlist; cid=%s, peer=%s", b.Cid(), from)
383+
if log.Level().Enabled(zapcore.DebugLevel) {
384+
for _, b := range notWanted {
385+
log.Debugf("[recv] block not in wantlist; cid=%s, peer=%s", b.Cid(), from)
386+
}
384387
}
385388

386389
allKs := make([]cid.Cid, 0, len(blks))
@@ -409,10 +412,6 @@ func (bs *Client) receiveBlocksFrom(ctx context.Context, from peer.ID, blks []bl
409412
bs.notif.Publish(from, b)
410413
}
411414

412-
for _, b := range wanted {
413-
log.Debugw("Bitswap.GetBlockRequest.End", "cid", b.Cid())
414-
}
415-
416415
return nil
417416
}
418417

@@ -431,8 +430,10 @@ func (bs *Client) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.
431430

432431
if len(iblocks) > 0 {
433432
bs.updateReceiveCounters(iblocks)
434-
for _, b := range iblocks {
435-
log.Debugf("[recv] block; cid=%s, peer=%s", b.Cid(), p)
433+
if log.Level().Enabled(zapcore.DebugLevel) {
434+
for _, b := range iblocks {
435+
log.Debugf("[recv] block; cid=%s, peer=%s", b.Cid(), p)
436+
}
436437
}
437438
}
438439

bitswap/client/internal/getter/getter.go

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func AsyncGetBlocks(ctx context.Context, sessctx context.Context, keys []cid.Cid
8383
remaining := cid.NewSet()
8484
promise := notif.Subscribe(ctx, keys...)
8585
for _, k := range keys {
86-
log.Debugw("Bitswap.GetBlockRequest.Start", "cid", k)
8786
remaining.Add(k)
8887
}
8988

bitswap/client/internal/messagequeue/messagequeue.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ import (
1616
logging "github.com/ipfs/go-log/v2"
1717
peer "github.com/libp2p/go-libp2p/core/peer"
1818
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
19-
"go.uber.org/zap"
19+
"go.uber.org/zap/zapcore"
2020
)
2121

22-
var (
23-
log = logging.Logger("bitswap/client/msgq")
24-
sflog = log.Desugar()
25-
)
22+
var log = logging.Logger("bitswap/client/msgq")
2623

2724
const (
2825
// maxMessageSize is the maximum message size in bytes
@@ -692,7 +689,7 @@ func (mq *MessageQueue) handleResponse(ks []cid.Cid) {
692689

693690
func (mq *MessageQueue) logOutgoingMessage(wantlist []bsmsg.Entry) {
694691
// Save some CPU cycles and allocations if log level is higher than debug
695-
if ce := sflog.Check(zap.DebugLevel, "sent message"); ce == nil {
692+
if !log.Level().Enabled(zapcore.DebugLevel) {
696693
return
697694
}
698695

bitswap/client/internal/session/session.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ import (
1717
peer "github.com/libp2p/go-libp2p/core/peer"
1818
"github.com/libp2p/go-libp2p/core/routing"
1919
"go.opentelemetry.io/otel/trace"
20-
"go.uber.org/zap"
20+
"go.uber.org/zap/zapcore"
2121
)
2222

23-
var (
24-
log = logging.Logger("bitswap/session")
25-
sflog = log.Desugar()
26-
)
23+
var log = logging.Logger("bitswap/session")
2724

2825
const (
2926
broadcastLiveWantsLimit = 64
@@ -208,7 +205,7 @@ func (s *Session) ReceiveFrom(from peer.ID, ks []cid.Cid, haves []cid.Cid, dontH
208205

209206
func (s *Session) logReceiveFrom(from peer.ID, interestedKs []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
210207
// Save some CPU cycles if log level is higher than debug
211-
if ce := sflog.Check(zap.DebugLevel, "Bitswap <- rcv message"); ce == nil {
208+
if !log.Level().Enabled(zapcore.DebugLevel) {
212209
return
213210
}
214211

bitswap/server/server.go

-3
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,7 @@ func (bs *Server) startWorkers(ctx context.Context) {
264264
func (bs *Server) taskWorker(ctx context.Context, id int) {
265265
defer bs.waitWorkers.Done()
266266

267-
log := log.With("ID", id)
268-
defer log.Debug("bitswap task worker shutting down...")
269267
for {
270-
log.Debug("Bitswap.TaskWorker.Loop")
271268
select {
272269
case nextEnvelope := <-bs.engine.Outbox():
273270
select {

0 commit comments

Comments
 (0)