tower_http_metrics/server/
service.rs

1use std::task::Context;
2use std::task::Poll;
3
4use http::Request as HttpRequest;
5use http::Response as HttpResponse;
6use tower_service::Service;
7
8use crate::server::future::InstrumentedFuture;
9
10use super::body::InstrumentedBody;
11use super::label_from_method;
12
13#[derive(Clone)]
14pub struct HttpServerMetricsService<S> {
15    inner: S,
16}
17
18impl<S> HttpServerMetricsService<S> {
19    pub(crate) fn new(inner: S) -> Self {
20        Self { inner }
21    }
22}
23
24impl<S, ReqBody, ResBody> Service<HttpRequest<ReqBody>> for HttpServerMetricsService<S>
25where
26    S: Service<HttpRequest<ReqBody>, Response = HttpResponse<ResBody>>,
27{
28    type Response = HttpResponse<InstrumentedBody<ResBody>>;
29
30    type Error = S::Error;
31
32    type Future = InstrumentedFuture<S::Future>;
33
34    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
35        self.inner.poll_ready(cx)
36    }
37
38    fn call(&mut self, req: HttpRequest<ReqBody>) -> Self::Future {
39        let method = label_from_method(req.method());
40        InstrumentedFuture::new(self.inner.call(req), method)
41    }
42}