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

Activities for Http Connections, Dns, Sockets and SslStream #103922

Merged
merged 50 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
4e740d0
Activities for Http Connection, Dns, Sockets and Tls
antonfirsov Jun 24, 2024
3945fe4
address review feedback
antonfirsov Jun 25, 2024
f40ce38
Merge branch 'main' into connection-activities-05
antonfirsov Jun 25, 2024
08bc611
resolve conflicts
antonfirsov Jun 25, 2024
e2cf15e
get rid of finally block
antonfirsov Jun 25, 2024
e963ed2
Merge branch 'main' into connection-activities-05
antonfirsov Jul 1, 2024
133c7b7
Merge branch 'main' into connection-activities-05
antonfirsov Jul 5, 2024
5119f9c
System.Net.NameResolution: record tags and ActivityStatusCode
antonfirsov Jul 5, 2024
4b1ffa8
set DNS activity DisplayName
antonfirsov Jul 5, 2024
5e0e971
Socket Activity tags
antonfirsov Jul 6, 2024
4988004
emit attributes on SslStream Activity
antonfirsov Jul 6, 2024
7777b68
connection setup activity graph
antonfirsov Jul 8, 2024
fd60820
emit tags on http connection activities
antonfirsov Jul 8, 2024
b730434
Merge branch 'connection-activities-05' of https://github.com/antonfi…
antonfirsov Jul 8, 2024
4ce1499
fix MacOS failure
antonfirsov Jul 8, 2024
ecce03b
adjustments
antonfirsov Jul 8, 2024
c109253
Merge branch 'main' into connection-activities-05
antonfirsov Jul 9, 2024
dc563ad
consolidate connection setup Activity error handling into a single ca…
antonfirsov Jul 9, 2024
c099441
address more review feedback
antonfirsov Jul 9, 2024
17e62a0
merge stuff
antonfirsov Jul 9, 2024
2d8bee3
emit the link on the request span instead of wait_for_connection
antonfirsov Jul 10, 2024
55b1eea
Merge branch 'main' into connection-activities-05
antonfirsov Jul 10, 2024
9995e27
adjust System.Net.NameResolution
antonfirsov Jul 10, 2024
c2c7c87
adjust System.Net.Sockets naming, add UDS test
antonfirsov Jul 10, 2024
8acab55
replace the _connectActivity field with a CWT
antonfirsov Jul 10, 2024
afe1648
Adjust SslStream tracing
antonfirsov Jul 10, 2024
96f1f18
ActivityKind
antonfirsov Jul 10, 2024
a9cd814
adjust HTTP Activities
antonfirsov Jul 10, 2024
f4548ff
Merge branch 'main' into connection-activities-05
antonfirsov Jul 10, 2024
576842c
SocketsHttpHandler_DiagnosticsTest_Http3
antonfirsov Jul 10, 2024
f04ab6e
WIP h3
antonfirsov Jul 10, 2024
7b94039
adjust to main
antonfirsov Jul 10, 2024
1c5e772
implement H/3 connection diagnostics
antonfirsov Jul 11, 2024
3d6911c
Merge branch 'main' into connection-activities-05
antonfirsov Jul 11, 2024
8c008e1
actually use the same ActivitySource for ConnectionSetup & WaitForCon…
antonfirsov Jul 11, 2024
e829df2
*handshake
antonfirsov Jul 11, 2024
7c94ae8
Update src/libraries/System.Net.NameResolution/src/System/Net/NameRes…
antonfirsov Jul 11, 2024
92afed7
Merge branch 'connection-activities-05' of https://github.com/antonfi…
antonfirsov Jul 11, 2024
06896bf
fix H3 logic based on feedback
antonfirsov Jul 11, 2024
b455d17
ConnectionSetupDiagnostics -> ConnectionSetupDistributedTracing
antonfirsov Jul 11, 2024
0d231ba
Merge branch 'main' into connection-activities-05
antonfirsov Jul 11, 2024
56e8cb3
suggestion
antonfirsov Jul 12, 2024
870eae5
suggestion
antonfirsov Jul 12, 2024
de774dd
suggestion
antonfirsov Jul 12, 2024
284734b
Merge branch 'main' into connection-activities-05
antonfirsov Jul 12, 2024
b1ef7f3
implement 'network.transport'
antonfirsov Jul 12, 2024
d99734f
SslStream: move Activity management code to NetSecurityTelemetry
antonfirsov Jul 12, 2024
ae587f7
readd assertion and add comment
antonfirsov Jul 12, 2024
9443cd2
Merge branch 'main' into connection-activities-05
antonfirsov Jul 12, 2024
c9a5ad2
Merge branch 'main' into connection-activities-05
antonfirsov Jul 13, 2024
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
Expand Up @@ -677,10 +677,9 @@ private async ValueTask<Stream> ConnectToTcpHostAsync(string host, int port, Htt
}
else
{
IPAddress[] addresses = Dns.GetHostAddresses(host);
using (cancellationToken.UnsafeRegister(static s => ((Socket)s!).Dispose(), socket))
{
socket.Connect(addresses, port);
socket.Connect(endPoint);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,10 @@ private static bool LogFailure(object hostNameOrAddress, in NameResolutionActivi
/// </remarks>
private static Task<TResult> RunAsync<TResult>(Func<object, NameResolutionActivity, TResult> func, object key, CancellationToken cancellationToken)
{
Activity? activityToRestore = NameResolutionActivity.IsTracingEnabled() ? Activity.Current : null;
bool tracingEnabled = NameResolutionActivity.IsTracingEnabled();
Activity? activityToRestore = tracingEnabled ? Activity.Current : null;
NameResolutionActivity activity = NameResolutionTelemetry.Log.BeforeResolution(key);
if (NameResolutionActivity.IsTracingEnabled())
if (tracingEnabled)
{
// Do not overwrite Activity.Current in the caller's ExecutionContext.
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
Activity.Current = activityToRestore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,32 @@ private Task ProcessAuthenticationAsync(bool isAsync = false, CancellationToken
{
ThrowIfExceptional();

Activity? activity = s_activitySource.StartActivity(ActivityName, IsServer ? ActivityKind.Server : ActivityKind.Client);
try
if (NetSecurityTelemetry.Log.IsEnabled() || s_activitySource.HasListeners())
{
if (NetSecurityTelemetry.Log.IsEnabled())
{
return ProcessAuthenticationWithTelemetryAsync(isAsync, cancellationToken);
}
else
{
return isAsync ?
ForceAuthenticationAsync<AsyncReadWriteAdapter>(IsServer, null, cancellationToken) :
ForceAuthenticationAsync<SyncReadWriteAdapter>(IsServer, null, cancellationToken);
}
return ProcessAuthenticationWithTelemetryAsync(isAsync, cancellationToken);
}
finally
else
{
activity?.Stop();
return isAsync ?
ForceAuthenticationAsync<AsyncReadWriteAdapter>(IsServer, null, cancellationToken) :
ForceAuthenticationAsync<SyncReadWriteAdapter>(IsServer, null, cancellationToken);
}
}

private async Task ProcessAuthenticationWithTelemetryAsync(bool isAsync, CancellationToken cancellationToken)
{
NetSecurityTelemetry.Log.HandshakeStart(IsServer, _sslAuthenticationOptions.TargetHost);
long startingTimestamp = Stopwatch.GetTimestamp();
long startingTimestamp;
if (NetSecurityTelemetry.Log.IsEnabled())
{
NetSecurityTelemetry.Log.HandshakeStart(IsServer, _sslAuthenticationOptions.TargetHost);
startingTimestamp = Stopwatch.GetTimestamp();
}
else
{
startingTimestamp = 0;
}

Activity? activity = s_activitySource.StartActivity(ActivityName, IsServer ? ActivityKind.Server : ActivityKind.Client);
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved

try
{
Expand All @@ -144,17 +146,27 @@ private async Task ProcessAuthenticationWithTelemetryAsync(bool isAsync, Cancell

await task.ConfigureAwait(false);

// SslStream could already have been disposed at this point, in which case _connectionOpenedStatus == 2
// Make sure that we increment the open connection counter only if it is guaranteed to be decremented in dispose/finalize
bool connectionOpen = Interlocked.CompareExchange(ref _connectionOpenedStatus, 1, 0) == 0;

NetSecurityTelemetry.Log.HandshakeCompleted(GetSslProtocolInternal(), startingTimestamp, connectionOpen);
if (startingTimestamp is not 0)
{
// SslStream could already have been disposed at this point, in which case _connectionOpenedStatus == 2
// Make sure that we increment the open connection counter only if it is guaranteed to be decremented in dispose/finalize
bool connectionOpen = Interlocked.CompareExchange(ref _connectionOpenedStatus, 1, 0) == 0;
NetSecurityTelemetry.Log.HandshakeCompleted(GetSslProtocolInternal(), startingTimestamp, connectionOpen);
}
}
catch (Exception ex)
{
NetSecurityTelemetry.Log.HandshakeFailed(IsServer, startingTimestamp, ex.Message);
if (startingTimestamp is not 0)
{
NetSecurityTelemetry.Log.HandshakeFailed(IsServer, startingTimestamp, ex.Message);
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
}

throw;
}
finally
{
activity?.Stop();
}
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ public static void EventSource_ExistsWithCorrectId()
Assert.NotEmpty(EventSource.GenerateManifest(esType, esType.Assembly.Location));
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[SkipOnPlatform(TestPlatforms.iOS | TestPlatforms.tvOS, "X509 certificate store is not supported on iOS or tvOS.")] // Match SslStream_StreamToStream_Authentication_Success
public async Task SuccessfulHandshake_ActivityRecorded()
[InlineData(false)]
[InlineData(true)]
public async Task SuccessfulHandshake_ActivityRecorded(bool synchronousApi)
{
await RemoteExecutor.Invoke(async () =>
await RemoteExecutor.Invoke(async synchronousApiStr =>
{
using ActivityRecorder recorder = new ActivityRecorder(ActivitySourceName, ActivityName);

var test = new SslStreamStreamToStreamTest_Async();
SslStreamStreamToStreamTest test = bool.Parse(synchronousApiStr)
? new SslStreamStreamToStreamTest_SyncParameters()
: new SslStreamStreamToStreamTest_Async();
await test.SslStream_StreamToStream_Authentication_Success();

recorder.VerifyActivityRecorded(2); // client + server
}).DisposeAsync();
Assert.True(recorder.LastFinishedActivity.Duration > TimeSpan.Zero);
}, synchronousApi.ToString()).DisposeAsync();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
Expand Down