From 5285a24ce95469099400093d64ced4f0134cc470 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 5 Dec 2023 09:52:50 -0800 Subject: [PATCH 01/11] Add grpc instrumentation as experimental feature --- .../AspNetCoreTraceInstrumentationOptions.cs | 46 +++++++--- .../Implementation/HttpInListener.cs | 15 ++-- ...elemetry.Instrumentation.AspNetCore.csproj | 3 + .../TracerProviderBuilderExtensions.cs | 4 + .../GrpcTests.server.cs | 85 ++++++++++--------- 5 files changed, 90 insertions(+), 63 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs index 69fe58f042..6caadf3db9 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs @@ -16,6 +16,10 @@ using System.Diagnostics; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +#if NET6_0_OR_GREATER +using OpenTelemetry.Internal; +#endif namespace OpenTelemetry.Instrumentation.AspNetCore; @@ -24,6 +28,26 @@ namespace OpenTelemetry.Instrumentation.AspNetCore; /// public class AspNetCoreTraceInstrumentationOptions { + /// + /// Initializes a new instance of the class. + /// + public AspNetCoreTraceInstrumentationOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) + { + } + + internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration) + { + Debug.Assert(configuration != null, "configuration was null"); + +#if NET6_0_OR_GREATER + if (configuration.TryGetBoolValue("OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION", out var enableGrpcInstrumentation)) + { + this.EnableGrpcAspNetCoreSupport = enableGrpcInstrumentation; + } +#endif + } + /// /// Gets or sets a filter function that determines whether or not to /// collect telemetry on a per request basis. @@ -77,17 +101,13 @@ public class AspNetCoreTraceInstrumentationOptions /// public bool RecordException { get; set; } - /* - * Removing for stable release of http instrumentation. - * grpc semantic conventions are not yet stable so this option will not be part of stable package. - #if NET6_0_OR_GREATER - /// - /// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. Default is true. - /// - /// - /// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md. - /// - public bool EnableGrpcAspNetCoreSupport { get; set; } = true; - #endif - */ +#if NET6_0_OR_GREATER + /// + /// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. Default is true. + /// + /// + /// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md. + /// + internal bool EnableGrpcAspNetCoreSupport { get; set; } +#endif } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index b10c8a6c4c..dc812440be 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -252,14 +252,13 @@ public void OnStopActivity(Activity activity, object payload) #endif activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); - /* - #if !NETSTANDARD2_0 - - if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod)) - { - this.AddGrpcAttributes(activity, grpcMethod, context); - } - */ + +#if !NETSTANDARD2_0 + if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod)) + { + this.AddGrpcAttributes(activity, grpcMethod, context); + } +#endif if (activity.Status == ActivityStatusCode.Unset) { activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, response.StatusCode)); diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj index e3782aa277..d183d1b76d 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj @@ -11,11 +11,13 @@ + + @@ -32,6 +34,7 @@ + diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs index 357cbad6f0..d61d5b6d4d 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs @@ -75,6 +75,10 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation( { services.Configure(name, configureAspNetCoreTraceInstrumentationOptions); } + +#if !NETSTANDARD2_0 + services.RegisterOptionsFactory(configuration => new AspNetCoreTraceInstrumentationOptions(configuration)); +#endif }); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) diff --git a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs index 73ce23b4c0..3fd4c88559 100644 --- a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs +++ b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs @@ -16,9 +16,20 @@ #if NET6_0_OR_GREATER using System.Diagnostics; +using System.Net; +using Greet; +using Grpc.Core; +using Grpc.Net.Client; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Moq; +using OpenTelemetry.Context.Propagation; using OpenTelemetry.Instrumentation.Grpc.Services.Tests; +using OpenTelemetry.Instrumentation.GrpcNetClient; +using OpenTelemetry.Trace; using Xunit; +using static OpenTelemetry.Internal.HttpSemanticConventionHelper; +using Status = OpenTelemetry.Trace.Status; namespace OpenTelemetry.Instrumentation.Grpc.Tests; @@ -35,34 +46,26 @@ public GrpcTests() this.server = new GrpcServer(); } - /* [Theory] [InlineData(null)] - [InlineData(true)] - [InlineData(false)] - public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(bool? enableGrpcAspNetCoreSupport) + [InlineData("true")] + [InlineData("false")] + [InlineData("True")] + [InlineData("False")] + public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(string enableGrpcAspNetCoreSupport) { var configuration = new ConfigurationBuilder() - .AddInMemoryCollection(new Dictionary { [SemanticConventionOptInKeyName] = "http" }) + .AddInMemoryCollection(new Dictionary + { + [SemanticConventionOptInKeyName] = "http", + ["OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, + }) .Build(); var exportedItems = new List(); - var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder() - .ConfigureServices(services => services.AddSingleton(configuration)); - - if (enableGrpcAspNetCoreSupport.HasValue) - { - tracerProviderBuilder.AddAspNetCoreInstrumentation(options => - { - options.EnableGrpcAspNetCoreSupport = enableGrpcAspNetCoreSupport.Value; - }); - } - else - { - tracerProviderBuilder.AddAspNetCoreInstrumentation(); - } - - using var tracerProvider = tracerProviderBuilder + using var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => services.AddSingleton(configuration)) + .AddAspNetCoreInstrumentation() .AddInMemoryExporter(exportedItems) .Build(); @@ -80,7 +83,7 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(bool? enableGrpcA Assert.Equal(ActivityKind.Server, activity.Kind); - if (!enableGrpcAspNetCoreSupport.HasValue || enableGrpcAspNetCoreSupport.Value) + if (enableGrpcAspNetCoreSupport != null && enableGrpcAspNetCoreSupport.Equals("true", StringComparison.OrdinalIgnoreCase)) { Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem)); Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService)); @@ -113,30 +116,28 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(bool? enableGrpcA [Theory(Skip = "Skipping for .NET 6 and higher due to bug #3023")] #endif [InlineData(null)] - [InlineData(true)] - [InlineData(false)] - public void GrpcAspNetCoreInstrumentationAddsCorrectAttributesWhenItCreatesNewActivity(bool? enableGrpcAspNetCoreSupport) + [InlineData("true")] + [InlineData("false")] + [InlineData("True")] + [InlineData("False")] + public void GrpcAspNetCoreInstrumentationAddsCorrectAttributesWhenItCreatesNewActivity(string enableGrpcAspNetCoreSupport) { try { // B3Propagator along with the headers passed to the client.SayHello ensure that the instrumentation creates a sibling activity Sdk.SetDefaultTextMapPropagator(new Extensions.Propagators.B3Propagator()); var exportedItems = new List(); - var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder(); - - if (enableGrpcAspNetCoreSupport.HasValue) - { - tracerProviderBuilder.AddAspNetCoreInstrumentation(options => - { - options.EnableGrpcAspNetCoreSupport = enableGrpcAspNetCoreSupport.Value; - }); - } - else - { - tracerProviderBuilder.AddAspNetCoreInstrumentation(); - } - - using var tracerProvider = tracerProviderBuilder + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + [SemanticConventionOptInKeyName] = "http", + ["OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, + }) + .Build(); + + using var tracerProviderBuilder = Sdk.CreateTracerProviderBuilder() + .ConfigureServices(services => services.AddSingleton(configuration)) + .AddAspNetCoreInstrumentation() .AddInMemoryExporter(exportedItems) .Build(); @@ -160,7 +161,7 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributesWhenItCreatesNewAc Assert.Equal(ActivityKind.Server, activity.Kind); - if (!enableGrpcAspNetCoreSupport.HasValue || enableGrpcAspNetCoreSupport.Value) + if (enableGrpcAspNetCoreSupport != null && enableGrpcAspNetCoreSupport.Equals("true", StringComparison.OrdinalIgnoreCase)) { Assert.Equal("grpc", activity.GetTagValue(SemanticConventions.AttributeRpcSystem)); Assert.Equal("greet.Greeter", activity.GetTagValue(SemanticConventions.AttributeRpcService)); @@ -197,7 +198,7 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributesWhenItCreatesNewAc })); } } - */ + public void Dispose() { this.server.Dispose(); From 82f089cfb0d8a306705c8786b09d10aca1e252de Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Tue, 5 Dec 2023 10:05:16 -0800 Subject: [PATCH 02/11] changelog --- .../CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index d6dc29b65c..71d9a7bd18 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -2,6 +2,18 @@ ## Unreleased +* Added support for gRPC instrumentation under an experimental feature flag +`OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION`. Note that the support + was removed in `1.6.0-rc.1` version of the package and versions released + before `1.6.0-rc.1` had gRPC instrumentation enabled by default. From now + onwards, gRPC instrumentation will only be enabled when + `OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION` is set to `True`. The + change is introduced in order to support stable release of `http` + instrumentation. Semantic conventions for RPC is still + [experimental](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/rpc) + and hence the package will only support it as an experimental feature. + ([#5130](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5130)) + ## 1.6.0-rc.1 Released 2023-Dec-01 From 69357e8df639b0e44a782616a39a709403f84926 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 10:31:16 -0800 Subject: [PATCH 03/11] address feedback (changelog + conditional comp) --- .../AspNetCoreTraceInstrumentationOptions.cs | 8 +------- .../CHANGELOG.md | 20 ++++++++++--------- .../TracerProviderBuilderExtensions.cs | 2 -- .../GrpcTests.server.cs | 4 ++-- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs index 6caadf3db9..f0fc396c96 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs @@ -17,9 +17,7 @@ using System.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; -#if NET6_0_OR_GREATER using OpenTelemetry.Internal; -#endif namespace OpenTelemetry.Instrumentation.AspNetCore; @@ -40,12 +38,10 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration) { Debug.Assert(configuration != null, "configuration was null"); -#if NET6_0_OR_GREATER - if (configuration.TryGetBoolValue("OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION", out var enableGrpcInstrumentation)) + if (configuration.TryGetBoolValue("OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION", out var enableGrpcInstrumentation)) { this.EnableGrpcAspNetCoreSupport = enableGrpcInstrumentation; } -#endif } /// @@ -101,7 +97,6 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration) /// public bool RecordException { get; set; } -#if NET6_0_OR_GREATER /// /// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. Default is true. /// @@ -109,5 +104,4 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration) /// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md. /// internal bool EnableGrpcAspNetCoreSupport { get; set; } -#endif } diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 71d9a7bd18..6e71b160e4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -2,16 +2,18 @@ ## Unreleased -* Added support for gRPC instrumentation under an experimental feature flag -`OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION`. Note that the support - was removed in `1.6.0-rc.1` version of the package and versions released - before `1.6.0-rc.1` had gRPC instrumentation enabled by default. From now - onwards, gRPC instrumentation will only be enabled when - `OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION` is set to `True`. The - change is introduced in order to support stable release of `http` - instrumentation. Semantic conventions for RPC is still +* Re-introduced support for gRPC instrumentation as an opt-in experimental + feature. From now onwards, gRPC can be enabled by setting + `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` to `True`. + `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` can be set + as an environment variable or via IConfiguration. The change is introduced in + order to support stable release of `http` instrumentation. Semantic + conventions for RPC is still [experimental](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/rpc) - and hence the package will only support it as an experimental feature. + and hence the package will only support it as an opt-in experimental feature. + Note that the support was removed in `1.6.0-rc.1` version of the package and + versions released before `1.6.0-rc.1` had gRPC instrumentation enabled by + default. ([#5130](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5130)) ## 1.6.0-rc.1 diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs index d61d5b6d4d..c0cca518f4 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/TracerProviderBuilderExtensions.cs @@ -76,9 +76,7 @@ public static TracerProviderBuilder AddAspNetCoreInstrumentation( services.Configure(name, configureAspNetCoreTraceInstrumentationOptions); } -#if !NETSTANDARD2_0 services.RegisterOptionsFactory(configuration => new AspNetCoreTraceInstrumentationOptions(configuration)); -#endif }); if (builder is IDeferredTracerProviderBuilder deferredTracerProviderBuilder) diff --git a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs index 172c5e7330..b94e66f315 100644 --- a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs +++ b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs @@ -58,7 +58,7 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributes(string enableGrpc .AddInMemoryCollection(new Dictionary { [SemanticConventionOptInKeyName] = "http", - ["OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, + ["OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, }) .Build(); @@ -131,7 +131,7 @@ public void GrpcAspNetCoreInstrumentationAddsCorrectAttributesWhenItCreatesNewAc .AddInMemoryCollection(new Dictionary { [SemanticConventionOptInKeyName] = "http", - ["OTEL_DOTNET_EXPERIMENTAL_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, + ["OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION"] = enableGrpcAspNetCoreSupport, }) .Build(); From ec7d655fabb4c22bfc22fd5d5c681289a25818d0 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 10:33:30 -0800 Subject: [PATCH 04/11] fix remark --- .../AspNetCoreTraceInstrumentationOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs index f0fc396c96..266aa5db62 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs @@ -98,7 +98,7 @@ internal AspNetCoreTraceInstrumentationOptions(IConfiguration configuration) public bool RecordException { get; set; } /// - /// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. Default is true. + /// Gets or sets a value indicating whether RPC attributes are added to an Activity when using Grpc.AspNetCore. /// /// /// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/rpc/rpc-spans.md. From db7323d4c2dacd36531c8012a85efec2e146538a Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 10:34:51 -0800 Subject: [PATCH 05/11] changelog fix --- .../CHANGELOG.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md index 6e71b160e4..5db9434309 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md @@ -4,11 +4,11 @@ * Re-introduced support for gRPC instrumentation as an opt-in experimental feature. From now onwards, gRPC can be enabled by setting - `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` to `True`. - `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` can be set - as an environment variable or via IConfiguration. The change is introduced in - order to support stable release of `http` instrumentation. Semantic - conventions for RPC is still + `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` flag to + `True`. `OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION` can + be set as an environment variable or via IConfiguration. The change is + introduced in order to support stable release of `http` instrumentation. + Semantic conventions for RPC is still [experimental](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/rpc) and hence the package will only support it as an opt-in experimental feature. Note that the support was removed in `1.6.0-rc.1` version of the package and From bf26c34f974809a411a169727670a4ca36c0532c Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 10:50:31 -0800 Subject: [PATCH 06/11] fix build --- .../OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs index b94e66f315..994763ac34 100644 --- a/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs +++ b/test/OpenTelemetry.Instrumentation.Grpc.Tests/GrpcTests.server.cs @@ -22,7 +22,6 @@ using Grpc.Net.Client; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Moq; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Instrumentation.Grpc.Services.Tests; using OpenTelemetry.Instrumentation.GrpcNetClient; From 02658792e1910e0aa9b54e2769ded9ca6326ed25 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 11:03:23 -0800 Subject: [PATCH 07/11] rmv netstandard check --- .../Implementation/HttpInListener.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index dc812440be..651dcea41c 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -19,17 +19,13 @@ using System.Diagnostics.CodeAnalysis; #endif using System.Reflection; -#if !NETSTANDARD2_0 using System.Runtime.CompilerServices; -#endif using Microsoft.AspNetCore.Http; #if !NETSTANDARD using Microsoft.AspNetCore.Routing; #endif using OpenTelemetry.Context.Propagation; -#if !NETSTANDARD2_0 using OpenTelemetry.Instrumentation.GrpcNetClient; -#endif using OpenTelemetry.Internal; using OpenTelemetry.Trace; @@ -253,12 +249,11 @@ public void OnStopActivity(Activity activity, object payload) activity.SetTag(SemanticConventions.AttributeHttpResponseStatusCode, TelemetryHelper.GetBoxedStatusCode(response.StatusCode)); -#if !NETSTANDARD2_0 if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod)) { this.AddGrpcAttributes(activity, grpcMethod, context); } -#endif + if (activity.Status == ActivityStatusCode.Unset) { activity.SetStatus(SpanHelper.ResolveSpanStatusForHttpStatusCode(activity.Kind, response.StatusCode)); @@ -339,14 +334,12 @@ static bool TryFetchException(object payload, out Exception exc) => ExceptionPropertyFetcher.TryFetch(payload, out exc) && exc != null; } -#if !NETSTANDARD2_0 [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool TryGetGrpcMethod(Activity activity, out string grpcMethod) { grpcMethod = GrpcTagHelper.GetGrpcMethodFromActivity(activity); return !string.IsNullOrEmpty(grpcMethod); } -#endif private string GetDisplayName(string httpMethod, string httpRoute = null) { @@ -357,7 +350,6 @@ private string GetDisplayName(string httpMethod, string httpRoute = null) : $"{normalizedMethod} {httpRoute}"; } -#if !NETSTANDARD2_0 [MethodImpl(MethodImplOptions.AggressiveInlining)] private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext context) { @@ -401,5 +393,4 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext } } } -#endif } From dc6a0a33bbb5f072e8132795e549097aaf21ea66 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 11:27:51 -0800 Subject: [PATCH 08/11] sort package names --- .../OpenTelemetry.Instrumentation.AspNetCore.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj index d183d1b76d..83b9e4e25b 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/OpenTelemetry.Instrumentation.AspNetCore.csproj @@ -30,11 +30,11 @@ - - + + From f7ba2f68c39443882bdadab2a0f99f310ebe6f81 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 14:59:15 -0800 Subject: [PATCH 09/11] mark static --- .../Implementation/HttpInListener.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 651dcea41c..9c00789851 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -251,7 +251,7 @@ public void OnStopActivity(Activity activity, object payload) if (this.options.EnableGrpcAspNetCoreSupport && TryGetGrpcMethod(activity, out var grpcMethod)) { - this.AddGrpcAttributes(activity, grpcMethod, context); + AddGrpcAttributes(activity, grpcMethod, context); } if (activity.Status == ActivityStatusCode.Unset) @@ -341,17 +341,8 @@ private static bool TryGetGrpcMethod(Activity activity, out string grpcMethod) return !string.IsNullOrEmpty(grpcMethod); } - private string GetDisplayName(string httpMethod, string httpRoute = null) - { - var normalizedMethod = RequestMethodHelper.GetNormalizedHttpMethod(httpMethod); - - return string.IsNullOrEmpty(httpRoute) - ? normalizedMethod - : $"{normalizedMethod} {httpRoute}"; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext context) + private static void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext context) { // The RPC semantic conventions indicate the span name // should not have a leading forward slash. @@ -393,4 +384,13 @@ private void AddGrpcAttributes(Activity activity, string grpcMethod, HttpContext } } } + + private string GetDisplayName(string httpMethod, string httpRoute = null) + { + var normalizedMethod = RequestMethodHelper.GetNormalizedHttpMethod(httpMethod); + + return string.IsNullOrEmpty(httpRoute) + ? normalizedMethod + : $"{normalizedMethod} {httpRoute}"; + } } From 214788fe4afbfe2c32c0a002c99e10f07cccd938 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 15:06:07 -0800 Subject: [PATCH 10/11] static --- .../Implementation/HttpInListener.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 9c00789851..8706743479 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -385,7 +385,7 @@ private static void AddGrpcAttributes(Activity activity, string grpcMethod, Http } } - private string GetDisplayName(string httpMethod, string httpRoute = null) + private static string GetDisplayName(string httpMethod, string httpRoute = null) { var normalizedMethod = RequestMethodHelper.GetNormalizedHttpMethod(httpMethod); From 8cff7820d3cb3cb07e3c7c806159f4a399128648 Mon Sep 17 00:00:00 2001 From: Vishwesh Bankwar Date: Wed, 6 Dec 2023 15:10:28 -0800 Subject: [PATCH 11/11] fix build --- .../Implementation/HttpInListener.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs index 8706743479..0bedc97fbd 100644 --- a/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs +++ b/src/OpenTelemetry.Instrumentation.AspNetCore/Implementation/HttpInListener.cs @@ -179,7 +179,7 @@ public void OnStartActivity(Activity activity, object payload) #endif var path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/"; - activity.DisplayName = this.GetDisplayName(request.Method); + activity.DisplayName = GetDisplayName(request.Method); // see the spec https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0/docs/http/http-spans.md @@ -242,7 +242,7 @@ public void OnStopActivity(Activity activity, object payload) var routePattern = (context.GetEndpoint() as RouteEndpoint)?.RoutePattern.RawText; if (!string.IsNullOrEmpty(routePattern)) { - activity.DisplayName = this.GetDisplayName(context.Request.Method, routePattern); + activity.DisplayName = GetDisplayName(context.Request.Method, routePattern); activity.SetTag(SemanticConventions.AttributeHttpRoute, routePattern); } #endif