Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket's connect operations tracing #38620

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Log.IsEnabled check made more limker trimming-friendly
  • Loading branch information
alnikola committed Jul 13, 2020
commit 245458e2fba8495b6f8b0d406dc511d4f4fdb8b7
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ private bool CanUseConnectEx(EndPoint remoteEP)

internal IAsyncResult UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback? callback, object? state, bool flowContext = false)
{
if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStart(remoteEP);
if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStart(remoteEP);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style-wise I've always found it preferable to eliminate these IsEnabled() calls entirely and rely on the checks that occur inside the EventSource, but I think the mono-linker will fully remove this pattern so its purely style and tiny perf at this point. Style is best decided by the folks who have to work on the code rather than me : )


if (CanUseConnectEx(remoteEP))
{
Expand Down Expand Up @@ -2427,7 +2427,7 @@ asyncResult as MultipleAddressConnectAsyncResult ??
Exception? ex = castedAsyncResult.Result as Exception;
if (ex != null || (SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
{
if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed();
if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed();

if (ex == null)
{
Expand All @@ -2443,7 +2443,7 @@ asyncResult as MultipleAddressConnectAsyncResult ??
ExceptionDispatchInfo.Throw(ex);
}

if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop();
if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStop();

if (NetEventSource.IsEnabled)
{
Expand Down Expand Up @@ -4345,7 +4345,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
if (SocketsTelemetry.IsEnabled(EventLevel.Error)) SocketsTelemetry.Log.ConnectFailed();
if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectFailed();

UpdateConnectSocketErrorForDisposed(ref errorCode);
// Update the internal state of this socket according to the error before throwing.
Expand All @@ -4355,7 +4355,7 @@ private void DoConnect(EndPoint endPointSnapshot, Internals.SocketAddress socket
throw socketException;
}

if (SocketsTelemetry.IsEnabled(EventLevel.Informational)) SocketsTelemetry.Log.ConnectStop();
if (SocketsTelemetry.Log.IsEnabled()) SocketsTelemetry.Log.ConnectStop();
alnikola marked this conversation as resolved.
Show resolved Hide resolved

if (_rightEndPoint == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ internal sealed class SocketsTelemetry : EventSource
{
public static readonly SocketsTelemetry Log = new SocketsTelemetry();

[NonEvent]
public static bool IsEnabled(EventLevel level)
{
return Log.IsEnabled(level, EventKeywords.All);
}

[NonEvent]
public void ConnectStart(Internals.SocketAddress address)
{
Expand All @@ -32,7 +26,10 @@ public void ConnectStart(EndPoint address)
[Event(1, Level = EventLevel.Informational)]
public void ConnectStart(string? address)
{
WriteEvent(eventId: 1, address ?? "");
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
WriteEvent(eventId: 1, address ?? "");
}
}

[Event(2, Level = EventLevel.Informational)]
Expand All @@ -44,21 +41,30 @@ public void ConnectStop()
[Event(3, Level = EventLevel.Error)]
public void ConnectFailed()
alnikola marked this conversation as resolved.
Show resolved Hide resolved
{
WriteEvent(eventId: 3);
ConnectStopInternal();
if (IsEnabled(EventLevel.Error, EventKeywords.All))
{
WriteEvent(eventId: 3);
ConnectStopInternal();
}
}

[Event(4, Level = EventLevel.Warning)]
public void ConnectCanceled()
{
alnikola marked this conversation as resolved.
Show resolved Hide resolved
WriteEvent(eventId: 4);
ConnectStopInternal();
if (IsEnabled(EventLevel.Warning, EventKeywords.All))
{
WriteEvent(eventId: 4);
ConnectStopInternal();
}
}

[NonEvent]
private void ConnectStopInternal()
{
WriteEvent(eventId: 2);
if (IsEnabled(EventLevel.Informational, EventKeywords.All))
{
WriteEvent(eventId: 2);
}
}
}
}
alnikola marked this conversation as resolved.
Show resolved Hide resolved