Skip to content

Commit

Permalink
make quic tracing easir to correlate with MsQuic (#55483)
Browse files Browse the repository at this point in the history
  • Loading branch information
wfurt committed Jul 12, 2021
1 parent 6ece5d3 commit d766727
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void RemoveStream(MsQuicStream? stream)

if (releaseHandles)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"{TraceId()} releasing handle after last stream.");
Handle?.Dispose();
}
}
Expand Down Expand Up @@ -123,8 +124,15 @@ public void SetClosing()
_closing = true;
}
}

internal string TraceId()
{
return $"[MsQuicConnection#{this.GetHashCode()}/{Handle?.DangerousGetHandle():x}]";
}
}

internal string TraceId() => _state.TraceId();

// constructor for inbound connections
public MsQuicConnection(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, SafeMsQuicConnectionHandle handle, bool remoteCertificateRequired = false, X509RevocationMode revocationMode = X509RevocationMode.Offline, RemoteCertificateValidationCallback? remoteCertificateValidationCallback = null)
{
Expand Down Expand Up @@ -162,7 +170,7 @@ public MsQuicConnection(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, Saf

if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(_state, $"[Connection#{_state.GetHashCode()}] inbound connection created");
NetEventSource.Info(_state, $"{TraceId()} inbound connection created");
}
}

Expand Down Expand Up @@ -201,7 +209,7 @@ public MsQuicConnection(QuicClientConnectionOptions options)

if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(_state, $"[Connection#{_state.GetHashCode()}] outbound connection created");
NetEventSource.Info(_state, $"{TraceId()} outbound connection created");
}
}

Expand Down Expand Up @@ -621,7 +629,7 @@ internal void SetNegotiatedAlpn(IntPtr alpn, int alpnLength)

if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(state, $"[Connection#{state.GetHashCode()}] received event {connectionEvent.Type}");
NetEventSource.Info(state, $"{state.TraceId()} received event {connectionEvent.Type}");
}

try
Expand Down Expand Up @@ -697,6 +705,8 @@ private void Dispose(bool disposing)
return;
}

if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(_state, $"{TraceId()} disposing {disposing}");

bool releaseHandles = false;
lock (_state)
{
Expand All @@ -716,6 +726,8 @@ private void Dispose(bool disposing)
_configuration?.Dispose();
if (releaseHandles)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(_state, $"{TraceId()} releasing handle");

// We may not be fully initialized if constructor fails.
_state.Handle?.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ private sealed class State

public void Cleanup()
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"{TraceId()} releasing handles.");

ShutdownState = ShutdownState.Finished;
CleanupSendState(this);
Handle?.Dispose();
Expand All @@ -77,8 +79,15 @@ public void Cleanup()
if (StateGCHandle.IsAllocated) StateGCHandle.Free();
ConnectionState?.RemoveStream(null);
}

internal string TraceId()
{
return $"[MsQuicStream#{this.GetHashCode()}/{Handle?.DangerousGetHandle():x}]";
}
}

internal string TraceId() => _state.TraceId();

// inbound.
internal MsQuicStream(MsQuicConnection.State connectionState, SafeMsQuicStreamHandle streamHandle, QUIC_STREAM_OPEN_FLAGS flags)
{
Expand Down Expand Up @@ -117,8 +126,8 @@ internal MsQuicStream(MsQuicConnection.State connectionState, SafeMsQuicStreamHa
{
NetEventSource.Info(
_state,
$"[Stream#{_state.GetHashCode()}] inbound {(flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL) ? "uni" : "bi")}directional stream created " +
$"in Connection#{_state.ConnectionState.GetHashCode()}.");
$"{TraceId()} inbound {(flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL) ? "uni" : "bi")}directional stream created " +
$"in {_state.ConnectionState.TraceId()}.");
}
}

Expand Down Expand Up @@ -170,8 +179,8 @@ internal MsQuicStream(MsQuicConnection.State connectionState, QUIC_STREAM_OPEN_F
{
NetEventSource.Info(
_state,
$"[Stream#{_state.GetHashCode()}] outbound {(flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL) ? "uni" : "bi")}directional stream created " +
$"in Connection#{_state.ConnectionState.GetHashCode()}.");
$"{TraceId()} outbound {(flags.HasFlag(QUIC_STREAM_OPEN_FLAGS.UNIDIRECTIONAL) ? "uni" : "bi")}directional stream created " +
$"in {_state.ConnectionState.TraceId()}.");
}
}

Expand Down Expand Up @@ -647,6 +656,9 @@ private void Dispose(bool disposing)
return;
}


if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(_state, $"{TraceId()} disposing {disposing}");

bool callShutdown = false;
bool abortRead = false;
bool releaseHandles = false;
Expand Down Expand Up @@ -698,10 +710,7 @@ private void Dispose(bool disposing)
_state.Cleanup();
}

if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(_state, $"[Stream#{_state.GetHashCode()}] disposed");
}
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(_state, $"{TraceId()} disposed");
}

private void EnableReceive()
Expand All @@ -726,7 +735,7 @@ private static uint HandleEvent(State state, ref StreamEvent evt)
{
if (NetEventSource.Log.IsEnabled())
{
NetEventSource.Info(state, $"[Stream#{state.GetHashCode()}] received event {evt.Type}");
NetEventSource.Info(state, $"{state.TraceId()} received event {evt.Type}");
}

try
Expand Down

0 comments on commit d766727

Please sign in to comment.