From 58232e0087f2cb40c903608458180c6e6a5505e6 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 30 Jul 2021 05:52:48 -0700 Subject: [PATCH] Account for AddressFamily pairs in NameResolution telemetry (#56614) --- .../src/System/Net/NameResolutionTelemetry.cs | 70 +++++-------------- 1 file changed, 16 insertions(+), 54 deletions(-) diff --git a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs index 1d837b3ab1a..a5405f02d47 100644 --- a/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs +++ b/src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs @@ -1,10 +1,10 @@ // 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.CodeAnalysis; using System.Diagnostics.Tracing; -using System.Runtime.InteropServices; +using System.Net.Sockets; using System.Threading; using Microsoft.Extensions.Internal; @@ -50,8 +50,6 @@ namespace System.Net } } - private const int MaxIPFormattedLength = 128; - [Event(ResolutionStartEventId, Level = EventLevel.Informational)] private void ResolutionStart(string hostNameOrAddress) => WriteEvent(ResolutionStartEventId, hostNameOrAddress); @@ -66,7 +64,12 @@ namespace System.Net public ValueStopwatch BeforeResolution(object hostNameOrAddress) { Debug.Assert(hostNameOrAddress != null); - Debug.Assert(hostNameOrAddress is string || hostNameOrAddress is IPAddress); + Debug.Assert( + hostNameOrAddress is string || + hostNameOrAddress is IPAddress || + hostNameOrAddress is KeyValuePair || + hostNameOrAddress is KeyValuePair, + $"Unknown hostNameOrAddress type: {hostNameOrAddress.GetType().Name}"); if (IsEnabled()) { @@ -75,14 +78,15 @@ namespace System.Net if (IsEnabled(EventLevel.Informational, EventKeywords.None)) { - if (hostNameOrAddress is string s) - { - ResolutionStart(s); - } - else + string host = hostNameOrAddress switch { - WriteEvent(ResolutionStartEventId, FormatIPAddressNullTerminated((IPAddress)hostNameOrAddress, stackalloc char[MaxIPFormattedLength])); - } + string h => h, + KeyValuePair t => t.Key, + IPAddress a => a.ToString(), + KeyValuePair t => t.Key.ToString(), + _ => null! + }; + ResolutionStart(host); } return ValueStopwatch.StartNew(); @@ -111,47 +115,5 @@ namespace System.Net } } } - - [NonEvent] - private static Span FormatIPAddressNullTerminated(IPAddress address, Span destination) - { - Debug.Assert(address != null); - - bool success = address.TryFormat(destination, out int charsWritten); - Debug.Assert(success); - - Debug.Assert(charsWritten < destination.Length); - destination[charsWritten] = '\0'; - - return destination.Slice(0, charsWritten + 1); - } - - - // WriteEvent overloads taking Span are imitating string arguments - // Span arguments are expected to be null-terminated - -#if !ES_BUILD_STANDALONE - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", - Justification = "Parameters to this method are primitive and are trimmer safe")] -#endif - [NonEvent] - private unsafe void WriteEvent(int eventId, Span arg1) - { - Debug.Assert(!arg1.IsEmpty && arg1.IndexOf('\0') == arg1.Length - 1, "Expecting a null-terminated ROS"); - - if (IsEnabled()) - { - fixed (char* arg1Ptr = &MemoryMarshal.GetReference(arg1)) - { - EventData descr = new EventData - { - DataPointer = (IntPtr)(arg1Ptr), - Size = arg1.Length * sizeof(char) - }; - - WriteEventCore(eventId, eventDataCount: 1, &descr); - } - } - } } } -- 2.34.1