gRPC 仪表化
gRPC 是一个现代的开源高性能远程过程调用(RPC)框架,可以在任何环境中运行。
下面你可以找到一个如何使用 Micrometer Observation 来对 gRPC 进行监控的示例。这意味着根据你的 Observation Handler 配置,你只需进行一次监控,就可以从中获得多种好处(例如,指标、分布式追踪)。
首先,需要设置客户端和服务端的拦截器。
// Setting up interceptors
ObservationGrpcServerInterceptor serverInterceptor;
ObservationGrpcClientInterceptor clientInterceptor;
this.serverInterceptor = new ObservationGrpcServerInterceptor(observationRegistry);
this.clientInterceptor = new ObservationGrpcClientInterceptor(observationRegistry);
接下来,服务器和通道需要添加拦截器。
// Adding them to the server and client side
EchoService echoService = new EchoService();
server = InProcessServerBuilder.forName("sample")
.addService(echoService)
.intercept(new ServerHeaderInterceptor())
.intercept(serverInterceptor)
.build();
server.start();
channel = InProcessChannelBuilder.forName("sample")
.intercept(new ClientHeaderInterceptor(), clientInterceptor)
.build();
以下是一个带有结果断言的用法示例。
// Usage example
SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
SimpleRequest request = SimpleRequest.newBuilder().setRequestMessage("Hello").build();
SimpleResponse response = stub.unaryRpc(request);
assertThat(response.getResponseMessage()).isEqualTo("Hello");
// Observation outcome
assertThat(observationRegistry).hasAnObservation(observationContextAssert -> {
observationContextAssert.hasNameEqualTo("grpc.client");
assertCommonKeyValueNames(observationContextAssert);
}).hasAnObservation(observationContextAssert -> {
observationContextAssert.hasNameEqualTo("grpc.server");
assertCommonKeyValueNames(observationContextAssert);
});