Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.