One of the components of my is a Rust application built with the Axum web framework. In its description, mentions: OpenTelemetry demo axum doesn't have its own middleware system but instead uses . This means gets timeouts, tracing, compression, authorization, and more, for free. It also enables you to share middleware with applications written using or . axum tower::Service axum hyper tonic -- axum README So far, I was happy to let this cryptic explanation lurk in the corner of my mind, but today is the day I want to understand what it means. Like many others, this post aims to explain to me and others how to do this. The crate offers the following information: tower Tower is a library of modular and reusable components for building robust networking clients and servers. Tower provides a simple core abstraction, the trait, which represents an asynchronous function taking a request and returning either a response or an error. This abstraction can be used to model both clients and servers. Service Generic components, like timeouts, rate limiting, and load balancing, can be modeled as s that wrap some inner service and apply additional behavior before or after the inner service is called. This allows implementing these components in a protocol-agnostic, composable way. Typically, such services are referred to as . Service middleware -- tower crate Tower is designed around Functional Programming and two main abstractions, and . Service Layer In its simplest expression, a is a function that reads an input and produces an output. It consists of two methods: Service One should call to ensure that the service can process requests poll_ready() processes the request and returns the response asynchronously call() Because calls can fail, the return value is wrapped in a . Moreover, since Tower deals with asynchronous calls, the is wrapped in a . Hence, a transforms a into a , with and needing to be defined by the developer. Result Result Future Service Self::Request Future<Result<Self::Response, Error>> Request Response The trait allows composing s together. Layer Service Here's a slightly more detailed diagram: A typical implementation will wrap an underlying component; the component may be a service itself. Hence, you can chain multiple features by composing various functions. Service The function implementation usually executes these steps in order, all of them being optional: call() Pre-call Call the wrapped component Post-call For example, a logging service could log the parameters before the call, call the logged component, and log the return value after the call. Another example would be a throttling service, which limits the rate of calls of the wrapped service: it would read the current status before the call and, if above a configured limit, would return immediately without calling the wrapped component. It will call the component and increment the status if the status is valid. The role of a layer would be to take one service and wrap it into the other. With this in mind, it's relatively easy to check the and understand what it does. It offers two services with their respective layers: one is to extract the trace and span IDs from an HTTP request, and another is to send the data to the OTEL collector. Axum-tracing-open telemetry crate Note that Tower comes with several out-of-the-box services, each available via a : feature crate : load-balance requests balance : <abbr title="Multi Producer Single Consumer">MPSC</abbr> buffer buffer : service discovery discover : conditional dispatch filter : retry slow requests hedge : limit requests limit : load measurement load : retry failed requests retry : timeout requests timeout Finally, note that Tower comes in three crates: is the public crate, while and are considered less stable. tower tower-service tower-layer In this post, we have explained what the Tower library is: it's a Functional Programming library that provides function composition. If you come from the Object-Oriented Programming paradigm, it's similar to the Decorator pattern. It builds upon two abstractions, is the function and composes functions. Service Layer It's widespread in the Rust ecosystem, and learning it is a good investment. To go further: Axum tower documentation tower crate axum_tracing_opentelemetry documentation Originally published at on August 20th, 2023 A Java Geek