webtorrent/otel.go | 1 + webtorrent/transport.go | 14 ++++++++++---- diff --git a/webtorrent/otel.go b/webtorrent/otel.go index 3c40b32a0bea16f923e32f713e4f0cd89b9e4d0d..d939c3f83401d7ac6c36cf039d2d54d8079fdc47 100644 --- a/webtorrent/otel.go +++ b/webtorrent/otel.go @@ -13,6 +13,7 @@ webrtcConnTypeKey = "webtorrent.webrtc.conn.type" ) func dataChannelStarted(peerConnectionCtx context.Context, dc *webrtc.DataChannel) (dataChannelCtx context.Context, span trace.Span) { + trace.SpanFromContext(peerConnectionCtx).AddEvent("starting data channel") dataChannelCtx, span = otel.Tracer(tracerName).Start(peerConnectionCtx, "DataChannel") dc.OnClose(func() { span.End() diff --git a/webtorrent/transport.go b/webtorrent/transport.go index 9cff003e21166f58b098acb1278bca517581c6be..561a6b9fb5c98783dc5bd29bc629c64b49227660 100644 --- a/webtorrent/transport.go +++ b/webtorrent/transport.go @@ -169,25 +169,31 @@ pc *wrappedPeerConnection, onOpen func(closer datachannel.ReadWriteCloser), ) { dc.OnOpen(func() { - trace.SpanFromContext(ctx).AddEvent("opened") + dataChannelSpan := trace.SpanFromContext(ctx) + dataChannelSpan.AddEvent("opened") raw, err := dc.Detach() if err != nil { // This shouldn't happen if the API is configured correctly, and we call from OnOpen. panic(err) } //dc.OnClose() - onOpen(hookDataChannelCloser(raw, pc)) + onOpen(hookDataChannelCloser(raw, pc, dataChannelSpan)) }) } // Hooks the datachannel's Close to Close the owning PeerConnection. The datachannel takes ownership // and responsibility for the PeerConnection. -func hookDataChannelCloser(dcrwc datachannel.ReadWriteCloser, pc *wrappedPeerConnection) datachannel.ReadWriteCloser { +func hookDataChannelCloser(dcrwc datachannel.ReadWriteCloser, pc *wrappedPeerConnection, dataChannelSpan trace.Span) datachannel.ReadWriteCloser { return struct { datachannelReadWriter io.Closer }{ dcrwc, - ioCloserFunc(pc.Close), + ioCloserFunc(func() error { + dcrwc.Close() + pc.Close() + dataChannelSpan.End() + return nil + }), } }