Skip to content

Instantly share code, notes, and snippets.

@markrendle
Last active June 10, 2020 10:32
Show Gist options
  • Save markrendle/6a7f7cb86058f6ac8bfcf13d8424ec6b to your computer and use it in GitHub Desktop.
Save markrendle/6a7f7cb86058f6ac8bfcf13d8424ec6b to your computer and use it in GitHub Desktop.
gRPC Interceptor to check for an X-Obsolete header
internal class ObsoleteLoggingInterceptor : Interceptor
{
private readonly ILogger<ObsoleteLoggingInterceptor> _logger;
public ObsoleteLoggingInterceptor(ILogger<ObsoleteLoggingInterceptor> logger)
{
_logger = logger;
}
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request,
ClientInterceptorContext<TRequest, TResponse> context,
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
var call = continuation(request, context);
return new AsyncUnaryCall<TResponse>(call.ResponseAsync,
CheckObsoleteHeaderAsync(context.Method.Name, call.ResponseHeadersAsync),
call.GetStatus, call.GetTrailers, call.Dispose);
}
// Override other methods as necessary
private async Task<Metadata> CheckObsoleteHeaderAsync(string methodName, Task<Metadata> responseHeadersAsync)
{
var metadata = await responseHeadersAsync;
var obsolete = metadata.FirstOrDefault(e => string.Equals(e.Key, "x-obsolete", StringComparison.OrdinalIgnoreCase));
if (!(obsolete is null))
{
_logger.LogWarning("{MethodName} is obsolete: {Message}", methodName, obsolete.Value);
}
return metadata;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment