Skip to content

Commit

Permalink
feat: finish copying excellent code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaxbits committed Apr 24, 2024
1 parent 6b61dbd commit 5e73c69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod audio;
mod cli;
mod error;
mod feed;
mod trace_layer;

use crate::cli::Cli;
use crate::error::Result;
Expand Down
30 changes: 30 additions & 0 deletions src/trace_layer.rs
Original file line number Diff line number Diff line change
@@ -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<Body>) -> Span {
tracing::error_span!("request",
uri = %req.uri(),
method = %req.method(),
source = request.extensions()
.get::<ConnectInfo<SocketAddr>>()
.map(|conn_info| tracing::field::display(conn_info))
.unwrap_or_else(|| tracing::field::display("<unknown>".to_string())),
status = tracing::field::Empty,
latency = tracing::field::Empty,
)
}

pub(crate) fn trace_layer_on_request(_req: &Request<Body>, _span: &Span) {
tracing::trace!("Got request")
}

pub(crate) fn trace_layer_on_response(resp: &Response<BoxBody>, 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");
}

0 comments on commit 5e73c69

Please sign in to comment.