gRPC - Logging with interceptor in gRPC service

要紀錄每次 gRPC 的呼叫,可以使用 gRPC interceptor 來做。


建立一個 Interceptor 類別繼承自 gRPC 的 Interceptor,建立個建構子允許注入 Logger,接著覆寫掉會用到的方法,像是 UnaryServerHandler,然後在內部將要記錄的資訊透過 Logger 寫入 Log 即可。


像是下面這邊筆者實作了一個 Interceptor,希望能紀錄每次 gRPC 的調用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;


namespace GrpcServiceInterceptor
{
public class LoggerInterceptor : Interceptor
{
private readonly ILogger<LoggerInterceptor> _logger;


public LoggerInterceptor(ILogger<LoggerInterceptor> logger)
{
_logger = logger;
}


public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
var response = await base.UnaryServerHandler(request, context, continuation);
Log(MethodType.Unary, request, response, context);


return response;
}


public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TResponse>(
IAsyncStreamReader<TRequest> requestStream,
ServerCallContext context,
ClientStreamingServerMethod<TRequest, TResponse> continuation)
{
var response = await base.ClientStreamingServerHandler(requestStream, context, continuation);
Log(MethodType.ClientStreaming, requestStream, response, context);


return response;
}


public override Task ServerStreamingServerHandler<TRequest, TResponse>(
TRequest request,
IServerStreamWriter<TResponse> responseStream,
ServerCallContext context,
ServerStreamingServerMethod<TRequest, TResponse> continuation)
{
Log(MethodType.ServerStreaming, request, responseStream, context);


return base.ServerStreamingServerHandler(request, responseStream, context, continuation);
}


public override Task DuplexStreamingServerHandler<TRequest, TResponse>(
IAsyncStreamReader<TRequest> requestStream,
IServerStreamWriter<TResponse> responseStream,
ServerCallContext context,
DuplexStreamingServerMethod<TRequest, TResponse> continuation)
{
Log(MethodType.DuplexStreaming, requestStream, responseStream, context);


return base.DuplexStreamingServerHandler(requestStream, responseStream, context, continuation);
}


private void Log<TRequest, TResponse>(MethodType methodType, TRequest request, TResponse response,
ServerCallContext context)
{
_logger.LogInformation(
$"gRPC call. Type: {methodType}. Request: {request.ToString()}. Response: {response.ToString()}");
}
}
}


Interceptor 寫好後掛載進去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc(options =>
{
var interceptors = options.Interceptors;
interceptors.Add<LoggerInterceptor>();
...
});
}
...
}
...


準備用來測試的程式,這邊筆者只是很單純的將請求加工後回傳。


運行起來實際去調用 gRPC。


gRPC 的調用就會確實的被記錄下來。