Skip to content

Commit

Permalink
[Instrumentation.GrpcCore] Fix analysis warnings (#963)
Browse files Browse the repository at this point in the history
Fix/suppress code analysis warnings in OpenTelemetry.Instrumentation.GrpcCore.
Contributes to #950.
  • Loading branch information
martincostello authored Feb 22, 2023
1 parent 2402943 commit 82b072f
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/grpc.core/Examples.GrpcCore.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
tcs.SetResult(true);
});

return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose());
return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose(), stoppingToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -52,6 +53,9 @@ public override TResponse BlockingUnaryCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
Expand Down Expand Up @@ -81,11 +85,16 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
rpcScope.RecordRequest(request);
var responseContinuation = continuation(request, rpcScope.Context);
var responseAsync = responseContinuation.ResponseAsync.ContinueWith(
Expand All @@ -103,7 +112,8 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
rpcScope.CompleteWithException(ex.InnerException);
throw ex.InnerException;
}
});
},
TaskScheduler.Current);

return new AsyncUnaryCall<TResponse>(
responseAsync,
Expand All @@ -128,11 +138,16 @@ public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreami
ClientInterceptorContext<TRequest, TResponse> context,
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
var responseContinuation = continuation(rpcScope.Context);
var clientRequestStreamProxy = new ClientStreamWriterProxy<TRequest>(
responseContinuation.RequestStream,
Expand All @@ -154,7 +169,8 @@ public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreami
rpcScope.CompleteWithException(ex.InnerException);
throw ex.InnerException;
}
});
},
TaskScheduler.Current);

return new AsyncClientStreamingCall<TRequest, TResponse>(
clientRequestStreamProxy,
Expand All @@ -181,11 +197,16 @@ public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRe
ClientInterceptorContext<TRequest, TResponse> context,
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
rpcScope.RecordRequest(request);
var responseContinuation = continuation(request, rpcScope.Context);

Expand Down Expand Up @@ -218,11 +239,16 @@ public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreami
ClientInterceptorContext<TRequest, TResponse> context,
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
var responseContinuation = continuation(rpcScope.Context);

var requestStreamProxy = new ClientStreamWriterProxy<TRequest>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ClientTracingInterceptorOptions
/// <summary>
/// Gets or sets a value indicating whether or not to record individual message events.
/// </summary>
public bool RecordMessageEvents { get; set; } = false;
public bool RecordMessageEvents { get; set; }

/// <summary>
/// Gets the propagator.
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Instrumentation.GrpcCore/RpcScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal abstract class RpcScope<TRequest, TResponse> : IDisposable
/// <summary>
/// The complete flag.
/// </summary>
private long complete = 0;
private long complete;

/// <summary>
/// The request message counter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand All @@ -77,6 +80,9 @@ public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TRe
ServerCallContext context,
ClientStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand Down Expand Up @@ -104,6 +110,9 @@ public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
ServerCallContext context,
ServerStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand All @@ -127,6 +136,9 @@ public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
/// <inheritdoc/>
public override async Task DuplexStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ServerTracingInterceptorOptions
/// <summary>
/// Gets or sets a value indicating whether or not to record individual message events.
/// </summary>
public bool RecordMessageEvents { get; set; } = false;
public bool RecordMessageEvents { get; set; }

/// <summary>
/// Gets the propagator.
Expand Down
22 changes: 11 additions & 11 deletions test/OpenTelemetry.Instrumentation.GrpcCore.Tests/FoobarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ internal class FoobarService : Foobar.FoobarBase
/// <summary>
/// Default traceparent header value with the sampling bit on.
/// </summary>
internal static readonly string DefaultTraceparentWithSampling = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
internal const string DefaultTraceparentWithSampling = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";

/// <summary>
/// The request header fail with status code.
/// </summary>
internal const string RequestHeaderFailWithStatusCode = "failurestatuscode";

/// <summary>
/// The request header error description.
/// </summary>
internal const string RequestHeaderErrorDescription = "failuredescription";

/// <summary>
/// The default parent from a traceparent header.
Expand All @@ -60,16 +70,6 @@ internal class FoobarService : Foobar.FoobarBase
/// </summary>
internal static readonly int DefaultResponseMessageSize = ((IMessage)DefaultResponseMessage).CalculateSize();

/// <summary>
/// The request header fail with status code.
/// </summary>
internal static readonly string RequestHeaderFailWithStatusCode = "failurestatuscode";

/// <summary>
/// The request header error description.
/// </summary>
internal static readonly string RequestHeaderErrorDescription = "failuredescription";

/// <summary>
/// Starts the specified service.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GrpcCoreClientInterceptorTests
/// <summary>
/// A bogus server uri.
/// </summary>
private static readonly string BogusServerUri = "dns:i.dont.exist:77923";
private const string BogusServerUri = "dns:i.dont.exist:77923";

/// <summary>
/// The default metadata func.
Expand All @@ -49,7 +49,7 @@ public class GrpcCoreClientInterceptorTests
[Fact]
public async Task AsyncUnarySuccess()
{
await this.TestHandlerSuccess(FoobarService.MakeUnaryAsyncRequest, DefaultMetadataFunc()).ConfigureAwait(false);
await TestHandlerSuccess(FoobarService.MakeUnaryAsyncRequest, DefaultMetadataFunc()).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -59,7 +59,7 @@ public async Task AsyncUnarySuccess()
[Fact]
public async Task AsyncUnaryUnavailable()
{
await this.TestHandlerFailure(
await TestHandlerFailure(
FoobarService.MakeUnaryAsyncRequest,
StatusCode.Unavailable,
validateErrorDescription: false,
Expand All @@ -73,7 +73,7 @@ await this.TestHandlerFailure(
[Fact]
public async Task AsyncUnaryFail()
{
await this.TestHandlerFailure(FoobarService.MakeUnaryAsyncRequest).ConfigureAwait(false);
await TestHandlerFailure(FoobarService.MakeUnaryAsyncRequest).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -97,7 +97,7 @@ static void MakeRequest(Foobar.FoobarClient client)
[Fact]
public async Task ClientStreamingSuccess()
{
await this.TestHandlerSuccess(FoobarService.MakeClientStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
await TestHandlerSuccess(FoobarService.MakeClientStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -107,7 +107,7 @@ public async Task ClientStreamingSuccess()
[Fact]
public async Task ClientStreamingUnavailable()
{
await this.TestHandlerFailure(
await TestHandlerFailure(
FoobarService.MakeClientStreamingRequest,
StatusCode.Unavailable,
validateErrorDescription: false,
Expand All @@ -121,7 +121,7 @@ await this.TestHandlerFailure(
[Fact]
public async Task ClientStreamingFail()
{
await this.TestHandlerFailure(FoobarService.MakeClientStreamingRequest).ConfigureAwait(false);
await TestHandlerFailure(FoobarService.MakeClientStreamingRequest).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -145,7 +145,7 @@ static void MakeRequest(Foobar.FoobarClient client)
[Fact]
public async Task ServerStreamingSuccess()
{
await this.TestHandlerSuccess(FoobarService.MakeServerStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
await TestHandlerSuccess(FoobarService.MakeServerStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -155,7 +155,7 @@ public async Task ServerStreamingSuccess()
[Fact]
public async Task ServerStreamingFail()
{
await this.TestHandlerFailure(FoobarService.MakeServerStreamingRequest).ConfigureAwait(false);
await TestHandlerFailure(FoobarService.MakeServerStreamingRequest).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -179,7 +179,7 @@ static void MakeRequest(Foobar.FoobarClient client)
[Fact]
public async Task DuplexStreamingSuccess()
{
await this.TestHandlerSuccess(FoobarService.MakeDuplexStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
await TestHandlerSuccess(FoobarService.MakeDuplexStreamingRequest, DefaultMetadataFunc()).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -189,7 +189,7 @@ public async Task DuplexStreamingSuccess()
[Fact]
public async Task DuplexStreamingUnavailable()
{
await this.TestHandlerFailure(
await TestHandlerFailure(
FoobarService.MakeDuplexStreamingRequest,
StatusCode.Unavailable,
validateErrorDescription: false,
Expand All @@ -203,7 +203,7 @@ await this.TestHandlerFailure(
[Fact]
public async Task DuplexStreamingFail()
{
await this.TestHandlerFailure(FoobarService.MakeDuplexStreamingRequest).ConfigureAwait(false);
await TestHandlerFailure(FoobarService.MakeDuplexStreamingRequest).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -376,7 +376,7 @@ static void ValidateCommonEventAttributes(ActivityEvent activityEvent)
/// <param name="clientRequestFunc">The client request function.</param>
/// <param name="additionalMetadata">The additional metadata, if any.</param>
/// <returns>A Task.</returns>
private async Task TestHandlerSuccess(Func<Foobar.FoobarClient, Metadata, Task> clientRequestFunc, Metadata additionalMetadata)
private static async Task TestHandlerSuccess(Func<Foobar.FoobarClient, Metadata, Task> clientRequestFunc, Metadata additionalMetadata)
{
var mockPropagator = new Mock<TextMapPropagator>();
PropagationContext capturedPropagationContext = default;
Expand Down Expand Up @@ -482,7 +482,7 @@ private async Task TestHandlerSuccess(Func<Foobar.FoobarClient, Metadata, Task>
/// <returns>
/// A Task.
/// </returns>
private async Task TestHandlerFailure(
private static async Task TestHandlerFailure(
Func<Foobar.FoobarClient, Metadata, Task> clientRequestFunc,
StatusCode statusCode = StatusCode.ResourceExhausted,
bool validateErrorDescription = true,
Expand Down
Loading

0 comments on commit 82b072f

Please sign in to comment.