Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move method
  • Loading branch information
alanwest committed Nov 3, 2023
commit e75b8710bbb081f47f650fbb3f39646daeea3337
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
#if !NETSTANDARD2_0
using System.Runtime.CompilerServices;
#endif
#if !NETSTANDARD
using System.Text.RegularExpressions;
#endif
using Microsoft.AspNetCore.Http;
#if !NETSTANDARD
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Diagnostics;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -436,7 +431,7 @@ public void OnMvcBeforeAction(Activity activity, object payload)
// the activity ends.
if (actionDescriptor != null && (string.IsNullOrEmpty(template) || actionDescriptor is PageActionDescriptor))
{
var httpRoute = GetHttpRouteFromActionDescriptor(httpContext, actionDescriptor);
var httpRoute = HttpTagHelper.GetHttpRouteFromActionDescriptor(httpContext, actionDescriptor);
this.GetDisplayNameAndHttpMethod(httpContext.Request.Method, httpRoute, out var _, out var displayName);
activity.DisplayName = displayName;
activity.SetTag(SemanticConventions.AttributeHttpRoute, httpRoute);
Expand Down Expand Up @@ -488,27 +483,6 @@ static bool TryFetchException(object payload, out Exception exc)
=> ExceptionPropertyFetcher.TryFetch(payload, out exc) && exc != null;
}

#if !NETSTANDARD
private static string GetHttpRouteFromActionDescriptor(HttpContext httpContext, ActionDescriptor actionDescriptor)
{
var result = (httpContext.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText;

if (actionDescriptor is ControllerActionDescriptor cad)
{
var controllerRegex = new Regex(@"\{controller=.*?\}+?");
var actionRegex = new Regex(@"\{action=.*?\}+?");
result = controllerRegex.Replace(result, cad.ControllerName);
result = actionRegex.Replace(result, cad.ActionName);
}
else if (actionDescriptor is PageActionDescriptor pad)
{
result = pad.ViewEnginePath;
}

return result;
}
#endif

private static string GetUri(HttpRequest request)
{
// this follows the suggestions from https://github.com/dotnet/aspnetcore/issues/28906
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,44 @@
// limitations under the License.
// </copyright>

using System.Text.RegularExpressions;
#if !NETSTANDARD
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Routing;
#endif

namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation;

/// <summary>
/// A collection of helper methods to be used when building Http activities.
/// </summary>
internal static class HttpTagHelper
{
private static readonly Regex ControllerNameRegex = new(@"\{controller=.*?\}+?", RegexOptions.Compiled);
private static readonly Regex ActionNameRegex = new(@"\{action=.*?\}+?", RegexOptions.Compiled);

#if !NETSTANDARD
public static string GetHttpRouteFromActionDescriptor(HttpContext httpContext, ActionDescriptor actionDescriptor)
{
var result = (httpContext.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText;

if (actionDescriptor is ControllerActionDescriptor cad)
{
result = ControllerNameRegex.Replace(result, cad.ControllerName);
result = ActionNameRegex.Replace(result, cad.ActionName);
}
else if (actionDescriptor is PageActionDescriptor pad)
{
result = pad.ViewEnginePath;
}

return result;
}
#endif

/// <summary>
/// Gets the OpenTelemetry standard version tag value for a span based on its protocol/>.
/// </summary>
Expand Down