Define a Reporter and Tracer

To use a tracer, first define a reporter so that trace data can be forwarded to a Zipkin endpoint. We’ll use the URLConnectionSender, and post it to a local Zipkin instance.

You’ll need to add this to every serverice and client:

  • AuthServer.main(…)
  • ChatServer.main(…)
  • ChatClient
AsyncReporter<Span> reporter = AsyncReporter.create(
    URLConnectionSender.create("http://localhost:9411/api/v1/spans"));

GrpcTracing tracing = GrpcTracing.create(Tracing.newBuilder()
    .localServiceName("my-service") // MAKE SURE YOU CHANGE THE NAME
    .reporter(reporter)
    .build());

Add Server Interceptors

We looked at server interceptors in the previous section. Add the tracing.newServerInterceptor() to every service:

  • AuthServer.main(…)
  • ChatServer.main(…)
final Server server = ServerBuilder.forPort(9091)
        .addService(ServerInterceptors.intercept(
            someServiceImpl, tracing.newServerInterceptor()))
        .build();