Micrometer Tracing with Retrofit

Nathan Deamer
Go City Engineering
1 min readOct 27, 2023

Observability is one of our Engineering Principles at Go City and is built into our ways of working. Our goal is to know when there is a problem before our customers notice so that we act, debug and resolve quickly. We do this by following the ‘Three pillars of observability’ (Logs, Metrics and Traces).

We recently had a problem with distributed traces in a couple of our services which are using Retrofit for requests to other microservices, the tracing headers were not being propagated between services which gave us no end to end visibility of requests.

Fortunately, Retrofit uses OkHttp and the team over at micrometer-tracing have created an Observation Interceptor which will propagate the tracing headers and give you the usual Prometheus metrics we are used to seeing from micrometer (http_server_requests_seconds_count/sum)

GreetingClient greetingClient(ObservationRegistry observationRegistry,
@Value("${greeting.endpoint}") String greetingEndpoint) {
return new Retrofit.Builder().baseUrl(greetingEndpoint)
.client(new OkHttpClient.Builder()
.addInterceptor(
OkHttpObservationInterceptor.builder(observationRegistry, "http.client.requests").build())
.build())
.addConverterFactory(ScalarsConverterFactory.create())
.build()
.create(GreetingClient.class);
}

A full sample can be found here:

--

--