Skip to content

Commit

Permalink
refactor: internal code
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Jul 14, 2024
1 parent 420cd8e commit 7fdf2d5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Prometheus.Client.HttpRequestDurations;

public static class PrometheusMiddlewareBuilderExtensions
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Metrics logging of request durations
Expand Down
21 changes: 7 additions & 14 deletions src/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
Expand Down Expand Up @@ -44,35 +45,27 @@ public static string GetRouteName(this HttpContext httpContext)

private static string GetRouteDataKeyValue(this HttpContext httpContext, string routeDataKey)
{
const string notFoundResult = "NotAvailable";

if (httpContext == null)
throw new ArgumentNullException(nameof(httpContext));

var routeData = httpContext.Features.Get<CapturedRouteDataFeature>()?.Values;

// If we have captured route data, we always prefer it.
// Otherwise, we extract new route data right now.
if (routeData == null)
routeData = httpContext.GetRouteData()?.Values;
var routeData = httpContext.Features.Get<RouteDataFeature>()?.Values ?? httpContext.GetRouteData().Values;

if (routeData?.Values.Count > 0)
if (routeData.Values.Count > 0)
{
var result = string.Empty;

foreach (var item in routeData)
foreach (var item in routeData.Where(item => item.Key == routeDataKey))
{
if (item.Key == routeDataKey)
{
result = item.Value?.ToString();
break;
}
result = item.Value?.ToString();
break;

Check warning on line 62 in src/HttpContextExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/HttpContextExtensions.cs#L62

Added line #L62 was not covered by tests
}

if (!string.IsNullOrEmpty(result))
return result;
}

return notFoundResult;
return "NotAvailable";

Check warning on line 69 in src/HttpContextExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/HttpContextExtensions.cs#L69

Added line #L69 was not covered by tests
}
}
11 changes: 2 additions & 9 deletions src/HttpRequestDurationsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ private void WriteMetrics(string statusCode, string method, string controller, s
labelValues.Add(path);

if (_options.CustomLabels != null)
{
foreach (var customLabel in _options.CustomLabels)
labelValues.Add(customLabel.Value());
}
labelValues.AddRange(_options.CustomLabels.Select(customLabel => customLabel.Value()));

Check warning on line 139 in src/HttpRequestDurationsMiddleware.cs

View check run for this annotation

Codecov / codecov/patch

src/HttpRequestDurationsMiddleware.cs#L139

Added line #L139 was not covered by tests

_histogram.WithLabels(labelValues.ToArray()).Observe(elapsedSeconds);
}
Expand All @@ -149,16 +146,12 @@ private static void TryCaptureRouteData(HttpContext context)
var routeData = context.GetRouteData();

if (routeData.Values.Count == 0)
{
return;
}

var capturedRouteData = new CapturedRouteDataFeature();
var capturedRouteData = new RouteDataFeature();

Check warning on line 151 in src/HttpRequestDurationsMiddleware.cs

View check run for this annotation

Codecov / codecov/patch

src/HttpRequestDurationsMiddleware.cs#L151

Added line #L151 was not covered by tests

foreach ((string key, object value) in routeData.Values)
{
capturedRouteData.Values.Add(key, value);
}

context.Features.Set(capturedRouteData);

Check warning on line 156 in src/HttpRequestDurationsMiddleware.cs

View check run for this annotation

Codecov / codecov/patch

src/HttpRequestDurationsMiddleware.cs#L156

Added line #L156 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Prometheus.Client.HttpRequestDurations;

internal class CapturedRouteDataFeature
internal class RouteDataFeature
{
public RouteValueDictionary Values { get; } = new();

Check warning on line 7 in src/RouteDataFeature.cs

View check run for this annotation

Codecov / codecov/patch

src/RouteDataFeature.cs#L7

Added line #L7 was not covered by tests
}

0 comments on commit 7fdf2d5

Please sign in to comment.