Skip to content

Ensure Http Telemetry correctness #40338

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

Merged
merged 8 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
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
Expand Up @@ -6,16 +6,18 @@
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;

namespace System.Net.Http
{
public class HttpRequestMessage : IDisposable
{
private const int MessageNotYetSent = 0;
private const int MessageAlreadySent = 1;
private const int MessageAlreadySent_StopNotYetCalled = 2;

// Track whether the message has been sent.
// The message shouldn't be sent again if this field is equal to MessageAlreadySent.
// The message should only be sent if this field is equal to MessageNotYetSent.
private int _sendStatus = MessageNotYetSent;

private HttpMethod _method;
Expand Down Expand Up @@ -183,7 +185,31 @@ private void InitializeValues(HttpMethod method, Uri? requestUri)

internal bool MarkAsSent()
{
return Interlocked.Exchange(ref _sendStatus, MessageAlreadySent) == MessageNotYetSent;
return Interlocked.CompareExchange(ref _sendStatus, MessageAlreadySent, MessageNotYetSent) == MessageNotYetSent;
}

internal void MarkAsTrackedByTelemetry()
{
Debug.Assert(_sendStatus != MessageAlreadySent_StopNotYetCalled);
_sendStatus = MessageAlreadySent_StopNotYetCalled;
}

internal void OnAborted() => OnStopped(aborted: true);

internal void OnStopped(bool aborted = false)
{
if (HttpTelemetry.Log.IsEnabled())
{
if (Interlocked.Exchange(ref _sendStatus, MessageAlreadySent) == MessageAlreadySent_StopNotYetCalled)
{
if (aborted)
{
HttpTelemetry.Log.RequestAborted();
}

HttpTelemetry.Log.RequestStop();
}
}
}

#region IDisposable Members
Expand All @@ -200,6 +226,8 @@ protected virtual void Dispose(bool disposing)
_content.Dispose();
}
}

OnStopped();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public override int Read(Span<byte> buffer)
if (_connection == null)
{
// Fully consumed the response in ReadChunksFromConnectionBuffer.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
return 0;
}

Expand Down Expand Up @@ -362,7 +361,6 @@ private ReadOnlyMemory<byte> ReadChunkFromConnectionBuffer(int maxBytesToRead, C
cancellationRegistration.Dispose();
CancellationHelper.ThrowIfCancellationRequested(cancellationRegistration.Token);

if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_state = ParsingState.Done;
_connection.CompleteResponse();
_connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public override int Read(Span<byte> buffer)
if (bytesRead == 0)
{
// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection = null;
connection.Dispose();
}
Expand Down Expand Up @@ -81,7 +80,6 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);

// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection = null;
connection.Dispose();
}
Expand Down Expand Up @@ -143,7 +141,6 @@ private async Task CompleteCopyToAsync(Task copyTask, HttpConnection connection,
private void Finish(HttpConnection connection)
{
// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection = null;
connection.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public override int Read(Span<byte> buffer)
if (_contentBytesRemaining == 0)
{
// End of response body
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection.CompleteResponse();
_connection = null;
}
Expand Down Expand Up @@ -110,7 +109,6 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
if (_contentBytesRemaining == 0)
{
// End of response body
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection.CompleteResponse();
_connection = null;
}
Expand Down Expand Up @@ -165,7 +163,6 @@ private async Task CompleteCopyToAsync(Task copyTask, CancellationToken cancella

private void Finish()
{
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_contentBytesRemaining = 0;
_connection!.CompleteResponse();
_connection = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private void Complete()
_creditWaiter = null;
}

if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestStop();
if (HttpTelemetry.Log.IsEnabled()) _request.OnStopped();
}

private void Cancel()
Expand Down Expand Up @@ -387,7 +387,7 @@ private void Cancel()
_waitSource.SetResult(true);
}

if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestAborted();
if (HttpTelemetry.Log.IsEnabled()) _request.OnAborted();
}

// Returns whether the waiter should be signalled or not.
Expand Down Expand Up @@ -1147,6 +1147,10 @@ private void CloseResponseBody()
{
Cancel();
}
else
{
_request.OnStopped();
}

_responseBuffer.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public HttpConnection(
if (NetEventSource.Log.IsEnabled()) TraceConnection(_stream);
}

~HttpConnection() => Dispose(disposing: false);

public void Dispose() => Dispose(disposing: true);

protected void Dispose(bool disposing)
Expand All @@ -100,6 +102,9 @@ protected void Dispose(bool disposing)
{
if (NetEventSource.Log.IsEnabled()) Trace("Connection closing.");
_pool.DecrementConnectionCount();

if (HttpTelemetry.Log.IsEnabled()) _currentRequest?.OnAborted();

if (disposing)
{
GC.SuppressFinalize(this);
Expand Down Expand Up @@ -630,7 +635,6 @@ public async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
Stream responseStream;
if (ReferenceEquals(normalizedMethod, HttpMethod.Head) || response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.NotModified)
{
if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestStop();
responseStream = EmptyReadStream.Instance;
CompleteResponse();
}
Expand All @@ -653,7 +657,6 @@ public async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
long contentLength = response.Content.Headers.ContentLength.GetValueOrDefault();
if (contentLength <= 0)
{
if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestStop();
responseStream = EmptyReadStream.Instance;
CompleteResponse();
}
Expand Down Expand Up @@ -1850,6 +1853,8 @@ private void CompleteResponse()
Debug.Assert(_currentRequest != null, "Expected the connection to be associated with a request.");
Debug.Assert(_writeOffset == 0, "Everything in write buffer should have been flushed.");

if (HttpTelemetry.Log.IsEnabled()) _currentRequest.OnStopped();

// Disassociate the connection from a request.
_currentRequest = null;

Expand Down Expand Up @@ -1963,13 +1968,4 @@ public sealed override void Trace(string message, [CallerMemberName] string? mem
memberName, // method name
message); // message
}

internal sealed class HttpConnectionWithFinalizer : HttpConnection
{
public HttpConnectionWithFinalizer(HttpConnectionPool pool, Connection connection, TransportContext? transportContext) : base(pool, connection, transportContext) { }

// This class is separated from HttpConnection so we only pay the price of having a finalizer
// when it's actually needed, e.g. when MaxConnectionsPerServer is enabled.
~HttpConnectionWithFinalizer() => Dispose(disposing: false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,7 @@ private ValueTask<Connection> ConnectToTcpHostAsync(string host, int port, HttpR

private HttpConnection ConstructHttp11Connection(Connection connection, TransportContext? transportContext)
{
return _maxConnections == int.MaxValue ?
new HttpConnection(this, connection, transportContext) :
new HttpConnectionWithFinalizer(this, connection, transportContext); // finalizer needed to signal the pool when a connection is dropped
return new HttpConnection(this, connection, transportContext);
}

// Returns the established stream or an HttpResponseMessage from the proxy indicating failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,22 @@ private async ValueTask<HttpResponseMessage> SendAsyncWithLogging(HttpRequestMes
request.RequestUri.PathAndQuery,
request.Version.Major,
request.Version.Minor);

request.MarkAsTrackedByTelemetry();

try
{
return await SendAsyncHelper(request, async, doRequestAuth, cancellationToken).ConfigureAwait(false);
}
catch (Exception e) when (LogException(e))
catch when (LogException(request))
{
// This code should never run.
throw;
}

static bool LogException(Exception e)
static bool LogException(HttpRequestMessage request)
{
HttpTelemetry.Log.RequestAborted();
HttpTelemetry.Log.RequestStop();
request.OnAborted();

// Returning false means the catch handler isn't run.
// So the exception isn't considered to be caught so it will now propagate up the stack.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;

namespace System.Net.Http
{
internal abstract class HttpContentStream : HttpBaseStream
{
protected HttpConnection? _connection;

// Makes sure we don't call HttpTelemetry events more than once.
private int _requestStopCalled; // 0==no, 1==yes

public HttpContentStream(HttpConnection connection)
{
_connection = connection;
Expand Down Expand Up @@ -45,14 +40,6 @@ protected HttpConnection GetConnectionOrThrow()
ThrowObjectDisposedException();
}

protected void LogRequestStop()
{
if (Interlocked.Exchange(ref _requestStopCalled, 1) == 0)
{
HttpTelemetry.Log.RequestStop();
}
}

private HttpConnection ThrowObjectDisposedException() => throw new ObjectDisposedException(GetType().Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public override int Read(Span<byte> buffer)
if (bytesRead == 0)
{
// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection = null;
connection.Dispose();
}
Expand Down Expand Up @@ -81,7 +80,6 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);

// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
_connection = null;
connection.Dispose();
}
Expand Down Expand Up @@ -143,7 +141,6 @@ private async Task CompleteCopyToAsync(Task copyTask, HttpConnection connection,
private void Finish(HttpConnection connection)
{
// We cannot reuse this connection, so close it.
if (HttpTelemetry.Log.IsEnabled()) LogRequestStop();
connection.Dispose();
_connection = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
<Compile Include="$(CommonTestPath)System\Net\Http\SchSendAuxRecordHttpTest.cs"
Link="Common\System\Net\Http\SchSendAuxRecordHttpTest.cs" />
<Compile Include="SyncHttpHandlerTest.cs" />
<Compile Include="TelemetryTest.cs" />
<Compile Include="StreamContentTest.cs" />
<Compile Include="StringContentTest.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\SyncBlockingContent.cs"
Expand Down
Loading