mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
refactor(logs): generate request ids for all requests
This commit is contained in:
parent
9ef6b9430c
commit
69786d066a
@ -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,)
|
||||
})
|
||||
.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| {})
|
||||
.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| {},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@ -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,
|
||||
)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user