Skip to content

Commit

Permalink
Added call options (#18)
Browse files Browse the repository at this point in the history
New options for ClientTracingConfiguration:
* WaitForReady
* FallbackCancellationToken
  • Loading branch information
Falco20019 authored Mar 17, 2020
1 parent 204ba6b commit 75604ab
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ A `ClientTracingInterceptor` also has default settings, which you can override b
- `WithStreaming()`: Logs to the client span whenever a message is sent or a response is received. *Note:* This package supports streaming but has not been rigorously tested. If you come across any issues, please let us know.
- `WithVerbosity()`: Logs to the client span additional events, such as call started, message sent, headers received, response received, and call complete. Default only logs if a call is cancelled.
- `WithTracedAttributes(params ClientRequestAttribute[] attrs)`: Sets tags on the client span in case you want to track information about the RPC call.
- `WithWaitForReady()`: Enables WaitForReady on all RPC calls.
- `WithFallbackCancellationToken(CancellationToken cancellationToken)`: Sets the cancellation token if the RPC call hasn't defined one.

### Example
```csharp
Expand All @@ -119,6 +121,8 @@ ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor
.WithOperationName(new CustomOperationNameConstructor())
.WithTracingAttributes(ClientTracingConfiguration.RequestAttribute.AllCallOptions,
ClientTracingConfiguration.ClientRequestAttribute.Headers)
.WithWaitForReady()
.WithFallbackCancellationToken(cancellationToken)
.Build();
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading;
using OpenTracing.Contrib.Grpc.OperationNameConstructor;

namespace OpenTracing.Contrib.Grpc.Configuration
Expand All @@ -17,16 +18,20 @@ public enum RequestAttribute
}

public ISet<RequestAttribute> TracedAttributes { get; }
public bool WaitForReady { get; }
public CancellationToken FallbackCancellationToken { get; }

internal ClientTracingConfiguration(ITracer tracer) : base(tracer)
{
TracedAttributes = new HashSet<RequestAttribute>();
}

internal ClientTracingConfiguration(ITracer tracer, IOperationNameConstructor operationNameConstructor, bool streaming, bool verbose, ISet<RequestAttribute> tracedAttributes)
internal ClientTracingConfiguration(ITracer tracer, IOperationNameConstructor operationNameConstructor, bool streaming, bool verbose, ISet<RequestAttribute> tracedAttributes, bool waitForReady, CancellationToken fallbackCancellationToken)
: base(tracer, operationNameConstructor, streaming, verbose)
{
TracedAttributes = tracedAttributes ?? new HashSet<RequestAttribute>();
WaitForReady = waitForReady;
FallbackCancellationToken = fallbackCancellationToken;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal ServerTracingConfiguration(ITracer tracer) : base(tracer)
TracedAttributes = new HashSet<RequestAttribute>();
}

internal ServerTracingConfiguration(ITracer tracer, IOperationNameConstructor operationNameConstructor, bool streaming, bool verbose, ISet<RequestAttribute> tracedAttributes)
internal ServerTracingConfiguration(ITracer tracer, IOperationNameConstructor operationNameConstructor, bool streaming, bool verbose, ISet<RequestAttribute> tracedAttributes)
: base(tracer, operationNameConstructor, streaming, verbose)
{
TracedAttributes = tracedAttributes ?? new HashSet<RequestAttribute>();
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTracing.Contrib.Grpc/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static ISpan SetException(this ISpan span, Exception ex)
{LogFields.ErrorObject, ex},

// Those fields will be removed once Configration.WithExpandExceptionLogs is implemented
{LogFields.ErrorKind, ex.GetType().Name},
{LogFields.ErrorKind, ex.GetType().Name},
{LogFields.Message, ex.Message},
{LogFields.Stack, ex.StackTrace}
});
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTracing.Contrib.Grpc/GrpcTraceLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace OpenTracing.Contrib.Grpc
{
internal class GrpcTraceLogger<TRequest, TResponse>
where TRequest : class
internal class GrpcTraceLogger<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
private readonly ISpan _span;
Expand Down
28 changes: 25 additions & 3 deletions src/OpenTracing.Contrib.Grpc/Handler/InterceptedClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,39 @@ public InterceptedClientHandler(ClientTracingConfiguration configuration, Client
{
_configuration = configuration;
_context = context;
if (context.Options.Headers == null)

var callOptions = ApplyConfigToCallOptions(_context.Options);
if (!Equals(callOptions, context.Options))
{
_context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host,
context.Options.WithHeaders(new Metadata())); // Add empty metadata to options
_context = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, callOptions);
}

var span = InitializeSpanWithHeaders();
_logger = new GrpcTraceLogger<TRequest, TResponse>(span, configuration);
_configuration.Tracer.Inject(span.Context, BuiltinFormats.HttpHeaders, new MetadataCarrier(_context.Options.Headers));
}

private CallOptions ApplyConfigToCallOptions(CallOptions callOptions)
{
if (callOptions.Headers == null)
{
// Add empty metadata to options:
callOptions = callOptions.WithHeaders(new Metadata());
}

if (_configuration.WaitForReady && callOptions.IsWaitForReady != _configuration.WaitForReady)
{
callOptions = callOptions.WithWaitForReady();
}

if (_configuration.FallbackCancellationToken != default && callOptions.CancellationToken != _configuration.FallbackCancellationToken)
{
callOptions = callOptions.WithCancellationToken(_configuration.FallbackCancellationToken);
}

return callOptions;
}

private ISpan InitializeSpanWithHeaders()
{
var operationName = _configuration.OperationNameConstructor.ConstructOperationName(_context.Method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenTracing.Contrib.Grpc.Configuration;
using OpenTracing.Contrib.Grpc.Handler;
using System.Collections.Generic;
using System.Threading;
using OpenTracing.Contrib.Grpc.OperationNameConstructor;

namespace OpenTracing.Contrib.Grpc.Interceptors
Expand Down Expand Up @@ -61,6 +62,8 @@ public class Builder
private bool _streaming;
private bool _verbose;
private ISet<ClientTracingConfiguration.RequestAttribute> _tracedAttributes;
private bool _waitForReady;
private CancellationToken _cancellationToken;

public Builder(ITracer tracer)
{
Expand Down Expand Up @@ -103,9 +106,27 @@ public Builder WithTracedAttributes(params ClientTracingConfiguration.RequestAtt
return this;
}

/// <summary>
/// Enables WaitForReady call option for all calls.
/// </summary>
/// <returns>this Builder configured to be verbose</returns>
public Builder WithWaitForReady()
{
_waitForReady = true;
return this;
}

/// <param name="cancellationToken">The cancellation token to set for all RPCs if none was set.</param>
/// <returns>this Builder configured to be verbose</returns>
public Builder WithFallbackCancellationToken(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
return this;
}

public ClientTracingInterceptor Build()
{
var configuration = new ClientTracingConfiguration(_tracer, _operationNameConstructor, _streaming, _verbose, _tracedAttributes);
var configuration = new ClientTracingConfiguration(_tracer, _operationNameConstructor, _streaming, _verbose, _tracedAttributes, _waitForReady, _cancellationToken);
return new ClientTracingInterceptor(configuration);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/OpenTracing.Contrib.Grpc.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private static async Task MainAsync()
.WithStreaming()
.WithVerbosity()
.WithTracedAttributes(ClientTracingConfiguration.RequestAttribute.AllCallOptions, ClientTracingConfiguration.RequestAttribute.Headers)
.WithWaitForReady()
.Build();

var client = new Phone.PhoneClient(new Channel("localhost:8011", ChannelCredentials.Insecure).Intercept(tracingInterceptor));
Expand Down

0 comments on commit 75604ab

Please sign in to comment.