Hyper-0.11.0 (async) - Implementing middleware for latency measurement for requests


Basically as this version is based on async io, How can I implement a middleware to measure latencies for each request and publish it to new relic or prometheus.

Call function for http service based on hyper-tokio returns future so how get to know when the request is actually completed and response is written to the network.

Something like Beforemiddleware and Aftermiddleware in Iron framework so that I can start transaction time when request is received and complete transaction when response is written to network.


You implement the Service trait. See this middleware example https://github.com/tokio-rs/tokio-middleware/blob/master/src/log.rs


As per my understanding every service is returning future so even if I wrap request around middleware it won’t be the actual latency measurement (request is not completed yet and data is not written to the network yet). It will just measure the time diff between executing my business logic inside the handler.

Instead of that I want the end of request (data written to network) and measure latency from that timestamp.

How can I do that can someone shed some light and let me know if I am wrong in understanding this.