Micrometer Tracing with Retrofit

Nathan Deamer
1 min read
· Oct 27, 2023

Press enter or click to view image in full size

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:

Add Retrofit sample by nathandeamer · Pull Request #46 · micrometer-metrics/micrometer-samples
Adding a retrofit sample which uses the OkHttpObservationInterceptor for metrics and traces

Observability
Retrofit
Spring Boot
Distributed Tracing

Published in Go City Engineering

Go City is the largest multi-attraction pass company in the world operating in 25+ cities across 14 countries with over 65% market share. Find out more at gocity.com.