Skip to content

Commit

Permalink
feat(tracing): trace http response & pretty (#49)
Browse files Browse the repository at this point in the history
* refactor(backend): trace http response

* refactor(tracing): pretty
  • Loading branch information
kwaa committed Jun 16, 2024
1 parent 066d188 commit b0d2616
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ panic = "abort"
strip = "symbols"

[features]
default = ["snmalloc", "hatsu_tracing/console"]
default = ["snmalloc", "hatsu_tracing/console", "hatsu_tracing/pretty"]
snmalloc = ["dep:snmalloc-rs"]
# hatsu_tracing
tracing-conosle = ["hatsu_tracing/console"]
tracing-json = ["hatsu_tracing/json"]
tracing_conosle = ["hatsu_tracing/console"]
tracing_json = ["hatsu_tracing/json"]
tracing_pretty = ["hatsu_tracing/pretty"]

[workspace]
members = [
Expand Down
12 changes: 10 additions & 2 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use std::net::SocketAddr;
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use hatsu_utils::{AppData, AppError};
use tokio_graceful_shutdown::{IntoSubsystem, SubsystemHandle};
use tower_http::{cors::CorsLayer, trace::TraceLayer};
use tower_http::{
cors::CorsLayer,
trace::{self, TraceLayer},
};
use tracing::Level;

mod favicon;
mod routes;
Expand Down Expand Up @@ -31,7 +35,11 @@ impl IntoSubsystem<AppError, AppError> for Server {
let app = routes::routes()
.layer(FederationMiddleware::new(self.federation_config))
.layer(CorsLayer::permissive())
.layer(TraceLayer::new_for_http());
.layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
);

// axum 0.6
// run our app with hyper
Expand Down
1 change: 1 addition & 0 deletions crates/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/lib.rs"
default = []
console = ["dep:console-subscriber"]
json = ["tracing-subscriber/json"]
pretty = []
# TODO: opentelemetry


Expand Down
7 changes: 6 additions & 1 deletion crates/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ use tracing_subscriber::{
};

pub fn init() -> Result<(), AppError> {
let fmt_layer = fmt_layer();

#[cfg(feature = "pretty")]
let fmt_layer = fmt_layer.pretty();

let registry = tracing_subscriber::registry()
.with(filter_layer())
.with(fmt_layer())
.with(fmt_layer)
.with(tracing_error::ErrorLayer::default());

#[cfg(feature = "console")]
Expand Down

0 comments on commit b0d2616

Please sign in to comment.