-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Martin Taillefer <[email protected]>
- Loading branch information
Showing
15 changed files
with
1,127 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Libraries/Microsoft.Extensions.Http.Telemetry/Metering/HelperExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.Http.Telemetry.Metering; | ||
|
||
internal static class HelperExtensions | ||
{ | ||
internal static HttpStatusCode GetStatusCode(this Exception ex) | ||
{ | ||
if (ex is TaskCanceledException) | ||
{ | ||
return HttpStatusCode.GatewayTimeout; | ||
} | ||
else if (ex is HttpRequestException exception) | ||
{ | ||
#if NET5_0_OR_GREATER | ||
return exception.StatusCode ?? HttpStatusCode.ServiceUnavailable; | ||
#else | ||
return HttpStatusCode.ServiceUnavailable; | ||
#endif | ||
} | ||
else | ||
{ | ||
return HttpStatusCode.InternalServerError; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Classifies the result of the HTTP response. | ||
/// </summary> | ||
/// <param name="statusCode">An <see cref="HttpStatusCode"/> to categorize the status code of HTTP response.</param> | ||
/// <returns><see cref="HttpRequestResultType"/> type of HTTP response and its <see cref="HttpStatusCode"/>.</returns> | ||
internal static HttpRequestResultType GetResultCategory(this HttpStatusCode statusCode) | ||
{ | ||
if (statusCode >= HttpStatusCode.Continue && statusCode < HttpStatusCode.BadRequest) | ||
{ | ||
return HttpRequestResultType.Success; | ||
} | ||
else if (statusCode >= HttpStatusCode.BadRequest && statusCode < HttpStatusCode.InternalServerError) | ||
{ | ||
return HttpRequestResultType.ExpectedFailure; | ||
} | ||
|
||
return HttpRequestResultType.Failure; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Libraries/Microsoft.Extensions.Http.Telemetry/Metering/HttpRequestResultType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.EnumStrings; | ||
|
||
namespace Microsoft.Extensions.Http.Telemetry.Metering; | ||
|
||
/// <summary> | ||
/// Statuses for classifying http request result. | ||
/// </summary> | ||
[Experimental] | ||
[EnumStrings] | ||
public enum HttpRequestResultType | ||
{ | ||
/// <summary> | ||
/// The status code of the http request indicates that the request is successful. | ||
/// </summary> | ||
Success, | ||
|
||
/// <summary> | ||
/// The status code of the http request indicates that this request did not succeed and to be treated as failure. | ||
/// </summary> | ||
Failure, | ||
|
||
/// <summary> | ||
/// The status code of the http request indicates that the request did not succeed but has failed with an error which is expected and acceptable for this request. | ||
/// </summary> | ||
/// <remarks> | ||
/// Expected failures are generally excluded from availability calculations i.e. they are neither | ||
/// treated as success nor as failures for availability calculation. | ||
/// </remarks> | ||
ExpectedFailure, | ||
} |
45 changes: 45 additions & 0 deletions
45
...ies/Microsoft.Extensions.Http.Telemetry/Metering/Internal/HttpClientDiagnosticObserver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if !NETFRAMEWORK | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.Extensions.Http.Telemetry.Metering.Internal; | ||
|
||
internal sealed class HttpClientDiagnosticObserver : IObserver<DiagnosticListener>, IDisposable | ||
{ | ||
private const string ListenerName = "HttpHandlerDiagnosticListener"; | ||
private readonly HttpClientRequestAdapter _httpClientRequestAdapter; | ||
private IDisposable? _observer; | ||
|
||
public HttpClientDiagnosticObserver(HttpClientRequestAdapter httpClientRequestAdapter) | ||
{ | ||
_httpClientRequestAdapter = httpClientRequestAdapter; | ||
} | ||
|
||
public void OnNext(DiagnosticListener value) | ||
{ | ||
if (value.Name == ListenerName) | ||
{ | ||
_observer?.Dispose(); | ||
_observer = value.SubscribeWithAdapter(_httpClientRequestAdapter); | ||
} | ||
} | ||
|
||
public void OnCompleted() | ||
{ | ||
// Method intentionally left empty. | ||
} | ||
|
||
public void OnError(Exception error) | ||
{ | ||
// Method intentionally left empty. | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_observer?.Dispose(); | ||
} | ||
} | ||
#endif |
27 changes: 27 additions & 0 deletions
27
...aries/Microsoft.Extensions.Http.Telemetry/Metering/Internal/HttpClientMeteringListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#if !NETFRAMEWORK | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.Extensions.Http.Telemetry.Metering.Internal; | ||
|
||
internal sealed class HttpClientMeteringListener : IDisposable | ||
{ | ||
internal static bool UsingDiagnosticsSource { get; set; } | ||
|
||
private readonly IDisposable _listener; | ||
|
||
public HttpClientMeteringListener(HttpClientDiagnosticObserver httpClientDiagnosticObserver) | ||
{ | ||
UsingDiagnosticsSource = true; | ||
_listener = DiagnosticListener.AllListeners.Subscribe(httpClientDiagnosticObserver); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_listener.Dispose(); | ||
} | ||
} | ||
#endif |
Oops, something went wrong.