Skip to content

Commit e1e38d2

Browse files
pass a context to logging.Tracer.NewConnectionTracer
This context has the same value attached to it as the context returned by Session.Context(). In the case of a dialed connection, this context is derived from the context used for dialing.
1 parent 4917760 commit e1e38d2

File tree

10 files changed

+54
-35
lines changed

10 files changed

+54
-35
lines changed

client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,17 @@ func dialContext(
203203
}
204204
c.packetHandlers = packetHandlers
205205

206+
c.tracingID = nextSessionTracingID()
206207
if c.config.Tracer != nil {
207-
c.tracer = c.config.Tracer.TracerForConnection(protocol.PerspectiveClient, c.destConnID)
208+
c.tracer = c.config.Tracer.TracerForConnection(
209+
context.WithValue(ctx, SessionTracingKey, c.tracingID),
210+
protocol.PerspectiveClient,
211+
c.destConnID,
212+
)
208213
}
209214
if c.tracer != nil {
210215
c.tracer.StartedConnection(c.conn.LocalAddr(), c.conn.RemoteAddr(), c.srcConnID, c.destConnID)
211216
}
212-
c.tracingID = nextSessionTracingID()
213217
if err := c.dial(ctx); err != nil {
214218
return nil, err
215219
}

client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ = Describe("Client", func() {
5454
originalClientSessConstructor = newClientSession
5555
tracer = mocklogging.NewMockConnectionTracer(mockCtrl)
5656
tr := mocklogging.NewMockTracer(mockCtrl)
57-
tr.EXPECT().TracerForConnection(protocol.PerspectiveClient, gomock.Any()).Return(tracer).MaxTimes(1)
57+
tr.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveClient, gomock.Any()).Return(tracer).MaxTimes(1)
5858
config = &Config{Tracer: tr, Versions: []protocol.VersionNumber{protocol.VersionTLS}}
5959
Eventually(areSessionsRunning).Should(BeFalse())
6060
// sess = NewMockQuicSession(mockCtrl)

internal/mocks/logging/tracer.go

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

logging/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package logging
44

55
import (
6+
"context"
67
"net"
78
"time"
89

@@ -95,7 +96,7 @@ type Tracer interface {
9596
// The ODCID is the original destination connection ID:
9697
// The destination connection ID that the client used on the first Initial packet it sent on this connection.
9798
// If nil is returned, tracing will be disabled for this connection.
98-
TracerForConnection(p Perspective, odcid ConnectionID) ConnectionTracer
99+
TracerForConnection(ctx context.Context, p Perspective, odcid ConnectionID) ConnectionTracer
99100

100101
SentPacket(net.Addr, *Header, ByteCount, []Frame)
101102
DroppedPacket(net.Addr, PacketType, ByteCount, PacketDropReason)

logging/mock_tracer_test.go

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

logging/multiplex.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logging
22

33
import (
4+
"context"
45
"net"
56
"time"
67
)
@@ -22,10 +23,10 @@ func NewMultiplexedTracer(tracers ...Tracer) Tracer {
2223
return &tracerMultiplexer{tracers}
2324
}
2425

25-
func (m *tracerMultiplexer) TracerForConnection(p Perspective, odcid ConnectionID) ConnectionTracer {
26+
func (m *tracerMultiplexer) TracerForConnection(ctx context.Context, p Perspective, odcid ConnectionID) ConnectionTracer {
2627
var connTracers []ConnectionTracer
2728
for _, t := range m.tracers {
28-
if ct := t.TracerForConnection(p, odcid); ct != nil {
29+
if ct := t.TracerForConnection(ctx, p, odcid); ct != nil {
2930
connTracers = append(connTracers, ct)
3031
}
3132
}

logging/multiplex_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logging
22

33
import (
4+
"context"
45
"net"
56
"time"
67

@@ -35,35 +36,39 @@ var _ = Describe("Tracing", func() {
3536
})
3637

3738
It("multiplexes the TracerForConnection call", func() {
38-
tr1.EXPECT().TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})
39-
tr2.EXPECT().TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})
40-
tracer.TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})
39+
ctx := context.Background()
40+
tr1.EXPECT().TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})
41+
tr2.EXPECT().TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})
42+
tracer.TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})
4143
})
4244

4345
It("uses multiple connection tracers", func() {
46+
ctx := context.Background()
4447
ctr1 := NewMockConnectionTracer(mockCtrl)
4548
ctr2 := NewMockConnectionTracer(mockCtrl)
46-
tr1.EXPECT().TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr1)
47-
tr2.EXPECT().TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr2)
48-
tr := tracer.TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3})
49+
tr1.EXPECT().TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr1)
50+
tr2.EXPECT().TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr2)
51+
tr := tracer.TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3})
4952
ctr1.EXPECT().LossTimerCanceled()
5053
ctr2.EXPECT().LossTimerCanceled()
5154
tr.LossTimerCanceled()
5255
})
5356

5457
It("handles tracers that return a nil ConnectionTracer", func() {
58+
ctx := context.Background()
5559
ctr1 := NewMockConnectionTracer(mockCtrl)
56-
tr1.EXPECT().TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr1)
57-
tr2.EXPECT().TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3})
58-
tr := tracer.TracerForConnection(PerspectiveServer, ConnectionID{1, 2, 3})
60+
tr1.EXPECT().TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3}).Return(ctr1)
61+
tr2.EXPECT().TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3})
62+
tr := tracer.TracerForConnection(ctx, PerspectiveServer, ConnectionID{1, 2, 3})
5963
ctr1.EXPECT().LossTimerCanceled()
6064
tr.LossTimerCanceled()
6165
})
6266

6367
It("returns nil when all tracers return a nil ConnectionTracer", func() {
64-
tr1.EXPECT().TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})
65-
tr2.EXPECT().TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})
66-
Expect(tracer.TracerForConnection(PerspectiveClient, ConnectionID{1, 2, 3})).To(BeNil())
68+
ctx := context.Background()
69+
tr1.EXPECT().TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})
70+
tr2.EXPECT().TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})
71+
Expect(tracer.TracerForConnection(ctx, PerspectiveClient, ConnectionID{1, 2, 3})).To(BeNil())
6772
})
6873

6974
It("traces the PacketSent event", func() {

qlog/qlog.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package qlog
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"io"
78
"log"
@@ -59,7 +60,7 @@ func NewTracer(getLogWriter func(p logging.Perspective, connectionID []byte) io.
5960
return &tracer{getLogWriter: getLogWriter}
6061
}
6162

62-
func (t *tracer) TracerForConnection(p logging.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
63+
func (t *tracer) TracerForConnection(_ context.Context, p logging.Perspective, odcid protocol.ConnectionID) logging.ConnectionTracer {
6364
if w := t.getLogWriter(p, odcid.Bytes()); w != nil {
6465
return NewConnectionTracer(w, p, odcid)
6566
}

server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ func (s *baseServer) handleInitialImpl(p *receivedPacket, hdr *wire.Header) erro
451451
}
452452
s.logger.Debugf("Changing connection ID to %s.", connID)
453453
var sess quicSession
454+
tracingID := nextSessionTracingID()
454455
if added := s.sessionHandler.AddWithConnID(hdr.DestConnectionID, connID, func() packetHandler {
455456
var tracer logging.ConnectionTracer
456457
if s.config.Tracer != nil {
@@ -459,7 +460,11 @@ func (s *baseServer) handleInitialImpl(p *receivedPacket, hdr *wire.Header) erro
459460
if origDestConnID.Len() > 0 {
460461
connID = origDestConnID
461462
}
462-
tracer = s.config.Tracer.TracerForConnection(protocol.PerspectiveServer, connID)
463+
tracer = s.config.Tracer.TracerForConnection(
464+
context.WithValue(context.Background(), SessionTracingKey, tracingID),
465+
protocol.PerspectiveServer,
466+
connID,
467+
)
463468
}
464469
sess = s.newSession(
465470
newSendConn(s.conn, p.remoteAddr, p.info),
@@ -475,7 +480,7 @@ func (s *baseServer) handleInitialImpl(p *receivedPacket, hdr *wire.Header) erro
475480
s.tokenGenerator,
476481
s.acceptEarlySessions,
477482
tracer,
478-
nextSessionTracingID(),
483+
tracingID,
479484
s.logger,
480485
hdr.Version,
481486
)

server_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ var _ = Describe("Server", func() {
322322
fn()
323323
return true
324324
})
325-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, protocol.ConnectionID{0xde, 0xad, 0xc0, 0xde})
325+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, protocol.ConnectionID{0xde, 0xad, 0xc0, 0xde})
326326
sess := NewMockQuicSession(mockCtrl)
327327
serv.newSession = func(
328328
_ sendConn,
@@ -579,7 +579,7 @@ var _ = Describe("Server", func() {
579579
fn()
580580
return true
581581
})
582-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
582+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
583583

584584
sess := NewMockQuicSession(mockCtrl)
585585
serv.newSession = func(
@@ -637,7 +637,7 @@ var _ = Describe("Server", func() {
637637
fn()
638638
return true
639639
}).AnyTimes()
640-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, gomock.Any()).AnyTimes()
640+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, gomock.Any()).AnyTimes()
641641

642642
serv.config.AcceptToken = func(net.Addr, *Token) bool { return true }
643643
acceptSession := make(chan struct{})
@@ -760,7 +760,7 @@ var _ = Describe("Server", func() {
760760
fn()
761761
return true
762762
}).Times(protocol.MaxAcceptQueueSize)
763-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, gomock.Any()).Times(protocol.MaxAcceptQueueSize)
763+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, gomock.Any()).Times(protocol.MaxAcceptQueueSize)
764764

765765
var wg sync.WaitGroup
766766
wg.Add(protocol.MaxAcceptQueueSize)
@@ -832,7 +832,7 @@ var _ = Describe("Server", func() {
832832
fn()
833833
return true
834834
})
835-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, gomock.Any())
835+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, gomock.Any())
836836

837837
serv.handlePacket(p)
838838
// make sure there are no Write calls on the packet conn
@@ -940,7 +940,7 @@ var _ = Describe("Server", func() {
940940
fn()
941941
return true
942942
})
943-
tracer.EXPECT().TracerForConnection(protocol.PerspectiveServer, gomock.Any())
943+
tracer.EXPECT().TracerForConnection(gomock.Any(), protocol.PerspectiveServer, gomock.Any())
944944
serv.handleInitialImpl(
945945
&receivedPacket{buffer: getPacketBuffer()},
946946
&wire.Header{DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}},

0 commit comments

Comments
 (0)