Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Oct 24, 2020
2 parents 87667ad + ce22c5f commit 1141ac5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ public HttpRequestDurationsMiddleware(RequestDelegate next, HttpRequestDurations

public async Task Invoke(HttpContext context)
{
var path = NormalizePath.Execute(context.Request.Path, _options);
#if HasRoutes
var path = _options.UseRouteName
? context.GetRouteName()
: context.Request.Path.ToString();
#else
string path = context.Request.Path.ToString();
#endif

path = NormalizePath.Execute(path, _options);

if (_options.IgnoreRoutesStartWith != null && _options.IgnoreRoutesStartWith.Any(i => path.StartsWith(i)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public class HttpRequestDurationsOptions
/// </summary>
public bool IncludeCustomNormalizePath => CustomNormalizePath != null;

#if HasRoutes
/// <summary>
/// Try to use route name instead of raw url
/// </summary>
public bool UseRouteName { get; set; }
#endif

/// <summary>
/// Should measure request
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Metrics logging of request durations for the Prometheus.Client</Description>
<Copyright>2020 © Serge K, Oleksandr Poliakov</Copyright>
<AssemblyTitle>Prometheus.Client.HttpRequestDurations</AssemblyTitle>
<VersionPrefix>3.1.0</VersionPrefix>
<VersionPrefix>3.2.0</VersionPrefix>
<Authors>Serge K, Oleksandr Poliakov</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp2.2;netcoreapp3.1</TargetFrameworks>
<PackageId>Prometheus.Client.HttpRequestDurations</PackageId>
Expand Down Expand Up @@ -42,4 +42,7 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<DefineConstants>$(DefineConstants);HasRoutes;</DefineConstants>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.AspNetCore.Http;
#if HasRoutes
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
#endif

namespace Prometheus.Client.HttpRequestDurations.Tools
{
internal static class HttpContextExtensions
{
#if HasRoutes
public static string GetRouteName(this HttpContext httpContext)
{
if (httpContext == null)
throw new ArgumentNullException(nameof(httpContext));

var endpoint = httpContext.GetEndpoint();

var result = endpoint?.Metadata.GetMetadata<RouteNameMetadata>()?.RouteName;

if (string.IsNullOrEmpty(result))
{
var routeAttribute = endpoint?.Metadata.GetMetadata<RouteAttribute>();
var methodAttribute = endpoint?.Metadata.GetMetadata<HttpMethodAttribute>();

result = $"{routeAttribute?.Template}{methodAttribute?.Template}";
}

return result;
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Linq;
using Microsoft.AspNetCore.Http;

namespace Prometheus.Client.HttpRequestDurations.Tools
{
internal static class NormalizePath
{
public static string Execute(PathString pathString, HttpRequestDurationsOptions options)
public static string Execute(string pathString, HttpRequestDurationsOptions options)
{
var result = pathString.ToString().ToLowerInvariant();
var result = pathString.ToLowerInvariant();
if (options.IncludeCustomNormalizePath)
result = options.CustomNormalizePath.Aggregate(result, (current, normalizePath) => normalizePath.Key.Replace(current, normalizePath.Value));

Expand Down
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 Microsoft.AspNetCore.Http;
using Prometheus.Client.HttpRequestDurations.Tools;
using Xunit;

Expand Down Expand Up @@ -43,7 +42,7 @@ public static IEnumerable<object[]> GetGuid()
[MemberData(nameof(GetInt))]
public void Int_Center(uint id)
{
var pathString = new PathString($"/path/to/{id}/next");
var pathString = $"/path/to/{id}/next";

var options = new HttpRequestDurationsOptions
{
Expand All @@ -61,7 +60,7 @@ public void Int_Center(uint id)
[MemberData(nameof(GetInt))]
public void Int_Right(uint id) // ID - can't be less 0
{
var pathString = new PathString($"/path/to/{id}");
var pathString = $"/path/to/{id}";

var options = new HttpRequestDurationsOptions
{
Expand All @@ -79,7 +78,7 @@ public void Int_Right(uint id) // ID - can't be less 0
[MemberData(nameof(GetInt))]
public void Int_Right_WithSlash(uint id) // ID - can't be less 0
{
var pathString = new PathString($"/path/to/{id}/");
var pathString = $"/path/to/{id}/";

var options = new HttpRequestDurationsOptions
{
Expand All @@ -97,7 +96,7 @@ public void Int_Right_WithSlash(uint id) // ID - can't be less 0
[MemberData(nameof(GetGuid))]
public void Guid_Center(string guid)
{
var pathString = new PathString($"/path/to/{guid}/next");
var pathString = $"/path/to/{guid}/next";

var options = new HttpRequestDurationsOptions
{
Expand All @@ -115,7 +114,7 @@ public void Guid_Center(string guid)
[MemberData(nameof(GetGuid))]
public void Guid_Right(string guid)
{
var pathString = new PathString($"/path/to/{guid}");
var pathString = $"/path/to/{guid}";

var options = new HttpRequestDurationsOptions
{
Expand All @@ -133,7 +132,7 @@ public void Guid_Right(string guid)
[MemberData(nameof(GetGuid))]
public void Guid_Right_WithSlash(string guid)
{
var pathString = new PathString($"/path/to/{guid}/");
var pathString = $"/path/to/{guid}/";

var options = new HttpRequestDurationsOptions
{
Expand Down

0 comments on commit 1141ac5

Please sign in to comment.