refactor(logs): generate request ids for all requests

This commit is contained in:
Kristofers Solo 2024-03-24 15:24:41 +02:00
parent 9ef6b9430c
commit 69786d066a
2 changed files with 25 additions and 10 deletions

View File

@ -17,20 +17,36 @@ use sqlx::PgPool;
pub use subscibtions::*;
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
use tracing::{info_span, Span};
use uuid::Uuid;
pub fn route(state: PgPool) -> Router {
Router::new()
.route("/health_check", get(health_check))
.route("/subscriptions", post(subscribe))
.with_state(state)
.layer(TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
let matched_path = request.extensions().get::<MatchedPath>().map(MatchedPath::as_str);
info_span!("http-request", method = ?request.method(), matched_path, some_other_field= tracing::field::Empty,)
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<_>| {
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
info_span!(
"http-request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
request_id=%Uuid::new_v4(),
)
})
.on_request(|_request: &Request<_>, _span: &Span| {})
.on_response(|_response: &Response<_>, _latency: Duration, _span: &Span| {})
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {})
.on_eos(|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span:&Span| {})
.on_failure(|_error: ServerErrorsFailureClass, _latency: Duration, _span:&Span| {})
.on_eos(
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {},
)
.on_failure(
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {},
),
)
}

View File

@ -15,7 +15,6 @@ pub struct FormData {
name = "Adding a new subscriber",
skip(form, pool),
fields(
request_id=%Uuid::new_v4(),
subscriber_email= %form.email,
subscriber_name = %form.name,
)