From d6aff08a465c99d0a49f1797b6f20c7c34e8e911 Mon Sep 17 00:00:00 2001 From: Ilia Brahinets Date: Fri, 5 Jul 2024 07:46:55 +0300 Subject: [PATCH] [Instrumentation.GrpcCore] Nullable support (#1936) --- .../.publicApi/PublicAPI.Shipped.txt | 1 + .../.publicApi/PublicAPI.Unshipped.txt | 28 ++++++------- .../AsyncStreamReaderProxy.cs | 8 ++-- .../CHANGELOG.md | 2 + .../ClientStreamWriterProxy.cs | 8 ++-- .../ClientTracingInterceptor.cs | 36 ++++++++++------ .../ClientTracingInterceptorOptions.cs | 2 +- .../GrpcCoreInstrumentation.cs | 2 +- ...nTelemetry.Instrumentation.GrpcCore.csproj | 3 +- .../RpcScope.cs | 41 +++++++++++-------- .../ServerStreamWriterProxy.cs | 4 +- .../ServerTracingInterceptorOptions.cs | 2 +- .../FoobarService.cs | 14 +++---- .../GrpcCoreClientInterceptorTests.cs | 37 +++++++++-------- .../GrpcCoreServerInterceptorTests.cs | 7 +++- .../InterceptorActivityListener.cs | 2 +- ...etry.Instrumentation.GrpcCore.Tests.csproj | 3 +- .../TestActivityTags.cs | 4 +- 18 files changed, 116 insertions(+), 88 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Shipped.txt b/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Shipped.txt index e69de29bb2..7dc5c58110 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Shipped.txt +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Shipped.txt @@ -0,0 +1 @@ +#nullable enable diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Unshipped.txt index 0fb3d72fd9..5415a53f00 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/.publicApi/PublicAPI.Unshipped.txt @@ -1,29 +1,29 @@ OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor -OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.ClientTracingInterceptor(OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions options) -> void +OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.ClientTracingInterceptor(OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions! options) -> void OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.ClientTracingInterceptorOptions() -> void -OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.Propagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator +OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.Propagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator! OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.RecordException.get -> bool OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.RecordException.set -> void OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.RecordMessageEvents.get -> bool OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptorOptions.RecordMessageEvents.set -> void OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor -OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ServerTracingInterceptor(OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions options) -> void +OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ServerTracingInterceptor(OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions! options) -> void OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions -OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.Propagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator +OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.Propagator.get -> OpenTelemetry.Context.Propagation.TextMapPropagator! OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.RecordException.get -> bool OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.RecordException.set -> void OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.RecordMessageEvents.get -> bool OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.RecordMessageEvents.set -> void OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptorOptions.ServerTracingInterceptorOptions() -> void OpenTelemetry.Trace.TracerProviderBuilderExtensions -override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncClientStreamingCall(Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncClientStreamingCallContinuation continuation) -> Grpc.Core.AsyncClientStreamingCall -override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncDuplexStreamingCall(Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncDuplexStreamingCallContinuation continuation) -> Grpc.Core.AsyncDuplexStreamingCall -override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncServerStreamingCall(TRequest request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncServerStreamingCallContinuation continuation) -> Grpc.Core.AsyncServerStreamingCall -override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncUnaryCall(TRequest request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncUnaryCallContinuation continuation) -> Grpc.Core.AsyncUnaryCall -override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.BlockingUnaryCall(TRequest request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.BlockingUnaryCallContinuation continuation) -> TResponse -override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ClientStreamingServerHandler(Grpc.Core.IAsyncStreamReader requestStream, Grpc.Core.ServerCallContext context, Grpc.Core.ClientStreamingServerMethod continuation) -> System.Threading.Tasks.Task -override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.DuplexStreamingServerHandler(Grpc.Core.IAsyncStreamReader requestStream, Grpc.Core.IServerStreamWriter responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.DuplexStreamingServerMethod continuation) -> System.Threading.Tasks.Task -override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ServerStreamingServerHandler(TRequest request, Grpc.Core.IServerStreamWriter responseStream, Grpc.Core.ServerCallContext context, Grpc.Core.ServerStreamingServerMethod continuation) -> System.Threading.Tasks.Task -override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.UnaryServerHandler(TRequest request, Grpc.Core.ServerCallContext context, Grpc.Core.UnaryServerMethod continuation) -> System.Threading.Tasks.Task -static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddGrpcCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder \ No newline at end of file +override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncClientStreamingCall(Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncClientStreamingCallContinuation! continuation) -> Grpc.Core.AsyncClientStreamingCall! +override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncDuplexStreamingCall(Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncDuplexStreamingCallContinuation! continuation) -> Grpc.Core.AsyncDuplexStreamingCall! +override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncServerStreamingCall(TRequest! request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncServerStreamingCallContinuation! continuation) -> Grpc.Core.AsyncServerStreamingCall! +override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.AsyncUnaryCall(TRequest! request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.AsyncUnaryCallContinuation! continuation) -> Grpc.Core.AsyncUnaryCall! +override OpenTelemetry.Instrumentation.GrpcCore.ClientTracingInterceptor.BlockingUnaryCall(TRequest! request, Grpc.Core.Interceptors.ClientInterceptorContext context, Grpc.Core.Interceptors.Interceptor.BlockingUnaryCallContinuation! continuation) -> TResponse! +override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ClientStreamingServerHandler(Grpc.Core.IAsyncStreamReader! requestStream, Grpc.Core.ServerCallContext! context, Grpc.Core.ClientStreamingServerMethod! continuation) -> System.Threading.Tasks.Task! +override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.DuplexStreamingServerHandler(Grpc.Core.IAsyncStreamReader! requestStream, Grpc.Core.IServerStreamWriter! responseStream, Grpc.Core.ServerCallContext! context, Grpc.Core.DuplexStreamingServerMethod! continuation) -> System.Threading.Tasks.Task! +override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.ServerStreamingServerHandler(TRequest! request, Grpc.Core.IServerStreamWriter! responseStream, Grpc.Core.ServerCallContext! context, Grpc.Core.ServerStreamingServerMethod! continuation) -> System.Threading.Tasks.Task! +override OpenTelemetry.Instrumentation.GrpcCore.ServerTracingInterceptor.UnaryServerHandler(TRequest! request, Grpc.Core.ServerCallContext! context, Grpc.Core.UnaryServerMethod! continuation) -> System.Threading.Tasks.Task! +static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddGrpcCoreInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/AsyncStreamReaderProxy.cs b/src/OpenTelemetry.Instrumentation.GrpcCore/AsyncStreamReaderProxy.cs index a0d1b03303..0885767c8a 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/AsyncStreamReaderProxy.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/AsyncStreamReaderProxy.cs @@ -24,17 +24,17 @@ internal class AsyncStreamReaderProxy : IAsyncStreamReader /// /// The on message action. /// - private readonly Action onMessage; + private readonly Action? onMessage; /// /// The on stream end action. /// - private readonly Action onStreamEnd; + private readonly Action? onStreamEnd; /// /// The on exception action. /// - private readonly Action onException; + private readonly Action? onException; /// /// Initializes a new instance of the class. @@ -43,7 +43,7 @@ internal class AsyncStreamReaderProxy : IAsyncStreamReader /// The on message action, if any. /// The on stream end action, if any. /// The on exception action, if any. - public AsyncStreamReaderProxy(IAsyncStreamReader reader, Action onMessage = null, Action onStreamEnd = null, Action onException = null) + public AsyncStreamReaderProxy(IAsyncStreamReader reader, Action? onMessage = null, Action? onStreamEnd = null, Action? onException = null) { this.reader = reader; this.onMessage = onMessage; diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/CHANGELOG.md b/src/OpenTelemetry.Instrumentation.GrpcCore/CHANGELOG.md index 2c50800621..276fedf468 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/CHANGELOG.md +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/CHANGELOG.md @@ -4,6 +4,8 @@ * Updated OpenTelemetry core component version(s) to `1.9.0`. ([#1888](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1888)) +* Updated minimal supported version of `Grpc.Core.Api` to `2.43.0`. + ([#1936](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1936)) ## 1.0.0-beta.6 diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientStreamWriterProxy.cs b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientStreamWriterProxy.cs index 254b4ba709..eafbd3bb32 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientStreamWriterProxy.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientStreamWriterProxy.cs @@ -24,17 +24,17 @@ internal class ClientStreamWriterProxy : IClientStreamWriter /// /// The on write action. /// - private readonly Action onWrite; + private readonly Action? onWrite; /// /// The on complete action. /// - private readonly Action onComplete; + private readonly Action? onComplete; /// /// The on exception action. /// - private readonly Action onException; + private readonly Action? onException; /// /// Initializes a new instance of the class. @@ -43,7 +43,7 @@ internal class ClientStreamWriterProxy : IClientStreamWriter /// The on write action if any. /// The on complete action, if any. /// The on exception action, if any. - public ClientStreamWriterProxy(IClientStreamWriter writer, Action onWrite = null, Action onComplete = null, Action onException = null) + public ClientStreamWriterProxy(IClientStreamWriter writer, Action? onWrite = null, Action? onComplete = null, Action? onException = null) { this.writer = writer; this.onWrite = onWrite; diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptor.cs b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptor.cs index 7f5d7d7da9..ce87f3a3d8 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptor.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptor.cs @@ -40,7 +40,7 @@ public override TResponse BlockingUnaryCall( Guard.ThrowIfNull(context); Guard.ThrowIfNull(continuation); - ClientRpcScope rpcScope = null; + ClientRpcScope? rpcScope = null; try { @@ -72,7 +72,7 @@ public override AsyncUnaryCall AsyncUnaryCall( Guard.ThrowIfNull(context); Guard.ThrowIfNull(continuation); - ClientRpcScope rpcScope = null; + ClientRpcScope? rpcScope = null; try { @@ -93,8 +93,14 @@ public override AsyncUnaryCall AsyncUnaryCall( } catch (AggregateException ex) { - rpcScope.CompleteWithException(ex.InnerException); - throw ex.InnerException; + if (ex.InnerException != null) + { + rpcScope.CompleteWithException(ex.InnerException); + throw ex.InnerException; + } + + rpcScope.CompleteWithException(ex); + throw; } }, TaskScheduler.Current); @@ -125,7 +131,7 @@ public override AsyncClientStreamingCall AsyncClientStreami Guard.ThrowIfNull(context); Guard.ThrowIfNull(continuation); - ClientRpcScope rpcScope = null; + ClientRpcScope? rpcScope = null; try { @@ -150,8 +156,14 @@ public override AsyncClientStreamingCall AsyncClientStreami } catch (AggregateException ex) { - rpcScope.CompleteWithException(ex.InnerException); - throw ex.InnerException; + if (ex.InnerException != null) + { + rpcScope.CompleteWithException(ex.InnerException); + throw ex.InnerException; + } + + rpcScope.CompleteWithException(ex); + throw; } }, TaskScheduler.Current); @@ -184,7 +196,7 @@ public override AsyncServerStreamingCall AsyncServerStreamingCall rpcScope = null; + ClientRpcScope? rpcScope = null; try { @@ -226,7 +238,7 @@ public override AsyncDuplexStreamingCall AsyncDuplexStreami Guard.ThrowIfNull(context); Guard.ThrowIfNull(continuation); - ClientRpcScope rpcScope = null; + ClientRpcScope? rpcScope = null; try { @@ -277,7 +289,7 @@ private sealed class ClientRpcScope : RpcScope /// The metadata setter action. /// - private static readonly Action MetadataSetter = (metadata, key, value) => { metadata.Add(new Metadata.Entry(key, value)); }; + private static readonly Action MetadataSetter = (metadata, key, value) => { metadata?.Add(new Metadata.Entry(key, value)); }; /// /// The context. @@ -287,7 +299,7 @@ private sealed class ClientRpcScope : RpcScope /// The parent activity. /// - private readonly Activity parentActivity; + private readonly Activity? parentActivity; /// /// Initializes a new instance of the class. @@ -343,7 +355,7 @@ public ClientRpcScope(ClientInterceptorContext context, Cli this.SetActivity(rpcActivity); options.Propagator.Inject(new PropagationContext(rpcActivity.Context, Baggage.Current), callOptions.Headers, MetadataSetter); - this.context = new ClientInterceptorContext(context.Method, context.Host, callOptions); + this.context = new ClientInterceptorContext(context.Method!, context.Host, callOptions); } /// diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptorOptions.cs b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptorOptions.cs index 02d214fea1..4e720ae237 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptorOptions.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/ClientTracingInterceptorOptions.cs @@ -28,5 +28,5 @@ public class ClientTracingInterceptorOptions /// /// Gets or sets additional activity tags used during unit testing. /// - internal IReadOnlyDictionary AdditionalTags { get; set; } + internal IReadOnlyDictionary? AdditionalTags { get; set; } } diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/GrpcCoreInstrumentation.cs b/src/OpenTelemetry.Instrumentation.GrpcCore/GrpcCoreInstrumentation.cs index a3a627133a..b7285ca7d3 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/GrpcCoreInstrumentation.cs +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/GrpcCoreInstrumentation.cs @@ -25,7 +25,7 @@ internal static class GrpcCoreInstrumentation /// /// The activity source name. /// - internal static readonly string ActivitySourceName = AssemblyName.Name; + internal static readonly string ActivitySourceName = AssemblyName.Name!; /// /// The activity source. diff --git a/src/OpenTelemetry.Instrumentation.GrpcCore/OpenTelemetry.Instrumentation.GrpcCore.csproj b/src/OpenTelemetry.Instrumentation.GrpcCore/OpenTelemetry.Instrumentation.GrpcCore.csproj index 099152bc14..c0bd481471 100644 --- a/src/OpenTelemetry.Instrumentation.GrpcCore/OpenTelemetry.Instrumentation.GrpcCore.csproj +++ b/src/OpenTelemetry.Instrumentation.GrpcCore/OpenTelemetry.Instrumentation.GrpcCore.csproj @@ -4,7 +4,6 @@ .NET gRPC Core based client and server interceptors for OpenTelemetry. $(PackageTags);gRPC Core;interceptors Instrumentation.GrpcCore- - disable