Skip to content

Commit

Permalink
Fix url.path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Mar 11, 2024
1 parent 78e266a commit 86fdf5a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void OnStartActivity(Activity activity, HttpContext context)
activity.SetTag(SemanticConventions.AttributeHttpRequestMethodOriginal, originalHttpMethod);
}

activity.SetTag(SemanticConventions.AttributeHttpTarget, path);
activity.SetTag(SemanticConventions.AttributeUrlPath, path);
activity.SetTag(SemanticConventions.AttributeUserAgentOriginal, request.UserAgent);
activity.SetTag(SemanticConventions.AttributeHttpUrl, GetUriTagValueFromRequestUri(request.Url));

Expand Down
2 changes: 2 additions & 0 deletions src/Shared/SemanticConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ internal static class SemanticConventions
public const string AttributeHttpRequestMethodOriginal = "http.request.method_original";
public const string AttributeHttpResponseStatusCode = "http.response.status_code"; // replaces: "http.status_code" (AttributeHttpStatusCode)
public const string AttributeUrlScheme = "url.scheme"; // replaces: "http.scheme" (AttributeHttpScheme)
public const string AttributeUrlPath = "url.path"; // replaces: "http.target" (AttributeHttpTarget)
public const string AttributeUrlQuery = "url.query"; // replaces: "http.target" (AttributeHttpTarget)

// v1.23.0
// https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-metrics.md#http-server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ namespace OpenTelemetry.Instrumentation.AspNet.Tests;
public class HttpInListenerTests
{
[Theory]
[InlineData("http://localhost/", "http://localhost/", "localhost", 80, "GET", "GET", null, 0, null)]
[InlineData("http://localhost/", "http://localhost/", "localhost", 80, "POST", "POST", null, 0, null, true)]
[InlineData("https://localhost/", "https://localhost/", "localhost", 443, "NonStandard", "_OTHER", "NonStandard", 0, null)]
[InlineData("https://localhost/", "https://user:pass@localhost/", "localhost", 443, "GET", "GET", null, 0, null)] // Test URL sanitization
[InlineData("http://localhost:443/", "http://localhost:443/", "localhost", 443, "GET", "GET", null, 0, null)] // Test http over 443
[InlineData("https://localhost:80/", "https://localhost:80/", "localhost", 80, "GET", "GET", null, 0, null)] // Test https over 80
[InlineData("https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "localhost", 80, "GET", "GET", null, 0, null)] // Test complex URL
[InlineData("https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "https://user:password@localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "localhost", 80, "GET", "GET", null, 0, null)] // Test complex URL sanitization
[InlineData("http://localhost:80/Index", "http://localhost:80/Index", "localhost", 80, "GET", "GET", null, 1, "{controller}/{action}/{id}")]
[InlineData("https://localhost:443/about_attr_route/10", "https://localhost:443/about_attr_route/10", "localhost", 443, "GET", "GET", null, 2, "about_attr_route/{customerId}")]
[InlineData("http://localhost:1880/api/weatherforecast", "http://localhost:1880/api/weatherforecast", "localhost", 1880, "GET", "GET", null, 3, "api/{controller}/{id}")]
[InlineData("https://localhost:1843/subroute/10", "https://localhost:1843/subroute/10", "localhost", 1843, "GET", "GET", null, 4, "subroute/{customerId}")]
[InlineData("http://localhost/api/value", "http://localhost/api/value", "localhost", 80, "GET", "GET", null, 0, null, false, "/api/value")] // Request will be filtered
[InlineData("http://localhost/api/value", "http://localhost/api/value", "localhost", 80, "GET", "GET", null, 0, null, false, "{ThrowException}")] // Filter user code will throw an exception
[InlineData("http://localhost/", "http://localhost/", "localhost", 80, "GET", "GET", null, 0, null, false, null, true)] // Test RecordException option
[InlineData("http://localhost/", "http://localhost/", "/", "localhost", 80, "GET", "GET", null, 0, null)]
[InlineData("http://localhost/", "http://localhost/", "/", "localhost", 80, "POST", "POST", null, 0, null, true)]
[InlineData("https://localhost/", "https://localhost/", "/", "localhost", 443, "NonStandard", "_OTHER", "NonStandard", 0, null)]
[InlineData("https://localhost/", "https://user:pass@localhost/", "/", "localhost", 443, "GET", "GET", null, 0, null)] // Test URL sanitization
[InlineData("http://localhost:443/", "http://localhost:443/", "/", "localhost", 443, "GET", "GET", null, 0, null)] // Test http over 443
[InlineData("https://localhost:80/", "https://localhost:80/", "/", "localhost", 80, "GET", "GET", null, 0, null)] // Test https over 80
[InlineData("https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "/Home/Index.htm", "localhost", 80, "GET", "GET", null, 0, null)] // Test complex URL
[InlineData("https://localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "https://user:password@localhost:80/Home/Index.htm?q1=v1&q2=v2#FragmentName", "/Home/Index.htm", "localhost", 80, "GET", "GET", null, 0, null)] // Test complex URL sanitization
[InlineData("http://localhost:80/Index", "http://localhost:80/Index", "/Index", "localhost", 80, "GET", "GET", null, 1, "{controller}/{action}/{id}")]
[InlineData("https://localhost:443/about_attr_route/10", "https://localhost:443/about_attr_route/10", "/about_attr_route/10", "localhost", 443, "GET", "GET", null, 2, "about_attr_route/{customerId}")]
[InlineData("http://localhost:1880/api/weatherforecast", "http://localhost:1880/api/weatherforecast", "/api/weatherforecast", "localhost", 1880, "GET", "GET", null, 3, "api/{controller}/{id}")]
[InlineData("https://localhost:1843/subroute/10", "https://localhost:1843/subroute/10", "/subroute/10", "localhost", 1843, "GET", "GET", null, 4, "subroute/{customerId}")]
[InlineData("http://localhost/api/value", "http://localhost/api/value", "/api/value", "localhost", 80, "GET", "GET", null, 0, null, false, "/api/value")] // Request will be filtered
[InlineData("http://localhost/api/value", "http://localhost/api/value", "/api/value", "localhost", 80, "GET", "GET", null, 0, null, false, "{ThrowException}")] // Filter user code will throw an exception
[InlineData("http://localhost/", "http://localhost/", "/", "localhost", 80, "GET", "GET", null, 0, null, false, null, true)] // Test RecordException option
public void AspNetRequestsAreCollectedSuccessfully(
string expectedUrl,
string url,
string expectedUrlPath,
string expectedHost,
int expectedPort,
string requestMethod,
Expand Down Expand Up @@ -141,7 +142,7 @@ public void AspNetRequestsAreCollectedSuccessfully(
Assert.Equal(expectedRequestMethod, span.GetTagValue("http.request.method"));
Assert.Equal(expectedOriginalRequestMethod, span.GetTagValue("http.request.method_original"));

Assert.Equal(HttpContext.Current.Request.Path, span.GetTagValue(SemanticConventions.AttributeHttpTarget) as string);
Assert.Equal(expectedUrlPath, span.GetTagValue("url.path"));
Assert.Equal("Custom User Agent v1.2.3", span.GetTagValue("user_agent.original"));

if (recordException)
Expand Down

0 comments on commit 86fdf5a

Please sign in to comment.