Account for AddressFamily pairs in NameResolution telemetry (#56614)
authorMiha Zupan <mihazupan.zupan1@gmail.com>
Fri, 30 Jul 2021 12:52:48 +0000 (05:52 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Jul 2021 12:52:48 +0000 (05:52 -0700)
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionTelemetry.cs

index 1d837b3ab1a840258824911f216c0380b7f2359f..a5405f02d474f5298174b48f70ab18bee6b1f665 100644 (file)
@@ -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<string, AddressFamily> ||
+                hostNameOrAddress is KeyValuePair<IPAddress, AddressFamily>,
+                $"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<string, AddressFamily> t => t.Key,
+                        IPAddress a => a.ToString(),
+                        KeyValuePair<IPAddress, AddressFamily> t => t.Key.ToString(),
+                        _ => null!
+                    };
+                    ResolutionStart(host);
                 }
 
                 return ValueStopwatch.StartNew();
@@ -111,47 +115,5 @@ namespace System.Net
                 }
             }
         }
-
-        [NonEvent]
-        private static Span<char> FormatIPAddressNullTerminated(IPAddress address, Span<char> 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<char> 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<char> arg1)
-        {
-            Debug.Assert(!arg1.IsEmpty && arg1.IndexOf('\0') == arg1.Length - 1, "Expecting a null-terminated ROS<char>");
-
-            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);
-                }
-            }
-        }
     }
 }