From 1d50a93571b00968e3da5332333c49e57e355cff Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 16 Jun 2022 14:16:50 -0700 Subject: [PATCH 1/4] ASPNETCore instrumentation to populate httpflavor tag --- .../CHANGELOG.md | 2 + .../Implementation/HttpInListener.cs | 1 + .../Implementation/HttpTagHelper.cs | 55 +++++++++++++++++++ ...stsCollectionsIsAccordingToTheSpecTests.cs | 1 + 4 files changed, 59 insertions(+) create mode 100644 src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 66e2bf0f8bc..3ade41b78fd 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Tracing instrumentation to populate 'http.flavor' tag. + ## 1.0.0-rc9.4 Released 2022-Jun-03 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 8e80800d159..8fcf9d6215f 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -155,6 +155,7 @@ public override void OnStartActivity(Activity activity, object payload) activity.SetTag(SemanticConventions.AttributeHttpMethod, request.Method); activity.SetTag(SemanticConventions.AttributeHttpTarget, path); activity.SetTag(SemanticConventions.AttributeHttpUrl, GetUri(request)); + activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocol(request.Protocol)); var userAgent = request.Headers["User-Agent"].FirstOrDefault(); if (!string.IsNullOrEmpty(userAgent)) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs new file mode 100644 index 00000000000..94899bdd221 --- /dev/null +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs @@ -0,0 +1,55 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +using System; +using System.Collections.Concurrent; + +namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation +{ + /// + /// A collection of helper methods to be used when building Http activities. + /// + internal static class HttpTagHelper + { + private static readonly ConcurrentDictionary ProtocolVersionToStringCache = new(); + + private static readonly Func ConvertProtocolToFlavorStringRef = ConvertProtocolToFlavorString; + + /// + /// Gets the OpenTelemetry standard version tag value for a span based on its protocol/>. + /// + /// . + /// Span flavor value. + public static string GetFlavorTagValueFromProtocol(string protocol) => ProtocolVersionToStringCache.GetOrAdd(protocol, ConvertProtocolToFlavorStringRef); + + private static string ConvertProtocolToFlavorString(string protocol) + { + switch (protocol) + { + case "HTTP/2": + return "2.0"; + + case "HTTP/3": + return "3.0"; + + case "HTTP/1.1": + return "1.1"; + + default: + return protocol; + } + } + } +} diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs index 84927ed7e2e..81576baf340 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/IncomingRequestsCollectionsIsAccordingToTheSpecTests.cs @@ -112,6 +112,7 @@ public async Task SuccessfulTemplateControllerCallGeneratesASpan( Assert.Equal(ActivityKind.Server, activity.Kind); Assert.Equal("localhost", activity.GetTagValue(SemanticConventions.AttributeHttpHost)); Assert.Equal("GET", activity.GetTagValue(SemanticConventions.AttributeHttpMethod)); + Assert.Equal("1.1", activity.GetTagValue(SemanticConventions.AttributeHttpFlavor)); Assert.Equal(urlPath, activity.GetTagValue(SemanticConventions.AttributeHttpTarget)); Assert.Equal($"http://localhost{urlPath}{query}", activity.GetTagValue(SemanticConventions.AttributeHttpUrl)); Assert.Equal(statusCode, activity.GetTagValue(SemanticConventions.AttributeHttpStatusCode)); From f817dc8b34f16f614c3bb04ef29852f6f220a9bc Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Thu, 16 Jun 2022 19:21:46 -0700 Subject: [PATCH 2/4] static dict --- .../Implementation/HttpTagHelper.cs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs index 94899bdd221..2d6929fd16f 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs @@ -15,6 +15,7 @@ // using System; using System.Collections.Concurrent; +using System.Collections.Generic; namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation { @@ -23,32 +24,27 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation /// internal static class HttpTagHelper { - private static readonly ConcurrentDictionary ProtocolVersionToStringCache = new(); - - private static readonly Func ConvertProtocolToFlavorStringRef = ConvertProtocolToFlavorString; + private static readonly Dictionary ProtocolToFlavorTag = new Dictionary + { + { "HTTP/2", "2.0" }, + { "HTTP/3", "3.0" }, + { "HTTP/1.1", "1.1" }, + }; /// /// Gets the OpenTelemetry standard version tag value for a span based on its protocol/>. /// /// . /// Span flavor value. - public static string GetFlavorTagValueFromProtocol(string protocol) => ProtocolVersionToStringCache.GetOrAdd(protocol, ConvertProtocolToFlavorStringRef); - - private static string ConvertProtocolToFlavorString(string protocol) + public static string GetFlavorTagValueFromProtocol(string protocol) { - switch (protocol) + if (ProtocolToFlavorTag.TryGetValue(protocol, out var flavorTag)) { - case "HTTP/2": - return "2.0"; - - case "HTTP/3": - return "3.0"; - - case "HTTP/1.1": - return "1.1"; - - default: - return protocol; + return flavorTag; + } + else + { + return protocol; } } } From 75bc373b39f5d5c346015130e99e5062abc4ef5b Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Fri, 17 Jun 2022 10:12:46 -0700 Subject: [PATCH 3/4] simpler --- .../Implementation/HttpTagHelper.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs index 2d6929fd16f..65767dab0aa 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs @@ -24,13 +24,6 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation /// internal static class HttpTagHelper { - private static readonly Dictionary ProtocolToFlavorTag = new Dictionary - { - { "HTTP/2", "2.0" }, - { "HTTP/3", "3.0" }, - { "HTTP/1.1", "1.1" }, - }; - /// /// Gets the OpenTelemetry standard version tag value for a span based on its protocol/>. /// @@ -38,13 +31,19 @@ internal static class HttpTagHelper /// Span flavor value. public static string GetFlavorTagValueFromProtocol(string protocol) { - if (ProtocolToFlavorTag.TryGetValue(protocol, out var flavorTag)) - { - return flavorTag; - } - else + switch (protocol) { - return protocol; + case "HTTP/2": + return "2.0"; + + case "HTTP/3": + return "3.0"; + + case "HTTP/1.1": + return "1.1"; + + default: + return protocol; } } } From cacf1fc3e23617eae12d8ea3326b409ee8939dd5 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Fri, 17 Jun 2022 10:48:36 -0700 Subject: [PATCH 4/4] fix changelog --- src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md | 1 + .../Implementation/HttpTagHelper.cs | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 3ade41b78fd..f4a4ac365a6 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased * Tracing instrumentation to populate 'http.flavor' tag. + ([3372](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3372)) ## 1.0.0-rc9.4 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs index 65767dab0aa..73114efe0b3 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpTagHelper.cs @@ -13,9 +13,6 @@ // See the License for the specific language governing permissions and // limitations under the License. // -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation {