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

[release/5.0] Http telemetry changes backport #41610

Merged
merged 3 commits into from
Aug 31, 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
379 changes: 255 additions & 124 deletions src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,28 @@ public HttpMessageInvoker(HttpMessageHandler handler, bool disposeHandler)
}
CheckDisposed();

return _handler.Send(request, cancellationToken);
if (HttpTelemetry.Log.IsEnabled() && !request.WasSentByHttpClient() && request.RequestUri != null)
{
HttpTelemetry.Log.RequestStart(request);

try
{
return _handler.Send(request, cancellationToken);
}
catch when (LogRequestFailed(telemetryStarted: true))
{
// Unreachable as LogRequestFailed will return false
throw;
}
finally
{
HttpTelemetry.Log.RequestStop();
}
}
else
{
return _handler.Send(request, cancellationToken);
}
}

public virtual Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
Expand All @@ -53,7 +74,40 @@ public HttpMessageInvoker(HttpMessageHandler handler, bool disposeHandler)
}
CheckDisposed();

if (HttpTelemetry.Log.IsEnabled() && !request.WasSentByHttpClient() && request.RequestUri != null)
{
return SendAsyncWithTelemetry(_handler, request, cancellationToken);
}

return _handler.SendAsync(request, cancellationToken);

static async Task<HttpResponseMessage> SendAsyncWithTelemetry(HttpMessageHandler handler, HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpTelemetry.Log.RequestStart(request);

try
{
return await handler.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
catch when (LogRequestFailed(telemetryStarted: true))
{
// Unreachable as LogRequestFailed will return false
throw;
}
finally
{
HttpTelemetry.Log.RequestStop();
}
}
}

internal static bool LogRequestFailed(bool telemetryStarted)
{
if (HttpTelemetry.Log.IsEnabled() && telemetryStarted)
{
HttpTelemetry.Log.RequestFailed();
}
scalablecory marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ 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 should only be sent if this field is equal to MessageNotYetSent.
// The message shouldn't be sent again if this field is equal to MessageAlreadySent.
private int _sendStatus = MessageNotYetSent;

private HttpMethod _method;
Expand Down Expand Up @@ -201,32 +200,10 @@ private void InitializeValues(HttpMethod method, Uri? requestUri)

internal bool MarkAsSent()
{
return Interlocked.CompareExchange(ref _sendStatus, MessageAlreadySent, MessageNotYetSent) == MessageNotYetSent;
return Interlocked.Exchange(ref _sendStatus, MessageAlreadySent) == MessageNotYetSent;
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
}

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();
}
}
}
internal bool WasSentByHttpClient() => _sendStatus == MessageAlreadySent;

#region IDisposable Members

Expand All @@ -242,8 +219,6 @@ protected virtual void Dispose(bool disposing)
_content.Dispose();
}
}

OnStopped();
}

public void Dispose()
Expand Down
Loading