From 5e73c69f2bb7d654be6e5c8e426255f84fca2cc4 Mon Sep 17 00:00:00 2001 From: Alex Jackson Date: Wed, 24 Apr 2024 10:24:54 -0500 Subject: [PATCH] feat: finish copying excellent code --- src/main.rs | 1 + src/trace_layer.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/trace_layer.rs diff --git a/src/main.rs b/src/main.rs index 482056e..bf10838 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod audio; mod cli; mod error; mod feed; +mod trace_layer; use crate::cli::Cli; use crate::error::Result; diff --git a/src/trace_layer.rs b/src/trace_layer.rs new file mode 100644 index 0000000..acad12e --- /dev/null +++ b/src/trace_layer.rs @@ -0,0 +1,30 @@ +use axum::{body::BoxBody, extract::ConnectInfo, response::Response}; +use hyper::{Body, Request}; +use std::{net::SocketAddr, time::Duration}; +use tracing::Span; + +pub(crate) fn trace_layer_make_span_with(req: &Request) -> Span { + tracing::error_span!("request", + uri = %req.uri(), + method = %req.method(), + source = request.extensions() + .get::>() + .map(|conn_info| tracing::field::display(conn_info)) + .unwrap_or_else(|| tracing::field::display("".to_string())), + status = tracing::field::Empty, + latency = tracing::field::Empty, + ) +} + +pub(crate) fn trace_layer_on_request(_req: &Request, _span: &Span) { + tracing::trace!("Got request") +} + +pub(crate) fn trace_layer_on_response(resp: &Response, latency: Duration, span: &Span) { + span.record( + "latency", + tracing::field::display(format!("{}us", latency.as_micros())), + ); + span.record("status", tracing::field::display(resp.status())); + tracing::trace!("Responded"); +}