From 9d32dd648c05e96e5995dc8793e669a347c86dd0 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 4 Aug 2023 01:33:07 +0200 Subject: [PATCH] [HTTP Metrics] Shorter version tags (#89959) Addressing https://github.com/dotnet/runtime/pull/89809#discussion_r1283541867 --- .../System/Net/Http/Metrics/MetricsHandler.cs | 4 ++-- .../SocketsHttpHandler/HttpConnectionBase.cs | 4 ++-- .../Metrics/SocketsHttpHandlerMetrics.cs | 4 ++-- .../tests/FunctionalTests/MetricsTest.cs | 17 ++++++++++++----- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs index 02826aa593d29f..bf92139284f57d 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs @@ -161,8 +161,8 @@ private static string GetErrorReason(Exception exception) { (1, 0) => "1.0", (1, 1) => "1.1", - (2, 0) => "2.0", - (3, 0) => "3.0", + (2, 0) => "2", + (3, 0) => "3", _ => httpVersion.ToString() }; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionBase.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionBase.cs index b67ea5ac48d62b..f472762e679959 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionBase.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionBase.cs @@ -47,8 +47,8 @@ public HttpConnectionBase(HttpConnectionPool pool, IPEndPoint? remoteEndPoint) // While requests may report HTTP/1.0 as the protocol, we treat all HTTP/1.X connections as HTTP/1.1. string protocol = this is HttpConnection ? "1.1" : - this is Http2Connection ? "2.0" : - "3.0"; + this is Http2Connection ? "2" : + "3"; _connectionMetrics = new ConnectionMetrics( metrics, diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/SocketsHttpHandlerMetrics.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/SocketsHttpHandlerMetrics.cs index d245de835080c4..e560435cf48a00 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/SocketsHttpHandlerMetrics.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Metrics/SocketsHttpHandlerMetrics.cs @@ -34,8 +34,8 @@ public void RequestLeftQueue(HttpRequestMessage request, HttpConnectionPool pool tags.Add("network.protocol.version", versionMajor switch { 1 => "1.1", - 2 => "2.0", - _ => "3.0" + 2 => "2", + _ => "3" }); tags.Add("url.scheme", pool.IsSecure ? "https" : "http"); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs index 3b601cf05e1aae..bdca584723a2ea 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs @@ -63,6 +63,13 @@ private static void VerifySchemeHostPortTags(KeyValuePair[] tag VerifyTag(tags, "server.port", uri.Port); } + private static string? GetVersionString(Version? version) => version == null ? null : version.Major switch + { + 1 => "1.1", + 2 => "2", + _ => "3" + }; + protected static void VerifyRequestDuration(Measurement measurement, Uri uri, Version? protocolVersion, @@ -75,7 +82,7 @@ protected static void VerifyRequestDuration(string instrumentName, double measurement, KeyValuePair[] tags, Uri uri, - Version? protocol, + Version? protocolVersion, int? statusCode, string method = "GET", string[] acceptedErrorReasons = null) @@ -84,7 +91,7 @@ protected static void VerifyRequestDuration(string instrumentName, Assert.InRange(measurement, double.Epsilon, 60); VerifySchemeHostPortTags(tags, uri); VerifyTag(tags, "http.request.method", method); - VerifyTag(tags, "network.protocol.version", protocol?.ToString()); + VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion)); VerifyTag(tags, "http.response.status_code", statusCode); if (acceptedErrorReasons == null) { @@ -113,7 +120,7 @@ protected static void VerifyOpenConnections(string actualName, object measuremen Assert.Equal(InstrumentNames.OpenConnections, actualName); Assert.Equal(expectedValue, Assert.IsType(measurement)); VerifySchemeHostPortTags(tags, uri); - VerifyTag(tags, "network.protocol.version", protocolVersion.ToString()); + VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion)); VerifyTag(tags, "http.connection.state", state); VerifySocketAddress(tags); } @@ -124,7 +131,7 @@ protected static void VerifyConnectionDuration(string instrumentName, object mea double value = Assert.IsType(measurement); Assert.InRange(value, double.Epsilon, 60); VerifySchemeHostPortTags(tags, uri); - VerifyTag(tags, "network.protocol.version", protocolVersion.ToString()); + VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion)); VerifySocketAddress(tags); } @@ -134,7 +141,7 @@ protected static void VerifyTimeInQueue(string instrumentName, object measuremen double value = Assert.IsType(measurement); Assert.InRange(value, double.Epsilon, 60); VerifySchemeHostPortTags(tags, uri); - VerifyTag(tags, "network.protocol.version", protocolVersion.ToString()); + VerifyTag(tags, "network.protocol.version", GetVersionString(protocolVersion)); VerifyTag(tags, "http.request.method", method); }