gRPC Instrumentation

gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment.

Below you can find an example of how to instrument gRPC with Micrometer Observation. That means that depending on your Observation Handler configuration you instrument once, and can have multiple benefits out of it (e.g. metrics, distributed tracing).

First, client and server side interceptors need to be setup.

// Setting up interceptors
ObservationGrpcServerInterceptor serverInterceptor;

ObservationGrpcClientInterceptor clientInterceptor;


this.serverInterceptor = new ObservationGrpcServerInterceptor(observationRegistry);
this.clientInterceptor = new ObservationGrpcClientInterceptor(observationRegistry);

Next, server and channels need to have the interceptors added.

// Adding them to the server and client side
EchoService echoService = new EchoService();
server = InProcessServerBuilder.forName("sample")
    .addService(echoService)
    .intercept(serverInterceptor)
    .build();
server.start();

channel = InProcessChannelBuilder.forName("sample").intercept(clientInterceptor).build();

Below you have an example of usage with the result assertions.

// 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
TestObservationRegistryAssert.assertThat(observationRegistry)
    .hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.client"))
    .hasAnObservation(observationContextAssert -> observationContextAssert.hasNameEqualTo("grpc.server"));