Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,30 @@ public AspNetCoreHttpRequestHandler(

public string GetDefaultResourceName(HttpRequest request)
{
// Be defensive: in some edge cases the HttpRequest object may be null (e.g.,
// framework- or host-specific error paths). Avoid throwing and return a safe default.
if (request is null)
{
return "UNKNOWN /";
}

string httpMethod = request.Method?.ToUpperInvariant() ?? "UNKNOWN";

string absolutePath = request.PathBase.HasValue
? request.PathBase.ToUriComponent() + request.Path.ToUriComponent()
: request.Path.ToUriComponent();
string absolutePath;
try
{
absolutePath = request.PathBase.HasValue
? request.PathBase.ToUriComponent() + request.Path.ToUriComponent()
: request.Path.ToUriComponent();
}
catch
{
// If anything goes wrong building the path, fall back to root
absolutePath = "/";
}

string resourceUrl = UriHelpers.GetCleanUriPath(absolutePath)
.ToLowerInvariant();
var cleanedPath = UriHelpers.GetCleanUriPath(absolutePath);
string resourceUrl = (cleanedPath ?? "/").ToLowerInvariant();

return $"{httpMethod} {resourceUrl}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#if !NETFRAMEWORK
using Datadog.Trace.PlatformHelpers;
using Datadog.Trace.Configuration;
using Datadog.Trace.Logging;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
Expand All @@ -16,6 +18,19 @@ public class AspNetCoreHttpRequestHandlerTests
{
public const string OriginalPath = "/somepath/Home/Index";

[Fact]
public void GetDefaultResourceName_ReturnsUnknownSlash_WhenRequestIsNull()
{
var handler = new AspNetCoreHttpRequestHandler(
DatadogLogging.GetLoggerFor<AspNetCoreHttpRequestHandlerTests>(),
"aspnet_core.request",
IntegrationId.AspNetCore);

var result = handler.GetDefaultResourceName(null);

result.Should().Be("UNKNOWN /");
}

[Theory]
[InlineData(null, "/somepath/Home/Index")]
[InlineData("", "/somepath/Home/Index")]
Expand Down
Loading