]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add some tracing
authorMatt Joiner <anacrolix@gmail.com>
Tue, 13 Dec 2022 04:28:34 +0000 (15:28 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 13 Dec 2022 04:28:34 +0000 (15:28 +1100)
tracker/server.go
tracker/udp/server/server.go

index f7d5ecb8beae4cb139fe0660932cc7af73728bca..0d5068d48bf2ffd19f08fd61c89039e8823acb88 100644 (file)
@@ -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))
index d76dc424b05fea4f5aa8f7b438e561216375f52c..716c3916a106e8b44575705fcb86640c7bdb5142 100644 (file)
@@ -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)