Skip to content

Commit

Permalink
Reduce unnecessary logging work (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero authored Jan 31, 2025
1 parent 0a0b298 commit b15619e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
17 changes: 9 additions & 8 deletions bitswap/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/libp2p/go-libp2p/core/routing"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap/zapcore"
)

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

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

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

for _, b := range wanted {
log.Debugw("Bitswap.GetBlockRequest.End", "cid", b.Cid())
}

return nil
}

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

if len(iblocks) > 0 {
bs.updateReceiveCounters(iblocks)
for _, b := range iblocks {
log.Debugf("[recv] block; cid=%s, peer=%s", b.Cid(), p)
if log.Level().Enabled(zapcore.DebugLevel) {
for _, b := range iblocks {
log.Debugf("[recv] block; cid=%s, peer=%s", b.Cid(), p)
}
}
}

Expand Down
1 change: 0 additions & 1 deletion bitswap/client/internal/getter/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func AsyncGetBlocks(ctx context.Context, sessctx context.Context, keys []cid.Cid
remaining := cid.NewSet()
promise := notif.Subscribe(ctx, keys...)
for _, k := range keys {
log.Debugw("Bitswap.GetBlockRequest.Start", "cid", k)
remaining.Add(k)
}

Expand Down
9 changes: 3 additions & 6 deletions bitswap/client/internal/messagequeue/messagequeue.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ import (
logging "github.com/ipfs/go-log/v2"
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
log = logging.Logger("bitswap/client/msgq")
sflog = log.Desugar()
)
var log = logging.Logger("bitswap/client/msgq")

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

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

Expand Down
9 changes: 3 additions & 6 deletions bitswap/client/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ import (
peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
log = logging.Logger("bitswap/session")
sflog = log.Desugar()
)
var log = logging.Logger("bitswap/session")

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

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

Expand Down
3 changes: 0 additions & 3 deletions bitswap/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ func (bs *Server) startWorkers(ctx context.Context) {
func (bs *Server) taskWorker(ctx context.Context, id int) {
defer bs.waitWorkers.Done()

log := log.With("ID", id)
defer log.Debug("bitswap task worker shutting down...")
for {
log.Debug("Bitswap.TaskWorker.Loop")
select {
case nextEnvelope := <-bs.engine.Outbox():
select {
Expand Down

0 comments on commit b15619e

Please sign in to comment.