From 69cc79aed00a37c103b9e46488e2001db959a1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Wed, 6 Mar 2024 16:52:28 +0100 Subject: [PATCH] [Instrumentation.Http, Instrumentation.AspNetCore] Always set server.port attribute --- .../CHANGELOG.md | 4 ++++ .../Implementation/HttpInListener.cs | 4 ++-- src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md | 4 ++++ .../Implementation/HttpHandlerDiagnosticListener.cs | 5 +---- .../HttpHandlerMetricsDiagnosticListener.cs | 7 ++----- .../HttpWebRequestActivitySource.netfx.cs | 12 +++--------- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index d3a6f7bd3f4..944adab0c93 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* `server.port` attribute is set even if it has default values (`80` for `HTTP` + and `443` for `HTTPS` protocol). + ([#5419](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5419)) + ## 1.7.1 Released 2024-Feb-09 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 97807d41512..a216a31d298 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -185,9 +185,9 @@ public void OnStartActivity(Activity activity, object payload) { activity.SetTag(SemanticConventions.AttributeServerAddress, request.Host.Host); - if (request.Host.Port is not null && request.Host.Port != 80 && request.Host.Port != 443) + if (request.Host.Port.HasValue) { - activity.SetTag(SemanticConventions.AttributeServerPort, request.Host.Port); + activity.SetTag(SemanticConventions.AttributeServerPort, request.Host.Port.Value); } } diff --git a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md index b634f2b943a..52b95fbda0c 100644 --- a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* `server.port` attribute is set even if it has default values (`80` for `HTTP` + and `443` for `HTTPS` protocol). + ([#5419](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5419)) + ## 1.7.1 Released 2024-Feb-09 diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs index e0625b420db..8132c92fa28 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerDiagnosticListener.cs @@ -147,10 +147,7 @@ public void OnStartActivity(Activity activity, object payload) RequestMethodHelper.SetHttpMethodTag(activity, request.Method.Method); activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host); - if (!request.RequestUri.IsDefaultPort) - { - activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port); - } + activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port); activity.SetTag(SemanticConventions.AttributeUrlFull, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri)); diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs index c6a710e2dd0..6da5982f8f4 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs @@ -50,12 +50,9 @@ public static void OnStopEventWritten(Activity activity, object payload) tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpRequestMethod, httpMethod)); tags.Add(new KeyValuePair(SemanticConventions.AttributeServerAddress, request.RequestUri.Host)); - tags.Add(new KeyValuePair(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme)); + tags.Add(new KeyValuePair(SemanticConventions.AttributeServerPort, request.RequestUri.Port)); - if (!request.RequestUri.IsDefaultPort) - { - tags.Add(new KeyValuePair(SemanticConventions.AttributeServerPort, request.RequestUri.Port)); - } + tags.Add(new KeyValuePair(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme)); if (TryFetchResponse(payload, out HttpResponseMessage response)) { diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs index c27bef64132..81e120c8f7e 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpWebRequestActivitySource.netfx.cs @@ -103,10 +103,7 @@ private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, A RequestMethodHelper.SetHttpMethodTag(activity, request.Method); activity.SetTag(SemanticConventions.AttributeServerAddress, request.RequestUri.Host); - if (!request.RequestUri.IsDefaultPort) - { - activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port); - } + activity.SetTag(SemanticConventions.AttributeServerPort, request.RequestUri.Port); activity.SetTag(SemanticConventions.AttributeUrlFull, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri)); @@ -429,17 +426,14 @@ private static void ProcessResult(IAsyncResult asyncResult, AsyncCallback asyncC tags.Add(new KeyValuePair(SemanticConventions.AttributeHttpRequestMethod, httpMethod)); tags.Add(SemanticConventions.AttributeServerAddress, request.RequestUri.Host); + tags.Add(SemanticConventions.AttributeServerPort, request.RequestUri.Port); + tags.Add(SemanticConventions.AttributeUrlScheme, request.RequestUri.Scheme); if (protocolVersion != null) { tags.Add(SemanticConventions.AttributeNetworkProtocolVersion, HttpTagHelper.GetProtocolVersionString(protocolVersion)); } - if (!request.RequestUri.IsDefaultPort) - { - tags.Add(SemanticConventions.AttributeServerPort, request.RequestUri.Port); - } - if (httpStatusCode.HasValue) { tags.Add(SemanticConventions.AttributeHttpResponseStatusCode, (int)httpStatusCode.Value);