Last active
June 10, 2020 10:32
-
-
Save markrendle/6a7f7cb86058f6ac8bfcf13d8424ec6b to your computer and use it in GitHub Desktop.
gRPC Interceptor to check for an X-Obsolete header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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