mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
refactor(logs): move to separate file
refactor(logs): move
This commit is contained in:
parent
06a1d24fc0
commit
db3cd40171
@ -1,2 +1,3 @@
|
|||||||
pub mod configuation;
|
pub mod configuation;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
pub mod telemetry;
|
||||||
|
|||||||
44
src/main.rs
44
src/main.rs
@ -1,32 +1,17 @@
|
|||||||
use std::{net::SocketAddr, time::Duration};
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use axum::{
|
|
||||||
body::Bytes,
|
|
||||||
extract::MatchedPath,
|
|
||||||
http::{HeaderMap, Request},
|
|
||||||
response::Response,
|
|
||||||
};
|
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
use zero2prod::{
|
||||||
use tracing::{info_span, subscriber::set_global_default, Span};
|
configuation::get_configuration,
|
||||||
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
routes::route,
|
||||||
use tracing_log::LogTracer;
|
telemetry::{get_subscriber, init_subscriber},
|
||||||
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
};
|
||||||
use zero2prod::{configuation::get_configuration, routes::route};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), std::io::Error> {
|
async fn main() -> Result<(), std::io::Error> {
|
||||||
LogTracer::init().expect("Failed to set logger");
|
let subscriber = get_subscriber("zero2prod", "info");
|
||||||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into());
|
init_subscriber(subscriber);
|
||||||
let formatting_layer = BunyanFormattingLayer::new("zero2prod".into(), std::io::stdout);
|
|
||||||
let subscriber = Registry::default()
|
|
||||||
.with(env_filter)
|
|
||||||
.with(JsonStorageLayer)
|
|
||||||
.with(formatting_layer);
|
|
||||||
|
|
||||||
set_global_default(subscriber).expect("Failed to set subscriber.");
|
|
||||||
|
|
||||||
let configuation = get_configuration().expect("Failed to read configuation.");
|
let configuation = get_configuration().expect("Failed to read configuation.");
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.connect(&configuation.database.to_string())
|
.connect(&configuation.database.to_string())
|
||||||
@ -37,16 +22,5 @@ async fn main() -> Result<(), std::io::Error> {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to bind port 8000.");
|
.expect("Failed to bind port 8000.");
|
||||||
|
|
||||||
let route = route(pool)
|
axum::serve(listener, route(pool)).await
|
||||||
.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| {})
|
|
||||||
);
|
|
||||||
axum::serve(listener, route).await
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,13 @@
|
|||||||
mod health_check;
|
mod health_check;
|
||||||
mod subscibtions;
|
mod subscibtions;
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
|
body::Bytes,
|
||||||
|
extract::MatchedPath,
|
||||||
|
http::{HeaderMap, Request},
|
||||||
|
response::Response,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
@ -9,10 +15,22 @@ use axum::{
|
|||||||
pub use health_check::*;
|
pub use health_check::*;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
pub use subscibtions::*;
|
pub use subscibtions::*;
|
||||||
|
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
||||||
|
use tracing::{info_span, Span};
|
||||||
|
|
||||||
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<_>| {
|
||||||
|
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| {})
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/telemetry.rs
Normal file
18
src/telemetry.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use tracing::{subscriber::set_global_default, Subscriber};
|
||||||
|
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
|
||||||
|
use tracing_log::LogTracer;
|
||||||
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
||||||
|
|
||||||
|
pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Sync + Send {
|
||||||
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| env_filter.into());
|
||||||
|
let formatting_layer = BunyanFormattingLayer::new(name.into(), std::io::stdout);
|
||||||
|
Registry::default()
|
||||||
|
.with(env_filter)
|
||||||
|
.with(JsonStorageLayer)
|
||||||
|
.with(formatting_layer)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_subscriber(subscriber: impl Subscriber + Sync + Send) {
|
||||||
|
LogTracer::init().expect("Failed to set logger");
|
||||||
|
set_global_default(subscriber).expect("Failed to set subscriber.");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user