From ab4599b6b8f10ca8fb9eb7b596eb1caf838b6af1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 13 Dec 2022 15:28:34 +1100 Subject: [PATCH] Add some tracing --- tracker/server.go | 32 ++++++++++++++++++++++++++++++-- tracker/udp/server/server.go | 5 +++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tracker/server.go b/tracker/server.go index f7d5ecb8..0d5068d4 100644 --- a/tracker/server.go +++ b/tracker/server.go @@ -2,12 +2,16 @@ package tracker import ( "context" + "encoding/hex" "net/netip" "sync" "time" "github.com/anacrolix/generics" "github.com/anacrolix/log" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/trace" "github.com/anacrolix/torrent/tracker/udp" ) @@ -81,9 +85,30 @@ func addMissing(orig []PeerInfo, new peerSet) { } } +var tracer = otel.Tracer("torrent.tracker.udp") + func (me *AnnounceHandler) Serve( ctx context.Context, req AnnounceRequest, addr AnnounceAddr, opts GetPeersOpts, ) (peers []PeerInfo, err error) { + ctx, span := tracer.Start( + ctx, + "AnnounceHandler.Serve", + trace.WithAttributes( + attribute.Int64("announce.request.num_want", int64(req.NumWant)), + attribute.Int("announce.request.port", int(req.Port)), + attribute.String("announce.request.info_hash", hex.EncodeToString(req.InfoHash[:])), + attribute.String("announce.request.event", req.Event.String()), + attribute.Int64("announce.get_peers.opts.max_count_value", int64(opts.MaxCount.Value)), + attribute.Bool("announce.get_peers.opts.max_count_ok", opts.MaxCount.Ok), + attribute.String("announce.source.addr.ip", addr.Addr().String()), + attribute.Int("announce.source.addr.port", int(addr.Port())), + ), + ) + defer span.End() + defer func() { + span.SetAttributes(attribute.Int("announce.get_peers.len", len(peers))) + }() + err = me.AnnounceTracker.TrackAnnounce(ctx, req, addr) if err != nil { return @@ -121,8 +146,11 @@ func (me *AnnounceHandler) Serve( } } me.mu.Lock() - // If we didn't have an operation, and don't have enough peers, start one. - if !op.Ok && len(peers) <= 1 { + // If we didn't have an operation, and don't have enough peers, start one. Allowing 1 is + // assuming the announcing peer might be that one. Really we should record a value to prevent + // duplicate announces. Also don't announce upstream if we got no peers because the caller asked + // for none. + if !op.Ok && len(peers) <= 1 && opts.MaxCount.UnwrapOr(1) > 0 { op.Value, op.Ok = me.ongoingUpstreamAugmentations[infoHash] if !op.Ok { op.Set(me.augmentPeersFromUpstream(req.InfoHash)) diff --git a/tracker/udp/server/server.go b/tracker/udp/server/server.go index d76dc424..716c3916 100644 --- a/tracker/udp/server/server.go +++ b/tracker/udp/server/server.go @@ -13,6 +13,7 @@ import ( "github.com/anacrolix/dht/v2/krpc" "github.com/anacrolix/generics" "github.com/anacrolix/log" + "go.opentelemetry.io/otel" "github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/tracker/udp" @@ -37,12 +38,16 @@ type Server struct { type RequestSourceAddr = net.Addr +var tracer = otel.Tracer("torrent.tracker.udp") + func (me *Server) HandleRequest( ctx context.Context, family udp.AddrFamily, source RequestSourceAddr, body []byte, ) error { + ctx, span := tracer.Start(ctx, "Server.HandleRequest") + defer span.End() var h udp.RequestHeader var r bytes.Reader r.Reset(body) -- 2.44.0