Skip to content

Commit

Permalink
drop HttpClientDesktopObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHess committed Sep 20, 2024
1 parent 154493c commit 42752ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ public static IServiceCollection AddMetricsActuator(this IServiceCollection serv
return services;
}

/// <summary>
/// Adds metrics observers to the service container.
/// </summary>
/// <param name="services">
/// The <see cref="IServiceCollection" /> to add services to.
/// </param>
/// <returns>
/// The incoming <paramref name="services" /> so that additional calls can be chained.
/// </returns>
public static IServiceCollection AddMetricsObservers(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);

services.ConfigureOptionsWithChangeTokenSource<MetricsObserverOptions, ConfigureMetricsObserverOptions>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticObserver, AspNetCoreHostingObserver>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticObserver, HttpClientCoreObserver>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticObserver, HttpClientDesktopObserver>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticObserver, HttpClientObserver>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IRuntimeDiagnosticSource, ClrRuntimeObserver>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<EventListener, EventCounterListener>());

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

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";
private readonly Histogram<double> _clientTimeMeasure;
private readonly Histogram<double> _clientCountMeasure;
private readonly ILogger _logger;

public HttpClientCoreObserver(IOptionsMonitor<MetricsObserverOptions> optionsMonitor, ILoggerFactory loggerFactory)
public HttpClientObserver(IOptionsMonitor<MetricsObserverOptions> optionsMonitor, ILoggerFactory loggerFactory)
: base(DefaultObserverName, DiagnosticName, loggerFactory)
{
ArgumentNullException.ThrowIfNull(optionsMonitor);
Expand All @@ -42,12 +42,12 @@ public HttpClientCoreObserver(IOptionsMonitor<MetricsObserverOptions> optionsMon

_clientTimeMeasure = SteeltoeMetrics.Meter.CreateHistogram<double>("http.client.request.time");
_clientCountMeasure = SteeltoeMetrics.Meter.CreateHistogram<double>("http.client.request.count");
_logger = loggerFactory.CreateLogger<HttpClientCoreObserver>();
_logger = loggerFactory.CreateLogger<HttpClientObserver>();
}

public override void ProcessEvent(string eventName, object? value)
{
if (value == null)
if (value == null || (eventName != StopEventName && eventName != ExceptionEvent))
{
return;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ private void HandleStopEvent(Activity current, HttpRequestMessage request, HttpR
}
}

private Dictionary<string, object?> GetLabels(HttpRequestMessage request, HttpResponseMessage? response, TaskStatus taskStatus)
private static Dictionary<string, object?> GetLabels(HttpRequestMessage request, HttpResponseMessage? response, TaskStatus taskStatus)
{
string uri = request.RequestUri!.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped);
string statusCode = GetStatusCode(response, taskStatus);
Expand All @@ -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);
}
}

0 comments on commit 42752ce

Please sign in to comment.