Skip to content

Commit eb17eb1

Browse files
authored
Revert "Adjust the DNS lookup duration metric (#93254)"
This reverts commit 946c524.
1 parent 946c524 commit eb17eb1

4 files changed

Lines changed: 25 additions & 58 deletions

File tree

src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public static string GetHostName()
2323
{
2424
name = NameResolutionPal.GetHostName();
2525
}
26-
catch (Exception ex) when (LogFailure(string.Empty, startingTimestamp, ex))
26+
catch when (LogFailure(string.Empty, startingTimestamp))
2727
{
2828
Debug.Fail("LogFailure should return false");
2929
throw;
3030
}
3131

32-
NameResolutionTelemetry.Log.AfterResolution(string.Empty, startingTimestamp);
32+
NameResolutionTelemetry.Log.AfterResolution(string.Empty, startingTimestamp, successful: true);
3333

3434
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(null, name);
3535
return name;
@@ -394,13 +394,13 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr
394394
Aliases = aliases
395395
};
396396
}
397-
catch (Exception ex) when (LogFailure(hostName, startingTimestamp, ex))
397+
catch when (LogFailure(hostName, startingTimestamp))
398398
{
399399
Debug.Fail("LogFailure should return false");
400400
throw;
401401
}
402402

403-
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp);
403+
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, successful: true);
404404

405405
return result;
406406
}
@@ -434,13 +434,13 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd
434434
}
435435
Debug.Assert(name != null);
436436
}
437-
catch (Exception ex) when (LogFailure(address, startingTimestamp, ex))
437+
catch when (LogFailure(address, startingTimestamp))
438438
{
439439
Debug.Fail("LogFailure should return false");
440440
throw;
441441
}
442442

443-
NameResolutionTelemetry.Log.AfterResolution(address, startingTimestamp);
443+
NameResolutionTelemetry.Log.AfterResolution(address, startingTimestamp, successful: true);
444444

445445
// Do the forward lookup to get the IPs for that host name
446446
startingTimestamp = NameResolutionTelemetry.Log.BeforeResolution(name);
@@ -464,13 +464,13 @@ private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAd
464464
AddressList = addresses
465465
};
466466
}
467-
catch (Exception ex) when (LogFailure(name, startingTimestamp, ex))
467+
catch when (LogFailure(name, startingTimestamp))
468468
{
469469
Debug.Fail("LogFailure should return false");
470470
throw;
471471
}
472472

473-
NameResolutionTelemetry.Log.AfterResolution(name, startingTimestamp);
473+
NameResolutionTelemetry.Log.AfterResolution(name, startingTimestamp, successful: true);
474474

475475
// One of three things happened:
476476
// 1. Success.
@@ -577,7 +577,7 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
577577
}
578578

579579
private static Task<T>? GetAddrInfoWithTelemetryAsync<T>(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken)
580-
where T : class
580+
where T : class
581581
{
582582
long startingTimestamp = Stopwatch.GetTimestamp();
583583
Task? task = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, addressFamily, cancellationToken);
@@ -594,19 +594,15 @@ private static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justR
594594
static async Task<T> CompleteAsync(Task task, string hostName, long startingTimestamp)
595595
{
596596
_ = NameResolutionTelemetry.Log.BeforeResolution(hostName);
597-
Exception? exception = null;
597+
T? result = null;
598598
try
599599
{
600-
return await ((Task<T>)task).ConfigureAwait(false);
601-
}
602-
catch (Exception ex)
603-
{
604-
exception = ex;
605-
throw;
600+
result = await ((Task<T>)task).ConfigureAwait(false);
601+
return result;
606602
}
607603
finally
608604
{
609-
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, exception);
605+
NameResolutionTelemetry.Log.AfterResolution(hostName, startingTimestamp, successful: result is not null);
610606
}
611607
}
612608
}
@@ -631,9 +627,9 @@ private static void ValidateHostName(string hostName)
631627
}
632628
}
633629

634-
private static bool LogFailure(object hostNameOrAddress, long? startingTimestamp, Exception exception)
630+
private static bool LogFailure(object hostNameOrAddress, long? startingTimestamp)
635631
{
636-
NameResolutionTelemetry.Log.AfterResolution(hostNameOrAddress, startingTimestamp, exception);
632+
NameResolutionTelemetry.Log.AfterResolution(hostNameOrAddress, startingTimestamp, successful: false);
637633
return false;
638634
}
639635

src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionMetrics.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,15 @@ internal static class NameResolutionMetrics
1313
private static readonly Meter s_meter = new("System.Net.NameResolution");
1414

1515
private static readonly Histogram<double> s_lookupDuration = s_meter.CreateHistogram<double>(
16-
name: "dns.lookup.duration",
16+
name: "dns.lookups.duration",
1717
unit: "s",
1818
description: "Measures the time taken to perform a DNS lookup.");
1919

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

22-
public static void AfterResolution(TimeSpan duration, string hostName, Exception? exception)
22+
public static void AfterResolution(TimeSpan duration, string hostName)
2323
{
24-
var hostNameTag = KeyValuePair.Create("dns.question.name", (object?)hostName);
25-
26-
if (exception is null)
27-
{
28-
s_lookupDuration.Record(duration.TotalSeconds, hostNameTag);
29-
}
30-
else
31-
{
32-
var errorTypeTag = KeyValuePair.Create("error.type", (object?)GetErrorType(exception));
33-
s_lookupDuration.Record(duration.TotalSeconds, hostNameTag, errorTypeTag);
34-
}
24+
s_lookupDuration.Record(duration.TotalSeconds, KeyValuePair.Create("dns.question.name", (object?)hostName));
3525
}
36-
37-
private static string GetErrorType(Exception exception) => (exception as SocketException)?.SocketErrorCode switch
38-
{
39-
SocketError.HostNotFound => "host_not_found",
40-
SocketError.TryAgain => "try_again",
41-
SocketError.AddressFamilyNotSupported => "address_family_not_supported",
42-
SocketError.NoRecovery => "no_recovery",
43-
44-
_ => exception.GetType().FullName!
45-
};
4626
}
4727
}

src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public long BeforeResolution(object hostNameOrAddress)
8181
}
8282

8383
[NonEvent]
84-
public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, Exception? exception = null)
84+
public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, bool successful)
8585
{
8686
Debug.Assert(startingTimestamp.HasValue);
8787
if (startingTimestamp == 0)
@@ -99,7 +99,7 @@ public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, E
9999

100100
if (IsEnabled(EventLevel.Informational, EventKeywords.None))
101101
{
102-
if (exception is not null)
102+
if (!successful)
103103
{
104104
ResolutionFailed();
105105
}
@@ -110,7 +110,7 @@ public void AfterResolution(object hostNameOrAddress, long? startingTimestamp, E
110110

111111
if (NameResolutionMetrics.IsEnabled())
112112
{
113-
NameResolutionMetrics.AfterResolution(duration, GetHostnameFromStateObject(hostNameOrAddress), exception);
113+
NameResolutionMetrics.AfterResolution(duration, GetHostnameFromStateObject(hostNameOrAddress));
114114
}
115115
}
116116

src/libraries/System.Net.NameResolution/tests/FunctionalTests/MetricsTest.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace System.Net.NameResolution.Tests
1414
{
1515
public class MetricsTest
1616
{
17-
private const string DnsLookupDuration = "dns.lookup.duration";
17+
private const string DnsLookupDuration = "dns.lookups.duration";
1818

1919
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
2020
public static void ResolveValidHostName_MetricsRecorded()
@@ -57,26 +57,17 @@ public static async Task ResolveInvalidHostName_MetricsRecorded()
5757
Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(Dns.BeginGetHostEntry(InvalidHostName, null, null)));
5858
Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostAddresses(Dns.BeginGetHostAddresses(InvalidHostName, null, null)));
5959

60-
double[] measurements = GetMeasurementsForHostname(recorder, InvalidHostName, "host_not_found");
60+
double[] measurements = GetMeasurementsForHostname(recorder, InvalidHostName);
6161

6262
Assert.Equal(6, measurements.Length);
6363
Assert.All(measurements, m => Assert.True(m > double.Epsilon));
6464
}
6565

66-
private static double[] GetMeasurementsForHostname(InstrumentRecorder<double> recorder, string hostname, string? expectedErrorType = null)
66+
private static double[] GetMeasurementsForHostname(InstrumentRecorder<double> recorder, string hostname)
6767
{
6868
return recorder
6969
.GetMeasurements()
70-
.Where(m =>
71-
{
72-
KeyValuePair<string, object?>[] tags = m.Tags.ToArray();
73-
if (!tags.Any(t => t.Key == "dns.question.name" && t.Value is string hostnameTag && hostnameTag == hostname))
74-
{
75-
return false;
76-
}
77-
string? actualErrorType = tags.FirstOrDefault(t => t.Key == "error.type").Value as string;
78-
return expectedErrorType == actualErrorType;
79-
})
70+
.Where(m => m.Tags.ToArray().Any(t => t.Key == "dns.question.name" && t.Value is string hostnameTag && hostnameTag == hostname))
8071
.Select(m => m.Value)
8172
.ToArray();
8273
}

0 commit comments

Comments
 (0)