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

Adjust the DNS lookup duration metric #93254

Merged
merged 5 commits into from
Oct 12, 2023
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
27 changes: 8 additions & 19 deletions src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static string GetHostName()
throw;
}

NameResolutionTelemetry.Log.AfterResolution(string.Empty, startingTimestamp, errorType: null);
NameResolutionTelemetry.Log.AfterResolution(string.Empty, startingTimestamp);

if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, name);
return name;
Expand Down Expand Up @@ -400,7 +400,7 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr
throw;
}

NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, errorType: null);
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp);

return result;
}
Expand Down Expand Up @@ -440,7 +440,7 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd
throw;
}

NameResolutionTelemetry.Log.AfterResolution(address, startingTimestamp, errorType: null);
NameResolutionTelemetry.Log.AfterResolution(address, startingTimestamp);

// Do the forward lookup to get the IPs for that host name
startingTimestamp = NameResolutionTelemetry.Log.BeforeResolution(name);
Expand Down Expand Up @@ -470,7 +470,7 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd
throw;
}

NameResolutionTelemetry.Log.AfterResolution(name, startingTimestamp, errorType: null);
NameResolutionTelemetry.Log.AfterResolution(name, startingTimestamp);

// One of three things happened:
// 1. Success.
Expand Down Expand Up @@ -594,33 +594,23 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
static async Task<T> CompleteAsync(Task task, string hostName, long startingTimestamp)
{
_ = NameResolutionTelemetry.Log.BeforeResolution(hostName);
string? errorType = null;
Exception? exception = null;
try
{
return await ((Task<T>)task).ConfigureAwait(false);
}
catch (Exception ex)
{
errorType = GetErrorType(ex);
exception = ex;
throw;
}
finally
{
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, errorType);
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, exception);
}
}
}

private static string GetErrorType(Exception exception) => (exception as SocketException)?.SocketErrorCode switch
{
SocketError.HostNotFound => "host_not_found",
SocketError.TryAgain => "try_again",
SocketError.AddressFamilyNotSupported => "address_family_not_supported",
SocketError.NoRecovery => "no_recovery",

_ => exception.GetType().Name
};

private static IPHostEntry CreateHostEntryForAddress(IPAddress address) =>
new IPHostEntry
{
Expand All @@ -643,8 +633,7 @@ private static void ValidateHostName(string hostName)

private static bool LogFailure(object hostNameOrAddress, long? startingTimestamp, Exception exception)
{
string errorType = GetErrorType(exception);
NameResolutionTelemetry.Log.AfterResolution(hostNameOrAddress, startingTimestamp, errorType);
NameResolutionTelemetry.Log.AfterResolution(hostNameOrAddress, startingTimestamp, exception);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ internal static class NameResolutionMetrics

public static bool IsEnabled() => s_lookupDuration.Enabled;

public static void AfterResolution(TimeSpan duration, string hostName, string? errorType)
public static void AfterResolution(TimeSpan duration, string hostName, Exception? exception)
{
var hostNameTag = KeyValuePair.Create("dns.question.name", (object?)hostName);

if (errorType is null)
if (exception is null)
{
s_lookupDuration.Record(duration.TotalSeconds, hostNameTag);
}
else
{
var errorTypeTag = KeyValuePair.Create("error.type", (object?)errorType);
var errorTypeTag = KeyValuePair.Create("error.type", (object?)GetErrorType(exception));
s_lookupDuration.Record(duration.TotalSeconds, hostNameTag, errorTypeTag);
}
}

private static string GetErrorType(Exception exception) => (exception as SocketException)?.SocketErrorCode switch
{
SocketError.HostNotFound => "host_not_found",
SocketError.TryAgain => "try_again",
SocketError.AddressFamilyNotSupported => "address_family_not_supported",
SocketError.NoRecovery => "no_recovery",

_ => exception.GetType().Name
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public long BeforeResolution(object hostNameOrAddress)
}

[NonEvent]
public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, string? errorType)
public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, Exception? exception = null)
{
Debug.Assert(startingTimestamp.HasValue);
if (startingTimestamp == 0)
Expand All @@ -99,7 +99,7 @@ public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, s

if (IsEnabled(EventLevel.Informational, EventKeywords.None))
{
if (errorType is not null)
if (exception is not null)
{
ResolutionFailed();
}
Expand All @@ -110,7 +110,7 @@ public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, s

if (NameResolutionMetrics.IsEnabled())
{
NameResolutionMetrics.AfterResolution(duration, GetHostnameFromStateObject(hostNameOrAddress), errorType);
NameResolutionMetrics.AfterResolution(duration, GetHostnameFromStateObject(hostNameOrAddress), exception);
}
}

Expand Down
Loading