|
| 1 | +use crate::AccessLog; |
| 2 | +use futures::{ready, TryFuture}; |
| 3 | +use pin_project::pin_project; |
| 4 | +use std::future::Future; |
| 5 | +use std::pin::Pin; |
| 6 | +use std::task::{Context, Poll}; |
| 7 | +use std::time::SystemTime; |
| 8 | +use tokio::sync::mpsc; |
| 9 | +use tracing::warn; |
| 10 | + |
| 11 | +/// A layer that adds access logging |
| 12 | +#[derive(Clone)] |
| 13 | +pub struct AccessLogLayer { |
| 14 | + sink: Option<mpsc::Sender<AccessLog>>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Clone)] |
| 18 | +pub struct AccessLogContext<Svc> { |
| 19 | + inner: Svc, |
| 20 | + sink: Option<mpsc::Sender<AccessLog>>, |
| 21 | +} |
| 22 | + |
| 23 | +#[pin_project] |
| 24 | +pub struct ResponseFuture<F> { |
| 25 | + state: Option<(AccessLog, mpsc::Sender<AccessLog>)>, |
| 26 | + |
| 27 | + #[pin] |
| 28 | + inner: F, |
| 29 | +} |
| 30 | + |
| 31 | +impl<Svc> tower::layer::Layer<Svc> for AccessLogLayer { |
| 32 | + type Service = AccessLogContext<Svc>; |
| 33 | + |
| 34 | + fn layer(&self, inner: Svc) -> Self::Service { |
| 35 | + Self::Service { |
| 36 | + inner, |
| 37 | + sink: self.sink.clone(), |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<Svc, B1, B2> tower::Service<http::Request<B1>> for AccessLogContext<Svc> |
| 43 | +where |
| 44 | + Svc: tower::Service<http::Request<B1>, Response = http::Response<B2>>, |
| 45 | +{ |
| 46 | + type Response = Svc::Response; |
| 47 | + type Error = Svc::Error; |
| 48 | + type Future = ResponseFuture<Svc::Future>; |
| 49 | + |
| 50 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Svc::Error>> { |
| 51 | + self.inner.poll_ready(cx) |
| 52 | + } |
| 53 | + |
| 54 | + fn call(&mut self, request: http::Request<B1>) -> Self::Future { |
| 55 | + let sink = match &self.sink { |
| 56 | + Some(sink) => sink, |
| 57 | + None => { |
| 58 | + return ResponseFuture { |
| 59 | + state: None, |
| 60 | + inner: self.inner.call(request), |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + let t0 = SystemTime::now(); |
| 66 | + |
| 67 | + let host = request.headers().get("Host").map(|x| x.clone()); |
| 68 | + |
| 69 | + let trace_id = request |
| 70 | + .headers() |
| 71 | + .get("X-Amzn-Trace-Id") |
| 72 | + .or_else(|| request.headers().get("X-Request-ID")) |
| 73 | + .map(|x| x.clone()); |
| 74 | + |
| 75 | + let user_agent = request.headers().get("User-Agent").map(|x| x.clone()); |
| 76 | + |
| 77 | + let log = AccessLog { |
| 78 | + uri: request.uri().clone(), |
| 79 | + method: request.method().clone(), |
| 80 | + status: Default::default(), |
| 81 | + host, |
| 82 | + user_agent, |
| 83 | + trace_id, |
| 84 | + start_time: t0, |
| 85 | + end_time: t0, |
| 86 | + }; |
| 87 | + |
| 88 | + ResponseFuture { |
| 89 | + state: Some((log, sink.clone())), |
| 90 | + inner: self.inner.call(request), |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl AccessLogLayer { |
| 96 | + pub fn new(sink: Option<mpsc::Sender<AccessLog>>) -> Self { |
| 97 | + Self { sink } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<F, B2> Future for ResponseFuture<F> |
| 102 | +where |
| 103 | + F: TryFuture<Ok = http::Response<B2>>, |
| 104 | +{ |
| 105 | + type Output = Result<F::Ok, F::Error>; |
| 106 | + |
| 107 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 108 | + let this = self.project(); |
| 109 | + let response: http::Response<B2> = ready!(this.inner.try_poll(cx))?; |
| 110 | + |
| 111 | + if let Some((mut log, mut sink)) = this.state.take() { |
| 112 | + log.end_time = SystemTime::now(); |
| 113 | + log.status = response.status().clone(); |
| 114 | + |
| 115 | + if let Err(error) = sink.try_send(log) { |
| 116 | + warn!(message = "access log dropped", %error); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Poll::Ready(Ok(response)) |
| 121 | + } |
| 122 | +} |
0 commit comments