From 2e7b7a9308f785f0d6041fe82eeaab1ef24b750d Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 15 Nov 2022 11:57:32 -0800 Subject: [PATCH] [HttpClient] Add `net.peer.name` and `net.peer.port` on metric dimensions (#3907) * Add net.peer.name and net.peer.port on metric * changelog * rmv todo --- .../CHANGELOG.md | 8 +++-- .../HttpHandlerMetricsDiagnosticListener.cs | 31 ++++++++++++++----- .../HttpClientTests.cs | 6 +++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md index 7eefd7d311a..f7d6a98cd2f 100644 --- a/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md @@ -2,8 +2,12 @@ ## Unreleased -* **Breaking change** `http.host` will no longer be populated. `net.peer.name` - and `net.peer.port` attributes will be populated instead. +* Added `net.peer.name` and `net.peer.port` as dimensions on + `http.client.duration` metric. + ([#3907](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3907)) + +* **Breaking change** `http.host` will no longer be populated on activity. + `net.peer.name` and `net.peer.port` attributes will be populated instead. ([#3832](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3832)) ## 1.0.0-rc9.9 diff --git a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs index 8b2ebf0a678..bf120ff3f73 100644 --- a/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.Http/Implementation/HttpHandlerMetricsDiagnosticListener.cs @@ -49,15 +49,30 @@ public override void OnEventWritten(string name, object payload) { var request = response.RequestMessage; - // TODO: This is just a minimal set of attributes. See the spec for additional attributes: - // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/http-metrics.md#http-client - var tags = new KeyValuePair[] + TagList tags; + if (!request.RequestUri.IsDefaultPort) { - new KeyValuePair(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)), - new KeyValuePair(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme), - new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode), - new KeyValuePair(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)), - }; + tags = new TagList + { + new KeyValuePair(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)), + new KeyValuePair(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme), + new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode), + new KeyValuePair(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)), + new KeyValuePair(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host), + new KeyValuePair(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port), + }; + } + else + { + tags = new TagList + { + new KeyValuePair(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method)), + new KeyValuePair(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme), + new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, (int)response.StatusCode), + new KeyValuePair(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version)), + new KeyValuePair(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host), + }; + } this.httpClientDuration.Record(activity.Duration.TotalMilliseconds, tags); } diff --git a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.cs b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.cs index 5e4d644450c..d7efa83efe4 100644 --- a/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.cs +++ b/test/OpenTelemetry.Instrumentation.Http.Tests/HttpClientTests.cs @@ -200,11 +200,15 @@ public async Task HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOut var scheme = new KeyValuePair(SemanticConventions.AttributeHttpScheme, "http"); var statusCode = new KeyValuePair(SemanticConventions.AttributeHttpStatusCode, tc.ResponseCode == 0 ? 200 : tc.ResponseCode); var flavor = new KeyValuePair(SemanticConventions.AttributeHttpFlavor, "2.0"); + var hostName = new KeyValuePair(SemanticConventions.AttributeNetPeerName, host); + var portNumber = new KeyValuePair(SemanticConventions.AttributeNetPeerPort, port); Assert.Contains(method, attributes); Assert.Contains(scheme, attributes); Assert.Contains(statusCode, attributes); Assert.Contains(flavor, attributes); - Assert.Equal(4, attributes.Length); + Assert.Contains(hostName, attributes); + Assert.Contains(portNumber, attributes); + Assert.Equal(6, attributes.Length); #endif } else