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::*;
|
pub use subscibtions::*;
|
||||||
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
||||||
use tracing::{info_span, Span};
|
use tracing::{info_span, Span};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn route(state: PgPool) -> Router {
|
pub fn route(state: PgPool) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/health_check", get(health_check))
|
.route("/health_check", get(health_check))
|
||||||
.route("/subscriptions", post(subscribe))
|
.route("/subscriptions", post(subscribe))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
.layer(TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
.layer(
|
||||||
let matched_path = request.extensions().get::<MatchedPath>().map(MatchedPath::as_str);
|
TraceLayer::new_for_http()
|
||||||
info_span!("http-request", method = ?request.method(), matched_path, some_other_field= tracing::field::Empty,)
|
.make_span_with(|request: &Request<_>| {
|
||||||
})
|
let matched_path = request
|
||||||
.on_request(|_request: &Request<_>, _span: &Span | {})
|
.extensions()
|
||||||
.on_response(|_response: &Response<_>, _latency: Duration, _span:&Span|{})
|
.get::<MatchedPath>()
|
||||||
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span:&Span|{})
|
.map(MatchedPath::as_str);
|
||||||
.on_eos(|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span:&Span| {})
|
info_span!(
|
||||||
.on_failure(|_error: ServerErrorsFailureClass, _latency: Duration, _span:&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",
|
name = "Adding a new subscriber",
|
||||||
skip(form, pool),
|
skip(form, pool),
|
||||||
fields(
|
fields(
|
||||||
request_id=%Uuid::new_v4(),
|
|
||||||
subscriber_email= %form.email,
|
subscriber_email= %form.email,
|
||||||
subscriber_name = %form.name,
|
subscriber_name = %form.name,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user