# Micrometer Tracing with Retrofit

[Nathan Deamer](https://medium.com/@nathandeamer?source=post_page---byline--e931b8a83121)  
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](https://gocity.com/) 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](https://www.oreilly.com/library/view/distributed-systems-observability/9781492033431/ch04.html)’ (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](https://square.github.io/retrofit/) uses OkHttp and the team over at [micrometer-tracing](https://github.com/micrometer-metrics/tracing) have created an [Observation Interceptor](https://github.com/micrometer-metrics/micrometer/blob/main/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptor.java) 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`)

```java
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**](https://github.com/micrometer-metrics/micrometer-samples/pull/46?source=post_page-----e931b8a83121)  
**Adding a retrofit sample which uses the OkHttpObservationInterceptor for metrics and traces**

[Observability](https://medium.com/tag/observability?source=post_page-----e931b8a83121)  
[Retrofit](https://medium.com/tag/retrofit?source=post_page-----e931b8a83121)  
[Spring Boot](https://medium.com/tag/spring-boot?source=post_page-----e931b8a83121)  
[Distributed Tracing](https://medium.com/tag/distributed-tracing?source=post_page-----e931b8a83121)

[**Published in Go City Engineering**](https://medium.com/go-city?source=post_page---post_publication_info--e931b8a83121)

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](http://gocity.com/).
