Skip to content

Commit

Permalink
Account for AddressFamily pairs in NameResolution telemetry (#56614)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Jul 30, 2021
1 parent facbdce commit 58232e0
Showing 1 changed file with 16 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -50,8 +50,6 @@ protected override void OnEventCommand(EventCommandEventArgs command)
}
}

private const int MaxIPFormattedLength = 128;

[Event(ResolutionStartEventId, Level = EventLevel.Informational)]
private void ResolutionStart(string hostNameOrAddress) => WriteEvent(ResolutionStartEventId, hostNameOrAddress);

Expand All @@ -66,7 +64,12 @@ protected override void OnEventCommand(EventCommandEventArgs command)
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())
{
Expand All @@ -75,14 +78,15 @@ public ValueStopwatch BeforeResolution(object hostNameOrAddress)

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();
Expand Down Expand Up @@ -111,47 +115,5 @@ public void AfterResolution(ValueStopwatch stopwatch, bool successful)
}
}
}

[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);
}
}
}
}
}

0 comments on commit 58232e0

Please sign in to comment.