Skip to content
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

Account for AddressFamily pairs in NameResolution telemetry #56614

Merged
merged 1 commit into from
Jul 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
}