Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent names for Transactions representing ASP.NET Core routes #2808

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ without native/platform specific bindings and SDKs. See [this ticket for more de
- When compiling AOT, Sentry isn't able to automatically register an unhandled exception handler in WinUI applications, since that also relies on reflection. If you're using Sentry with a WinUI application and you want to use AOT compilation, you'll need to take care of registering the unhandled event handler yourself. TODO *** Fill in the gaps here when https://github.com/getsentry/sentry-dotnet/issues/2778 has been completed ***
- ([#2732](https://github.com/getsentry/sentry-dotnet/pull/2732)), ([#2793](https://github.com/getsentry/sentry-dotnet/pull/2793))
- The TracePropagationTarget class has been removed. Use the SubstringOrRegexPattern class instead. ([#2763](https://github.com/getsentry/sentry-dotnet/pull/2763))
- Transaction names for ASP.NET Core are now consistently named `HTTP-VERB /path` (e.g. `GET /home`). Previously the leading forward slash might not have been present for some endpoints ([#2808](https://github.com/getsentry/sentry-dotnet/pull/2808))

#### Sentry.Google.Cloud.Functions

Expand Down
4 changes: 3 additions & 1 deletion src/Sentry.AspNetCore/RouteUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ internal static class RouteUtils
builder.Append(routePattern);
}

return builder.ToString();
// Force a leading slash (if there isn't already one present)
var url = builder.ToString();
return url.Length >0 && url[0] == '/' ? url : $"/{url}";
}

// Internal for testing.
Expand Down
36 changes: 18 additions & 18 deletions test/Sentry.AspNetCore.Tests/RouteUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ private static void AddRouteValuesIfNotNull(RouteValueDictionary route, string k
}

[Theory]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "myPath/theArea/house/about/{id?}", "house", "about", "theArea")]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "myPath/{area=MyArea}/house/about/{id?}", "house", "about", null)]
[InlineData("{area=}/{controller=}/{action=}/{id?}", "myPath/{area=}/{controller=}/{action=}/{id?}", "house", "about", "theArea")]
[InlineData("{controller=Home}/{action=Index}/{id?}", "myPath/house/about/{id?}", "house", "about", null)]
[InlineData("{controller=Home}/{action=Index}", "myPath/house/about", "house", "about", null)]
[InlineData("{controller=Home}/{id?}", "myPath/house/{id?}", "house", "about", null)]
[InlineData("{action=Index}/{id?}", "myPath/about/{id?}", null, "about", null)]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "/myPath/theArea/house/about/{id?}", "house", "about", "theArea")]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "/myPath/{area=MyArea}/house/about/{id?}", "house", "about", null)]
[InlineData("{area=}/{controller=}/{action=}/{id?}", "/myPath/{area=}/{controller=}/{action=}/{id?}", "house", "about", "theArea")]
[InlineData("{controller=Home}/{action=Index}/{id?}", "/myPath/house/about/{id?}", "house", "about", null)]
[InlineData("{controller=Home}/{action=Index}", "/myPath/house/about", "house", "about", null)]
[InlineData("{controller=Home}/{id?}", "/myPath/house/{id?}", "house", "about", null)]
[InlineData("{action=Index}/{id?}", "/myPath/about/{id?}", null, "about", null)]
public void NewRouteFormat_MvcRouteWithPathBase_ParsedParameters(string routeInput, string expectedOutput, string controller, string action, string area)
{
// Arrange
Expand All @@ -48,14 +48,14 @@ public void NewRouteFormat_MvcRouteWithPathBase_ParsedParameters(string routeInp
}

[Theory]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "theArea/house/about/{id?}", "house", "about", "theArea", null)]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "{area=MyArea}/house/about/{id?}", "house", "about", null, null)]
[InlineData("{area=}/{controller=}/{action=}/{id?}", "{area=}/{controller=}/{action=}/{id?}", "house", "about", "theArea", null)]
[InlineData("{controller=Home}/{action=Index}/{id?}", "house/about/{id?}", "house", "about", null, null)]
[InlineData("{controller=Home}/{action=Index}", "house/about", "house", "about", null, null)]
[InlineData("{controller=Home}/{id?}", "house/{id?}", "house", "about", null, null)]
[InlineData("{action=Index}/{id?}", "about/{id?}", null, "about", null, null)]
[InlineData("v{version:apiVersion}/Target", "v1.1/Target", null, "about", null, "1.1")]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "/theArea/house/about/{id?}", "house", "about", "theArea", null)]
[InlineData("{area=MyArea}/{controller=Home}/{action=Index}/{id?}", "/{area=MyArea}/house/about/{id?}", "house", "about", null, null)]
[InlineData("{area=}/{controller=}/{action=}/{id?}", "/{area=}/{controller=}/{action=}/{id?}", "house", "about", "theArea", null)]
[InlineData("{controller=Home}/{action=Index}/{id?}", "/house/about/{id?}", "house", "about", null, null)]
[InlineData("{controller=Home}/{action=Index}", "/house/about", "house", "about", null, null)]
[InlineData("{controller=Home}/{id?}", "/house/{id?}", "house", "about", null, null)]
[InlineData("{action=Index}/{id?}", "/about/{id?}", null, "about", null, null)]
[InlineData("v{version:apiVersion}/Target", "/v1.1/Target", null, "about", null, "1.1")]
public void NewRouteFormat_MvcRouteWithoutPathBase_ParsedParameters(string routeInput, string expectedOutput, string controller, string action, string area, string version)
{
// Arrange
Expand All @@ -69,9 +69,9 @@ public void NewRouteFormat_MvcRouteWithoutPathBase_ParsedParameters(string route
}

[Theory]
[InlineData("myPath/some/Path", "/myPath", "some/Path")]
[InlineData("some/Path", null, "some/Path")]
[InlineData("api/health", "/api", "/health")]
[InlineData("/myPath/some/Path", "/myPath", "some/Path")]
[InlineData("/some/Path", null, "some/Path")]
[InlineData("/api/health", "/api", "/health")]
[InlineData(null, null, "")]
[InlineData(null, null, null)]
public void NewRouteFormat_WithPathBase_MatchesExpectedRoute(string expectedRoute, string pathBase, string rawRoute)
Expand Down
42 changes: 42 additions & 0 deletions test/Sentry.AspNetCore.Tests/SentryTracingMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,48 @@ public async Task Transaction_is_started_automatically_from_incoming_trace_heade
);
}

[Fact]
public async Task Transaction_name_includes_slash_prefix()
{
// Arrange
var sentryClient = Substitute.For<ISentryClient>();

var hub = new Hub(new SentryOptions { Dsn = ValidDsn, TracesSampleRate = 1 }, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
.UseSentry()
.ConfigureServices(services =>
{
services.AddRouting();

services.RemoveAll(typeof(Func<IHub>));
services.AddSingleton<Func<IHub>>(() => hub);
})
.Configure(app =>
{
app.UseRouting();

app.UseEndpoints(routes => routes.Map("foo", _ => Task.CompletedTask));
}));

var client = server.CreateClient();
Transaction transaction = null;
sentryClient.CaptureTransaction(
Arg.Do<Transaction>(t => transaction = t),
Arg.Any<Hint>()
);

// Act
using var request = new HttpRequestMessage(HttpMethod.Get, "foo");

await client.SendAsync(request);

// Assert
transaction.Should().NotBeNull();
transaction.Name.Should().Be("GET /foo");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Payloads: [
{
Source: {
Name: GET v1.1/Target,
Name: GET /v1.1/Target,
NameSource: Route,
Platform: csharp,
Operation: http.server,
Expand Down