dd-trace-dotnet icon indicating copy to clipboard operation
dd-trace-dotnet copied to clipboard

[BUG]: Endpoint names contain version placeholders when using ASP.NET API Versioning

Open IGx89 opened this issue 9 months ago • 1 comments

Tracer Version(s)

3.13.0

Operating system and platform

Linux (x64)

Instrumentation Mode

manual with NuGet package

TFM

net8.0

Bug Report

Most of our APIs at my org use ASP.NET API Versioning with its SubstituteApiVersionInUrl option set to true, and have controllers with routes such as v{version:apiVersion}/helloworld. Our endpoints in our OpenAPI specs show up as v1/helloworld, but in Datadog (Endpoints in APM, http.route in the span) they show up like v{version}/helloworld. Is there any way the tracer could handle that situation better, so Datadog shows the resolved URL of the endpoint? Besides being an annoyance, it breaks the mapping between the API definition in the Software Catalog (which has v1/helloworld since that's in the OpenAPI spec) and the endpoints on the service page.

Reproduction Code

// Controller
[ApiVersion( 1.0 )]
[Route( "api/v{version:apiVersion}/[controller]" )]
public class HelloWorldController : ControllerBase
{
    // GET api/v{version}/helloworld
    [HttpGet]
    public IActionResult Get( ApiVersion apiVersion ) => Ok( new { Controller = GetType().Name, Version = apiVersion.ToString() } );
}

// Program.cs
builder.Services.AddApiVersioning()
	.AddMvc()
	.AddApiExplorer(options => options.SubstituteApiVersionInUrl = true);

Example project: https://github.com/dotnet/aspnet-api-versioning/tree/main/examples/AspNetCore/WebApi/OpenApiExample

IGx89 avatar Apr 01 '25 21:04 IGx89

Hi @IGx89,

Looking at this we simply create the span too soon and the route we get is what is shown with the {version} placeholders.

I'm unsure what the best path forward here is at the moment, so I'll gather some more feedback from the team. From my testing we may need to do something like so https://github.com/DataDog/dd-trace-dotnet/commit/7908b01fc9b11632eafb1ac0802e6f8d319b098c to support this.

bouwkast avatar Apr 07 '25 14:04 bouwkast