Skip to content

Commit

Permalink
Merge pull request #116 from prom-client-net/refactor/internal-code
Browse files Browse the repository at this point in the history
refactor: internal code
  • Loading branch information
phnx47 authored Jul 14, 2024
2 parents e82f83b + 7fdf2d5 commit 02bd6eb
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 45 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;

namespace Prometheus.Client.HttpRequestDurations.Tools;
namespace Prometheus.Client.HttpRequestDurations;

internal static class HttpContextExtensions
{
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<ICapturedRouteDataFeature>()?.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;
}

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

return notFoundResult;
return "NotAvailable";
}
}
14 changes: 3 additions & 11 deletions src/HttpRequestDurationsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Prometheus.Client.HttpRequestDurations.Tools;

namespace Prometheus.Client.HttpRequestDurations;

Expand Down Expand Up @@ -137,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()));

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

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

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

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

context.Features.Set<ICapturedRouteDataFeature>(capturedRouteData);
context.Features.Set(capturedRouteData);
}
}
2 changes: 1 addition & 1 deletion src/Tools/NormalizePath.cs → src/NormalizePath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;

namespace Prometheus.Client.HttpRequestDurations.Tools;
namespace Prometheus.Client.HttpRequestDurations;

internal static class NormalizePath
{
Expand Down
8 changes: 8 additions & 0 deletions src/RouteDataFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Routing;

namespace Prometheus.Client.HttpRequestDurations;

internal class RouteDataFeature
{
public RouteValueDictionary Values { get; } = new();
}
8 changes: 0 additions & 8 deletions src/Tools/CapturedRouteDataFeature.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/Tools/ICapturedRouteDataFeature.cs

This file was deleted.

1 change: 0 additions & 1 deletion tests/NormalizePathTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Prometheus.Client.HttpRequestDurations.Tools;
using Xunit;

namespace Prometheus.Client.HttpRequestDurations.Tests;
Expand Down

0 comments on commit 02bd6eb

Please sign in to comment.