From 42752ce18221257ecbedb7af2ba3f6864ba0d50f Mon Sep 17 00:00:00 2001 From: Tim Hess Date: Fri, 20 Sep 2024 15:56:14 -0500 Subject: [PATCH] drop HttpClientDesktopObserver --- .../EndpointServiceCollectionExtensions.cs | 12 +- .../Observers/HttpClientDesktopObserver.cs | 125 ------------------ ...tCoreObserver.cs => HttpClientObserver.cs} | 37 +++--- 3 files changed, 26 insertions(+), 148 deletions(-) delete mode 100644 src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientDesktopObserver.cs rename src/Management/src/Endpoint/Actuators/Metrics/Observers/{HttpClientCoreObserver.cs => HttpClientObserver.cs} (81%) diff --git a/src/Management/src/Endpoint/Actuators/Metrics/EndpointServiceCollectionExtensions.cs b/src/Management/src/Endpoint/Actuators/Metrics/EndpointServiceCollectionExtensions.cs index da917cd238..4c55c9cf83 100644 --- a/src/Management/src/Endpoint/Actuators/Metrics/EndpointServiceCollectionExtensions.cs +++ b/src/Management/src/Endpoint/Actuators/Metrics/EndpointServiceCollectionExtensions.cs @@ -36,14 +36,22 @@ public static IServiceCollection AddMetricsActuator(this IServiceCollection serv return services; } + /// + /// Adds metrics observers to the service container. + /// + /// + /// The to add services to. + /// + /// + /// The incoming so that additional calls can be chained. + /// public static IServiceCollection AddMetricsObservers(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); services.ConfigureOptionsWithChangeTokenSource(); services.TryAddEnumerable(ServiceDescriptor.Singleton()); - services.TryAddEnumerable(ServiceDescriptor.Singleton()); - services.TryAddEnumerable(ServiceDescriptor.Singleton()); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.TryAddEnumerable(ServiceDescriptor.Singleton()); diff --git a/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientDesktopObserver.cs b/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientDesktopObserver.cs deleted file mode 100644 index 122f93964e..0000000000 --- a/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientDesktopObserver.cs +++ /dev/null @@ -1,125 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License. -// See the LICENSE file in the project root for more information. - -using System.Diagnostics; -using System.Diagnostics.Metrics; -using System.Globalization; -using System.Net; -using System.Text.RegularExpressions; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Steeltoe.Common; -using Steeltoe.Management.Diagnostics; - -namespace Steeltoe.Management.Endpoint.Actuators.Metrics.Observers; - -internal sealed class HttpClientDesktopObserver : MetricsObserver -{ - private const string StatusTagKey = "status"; - private const string UriTagKey = "uri"; - private const string MethodTagKey = "method"; - private const string ClientTagKey = "clientName"; - private const string DiagnosticName = "System.Net.Http.Desktop"; - private const string DefaultObserverName = "HttpClientDesktopObserver"; - private const string StopEventName = "System.Net.Http.Desktop.HttpRequestOut.Stop"; - private const string StopExEventName = "System.Net.Http.Desktop.HttpRequestOut.Ex.Stop"; - - private readonly Histogram _clientTimeMeasure; - private readonly Histogram _clientCountMeasure; - private readonly ILogger _logger; - - public HttpClientDesktopObserver(IOptionsMonitor optionsMonitor, ILoggerFactory loggerFactory) - : base(DefaultObserverName, DiagnosticName, loggerFactory) - { - ArgumentNullException.ThrowIfNull(optionsMonitor); - - string? egressIgnorePattern = optionsMonitor.CurrentValue.EgressIgnorePattern; - - if (egressIgnorePattern != null) - { - SetPathMatcher(new Regex(egressIgnorePattern, RegexOptions.None, TimeSpan.FromSeconds(1))); - } - - _clientTimeMeasure = SteeltoeMetrics.Meter.CreateHistogram("http.desktop.client.request.time"); - _clientCountMeasure = SteeltoeMetrics.Meter.CreateHistogram("http.desktop.client.request.count"); - _logger = loggerFactory.CreateLogger(); - } - - public override void ProcessEvent(string eventName, object? value) - { - if (value == null) - { - return; - } - - Activity? current = Activity.Current; - - if (current == null) - { - return; - } - - var request = GetPropertyOrDefault(value, "Request"); - - if (request == null) - { - return; - } - - if (eventName == StopEventName) - { - _logger.LogTrace("HandleStopEvent start {Thread}", System.Environment.CurrentManagedThreadId); - - var response = GetPropertyOrDefault(value, "Response"); - - if (response != null) - { - HandleStopEvent(current, request, response.StatusCode); - } - - _logger.LogTrace("HandleStopEvent finished {Thread}", System.Environment.CurrentManagedThreadId); - } - else if (eventName == StopExEventName) - { - _logger.LogTrace("HandleStopEventEx start {Thread}", System.Environment.CurrentManagedThreadId); - - var statusCode = GetPropertyOrDefault(value, "StatusCode"); - - HandleStopEvent(current, request, statusCode); - - _logger.LogTrace("HandleStopEventEx finished {Thread}", System.Environment.CurrentManagedThreadId); - } - } - - private void HandleStopEvent(Activity current, HttpWebRequest request, HttpStatusCode statusCode) - { - if (ShouldIgnoreRequest(request.RequestUri.AbsolutePath)) - { - _logger.LogDebug("HandleStopEvent: Ignoring path: {Path}", SecurityUtilities.SanitizeInput(request.RequestUri.AbsolutePath)); - return; - } - - if (current.Duration.TotalMilliseconds > 0) - { - ReadOnlySpan> labels = GetLabels(request, statusCode).AsReadonlySpan(); - _clientTimeMeasure.Record(current.Duration.TotalMilliseconds, labels); - _clientCountMeasure.Record(1, labels); - } - } - - private Dictionary GetLabels(HttpWebRequest request, HttpStatusCode statusCode) - { - string uri = request.RequestUri.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped); - string status = ((int)statusCode).ToString(CultureInfo.InvariantCulture); - string clientName = request.RequestUri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped); - - return new Dictionary - { - { UriTagKey, uri }, - { StatusTagKey, status }, - { ClientTagKey, clientName }, - { MethodTagKey, request.Method } - }; - } -} diff --git a/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientCoreObserver.cs b/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientObserver.cs similarity index 81% rename from src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientCoreObserver.cs rename to src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientObserver.cs index 29fa3e490d..ae4976711f 100644 --- a/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientCoreObserver.cs +++ b/src/Management/src/Endpoint/Actuators/Metrics/Observers/HttpClientObserver.cs @@ -13,14 +13,14 @@ namespace Steeltoe.Management.Endpoint.Actuators.Metrics.Observers; -internal sealed class HttpClientCoreObserver : MetricsObserver +internal sealed class HttpClientObserver : MetricsObserver { private const string StatusTagKey = "status"; private const string UriTagKey = "uri"; private const string MethodTagKey = "method"; private const string ClientTagKey = "clientName"; private const string DiagnosticName = "HttpHandlerDiagnosticListener"; - private const string DefaultObserverName = "HttpClientCoreObserver"; + private const string DefaultObserverName = "HttpClientObserver"; private const string StopEventName = "System.Net.Http.HttpRequestOut.Stop"; private const string ExceptionEvent = "System.Net.Http.Exception"; @@ -28,7 +28,7 @@ internal sealed class HttpClientCoreObserver : MetricsObserver private readonly Histogram _clientCountMeasure; private readonly ILogger _logger; - public HttpClientCoreObserver(IOptionsMonitor optionsMonitor, ILoggerFactory loggerFactory) + public HttpClientObserver(IOptionsMonitor optionsMonitor, ILoggerFactory loggerFactory) : base(DefaultObserverName, DiagnosticName, loggerFactory) { ArgumentNullException.ThrowIfNull(optionsMonitor); @@ -42,12 +42,12 @@ public HttpClientCoreObserver(IOptionsMonitor optionsMon _clientTimeMeasure = SteeltoeMetrics.Meter.CreateHistogram("http.client.request.time"); _clientCountMeasure = SteeltoeMetrics.Meter.CreateHistogram("http.client.request.count"); - _logger = loggerFactory.CreateLogger(); + _logger = loggerFactory.CreateLogger(); } public override void ProcessEvent(string eventName, object? value) { - if (value == null) + if (value == null || (eventName != StopEventName && eventName != ExceptionEvent)) { return; } @@ -107,7 +107,7 @@ private void HandleStopEvent(Activity current, HttpRequestMessage request, HttpR } } - private Dictionary GetLabels(HttpRequestMessage request, HttpResponseMessage? response, TaskStatus taskStatus) + private static Dictionary GetLabels(HttpRequestMessage request, HttpResponseMessage? response, TaskStatus taskStatus) { string uri = request.RequestUri!.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped); string statusCode = GetStatusCode(response, taskStatus); @@ -122,24 +122,19 @@ private void HandleStopEvent(Activity current, HttpRequestMessage request, HttpR }; } - private string GetStatusCode(HttpResponseMessage? response, TaskStatus taskStatus) + private static string GetStatusCode(HttpResponseMessage? response, TaskStatus taskStatus) { - if (response != null) + if (response == null) { - int value = (int)response.StatusCode; - return value.ToString(CultureInfo.InvariantCulture); + return taskStatus switch + { + TaskStatus.Faulted => "CLIENT_FAULT", + TaskStatus.Canceled => "CLIENT_CANCELED", + _ => "CLIENT_ERROR" + }; } - if (taskStatus == TaskStatus.Faulted) - { - return "CLIENT_FAULT"; - } - - if (taskStatus == TaskStatus.Canceled) - { - return "CLIENT_CANCELED"; - } - - return "CLIENT_ERROR"; + int value = (int)response.StatusCode; + return value.ToString(CultureInfo.InvariantCulture); } }