-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add System.Net.NameResolution metrics #88773
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b94dd5a
Add System.Net.NameResolution metrics
MihaZupan c46040a
Avoid potential race condition in test
MihaZupan 5ae7ed5
Fix metrics counter firing twice when called from RunAsync without te…
MihaZupan 60981e0
Add an extra assert
MihaZupan 9875241
More RemoteExecutor
MihaZupan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionMetrics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Net.Sockets; | ||
|
||
namespace System.Net | ||
{ | ||
internal static class NameResolutionMetrics | ||
{ | ||
private static readonly Meter s_meter = new("System.Net.NameResolution"); | ||
|
||
private static readonly Counter<long> s_lookupsRequestedCounter = s_meter.CreateCounter<long>( | ||
name: "dns-lookups-requested", | ||
description: "Number of DNS lookups requested."); | ||
|
||
public static bool IsEnabled() => s_lookupsRequestedCounter.Enabled; | ||
|
||
public static void BeforeResolution(object hostNameOrAddress, out string? host) | ||
{ | ||
if (s_lookupsRequestedCounter.Enabled) | ||
{ | ||
host = GetHostnameFromStateObject(hostNameOrAddress); | ||
|
||
s_lookupsRequestedCounter.Add(1, KeyValuePair.Create("hostname", (object?)host)); | ||
} | ||
else | ||
{ | ||
host = null; | ||
} | ||
} | ||
|
||
public static string GetHostnameFromStateObject(object hostNameOrAddress) | ||
{ | ||
Debug.Assert(hostNameOrAddress is not null); | ||
|
||
string host = hostNameOrAddress switch | ||
{ | ||
string h => h, | ||
KeyValuePair<string, AddressFamily> t => t.Key, | ||
IPAddress a => a.ToString(), | ||
KeyValuePair<IPAddress, AddressFamily> t => t.Key.ToString(), | ||
_ => null! | ||
}; | ||
|
||
Debug.Assert(host is not null, $"Unknown hostNameOrAddress type: {hostNameOrAddress.GetType().Name}"); | ||
|
||
return host; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/libraries/System.Net.NameResolution/tests/FunctionalTests/MetricsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace System.Net.NameResolution.Tests | ||
{ | ||
public class MetricsTest | ||
{ | ||
private const string DnsLookupsRequested = "dns-lookups-requested"; | ||
|
||
[Fact] | ||
public static async Task ResolveValidHostName_MetricsRecorded() | ||
{ | ||
const string ValidHostName = "localhost"; | ||
|
||
using var recorder = new InstrumentRecorder<long>(DnsLookupsRequested); | ||
|
||
await Dns.GetHostEntryAsync(ValidHostName); | ||
await Dns.GetHostAddressesAsync(ValidHostName); | ||
|
||
Dns.GetHostEntry(ValidHostName); | ||
Dns.GetHostAddresses(ValidHostName); | ||
|
||
Dns.EndGetHostEntry(Dns.BeginGetHostEntry(ValidHostName, null, null)); | ||
Dns.EndGetHostAddresses(Dns.BeginGetHostAddresses(ValidHostName, null, null)); | ||
|
||
long[] measurements = GetMeasurementsForHostname(recorder, ValidHostName); | ||
|
||
// >= 6 because other tests running in parallel may have also resolved the same host. | ||
MihaZupan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.True(measurements.Length >= 6, $"Was {measurements.Length}"); | ||
Assert.All(measurements, m => Assert.Equal(1, m)); | ||
} | ||
|
||
[Fact] | ||
public static async Task ResolveInvalidHostName_MetricsRecorded() | ||
{ | ||
const string InvalidHostName = $"invalid...example.com...{nameof(ResolveInvalidHostName_MetricsRecorded)}"; | ||
|
||
using var recorder = new InstrumentRecorder<long>(DnsLookupsRequested); | ||
|
||
await Assert.ThrowsAnyAsync<SocketException>(async () => await Dns.GetHostEntryAsync(InvalidHostName)); | ||
await Assert.ThrowsAnyAsync<SocketException>(async () => await Dns.GetHostAddressesAsync(InvalidHostName)); | ||
|
||
Assert.ThrowsAny<SocketException>(() => Dns.GetHostEntry(InvalidHostName)); | ||
Assert.ThrowsAny<SocketException>(() => Dns.GetHostAddresses(InvalidHostName)); | ||
|
||
Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostEntry(Dns.BeginGetHostEntry(InvalidHostName, null, null))); | ||
Assert.ThrowsAny<SocketException>(() => Dns.EndGetHostAddresses(Dns.BeginGetHostAddresses(InvalidHostName, null, null))); | ||
|
||
long[] measurements = GetMeasurementsForHostname(recorder, InvalidHostName); | ||
|
||
Assert.Equal(6, measurements.Length); | ||
Assert.All(measurements, m => Assert.Equal(1, m)); | ||
} | ||
|
||
private static long[] GetMeasurementsForHostname(InstrumentRecorder<long> recorder, string hostname) | ||
{ | ||
return recorder | ||
.GetMeasurements() | ||
.Where(m => m.Tags.ToArray().Any(t => t.Key == "hostname" && t.Value is string hostnameTag && hostnameTag == hostname)) | ||
.Select(m => m.Value) | ||
.ToArray(); | ||
} | ||
|
||
private sealed class InstrumentRecorder<T> : IDisposable where T : struct | ||
{ | ||
private readonly MeterListener _meterListener = new(); | ||
private readonly List<Measurement<T>> _values = new(); | ||
|
||
public InstrumentRecorder(string instrumentName) | ||
{ | ||
_meterListener.InstrumentPublished = (instrument, listener) => | ||
{ | ||
if (instrument.Meter.Name == "System.Net.NameResolution" && instrument.Name == instrumentName) | ||
{ | ||
listener.EnableMeasurementEvents(instrument); | ||
} | ||
}; | ||
_meterListener.SetMeasurementEventCallback<T>(OnMeasurementRecorded); | ||
_meterListener.Start(); | ||
} | ||
|
||
private void OnMeasurementRecorded(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state) => _values.Add(new Measurement<T>(measurement, tags)); | ||
public IReadOnlyList<Measurement<T>> GetMeasurements() => _values.ToArray(); | ||
public void Dispose() => _meterListener.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @noahfalk to figure out the naming, since this is establishing a precedent. Should it be
dns.lookups.requested
?(The instruments in my pr are to be renamed.)