diff --git a/src/lib.rs b/src/lib.rs index df882bb1c..a58ba80f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,10 @@ pub mod api_routes_http; pub mod code_migrations; pub mod prometheus_metrics; -pub mod root_span_builder; pub mod scheduled_tasks; pub mod session_middleware; -use crate::{ - code_migrations::run_advanced_migrations, - root_span_builder::QuieterRootSpanBuilder, - session_middleware::SessionMiddleware, -}; +use crate::{code_migrations::run_advanced_migrations, session_middleware::SessionMiddleware}; use activitypub_federation::config::{FederationConfig, FederationMiddleware}; use actix_cors::Cors; use actix_web::{ @@ -55,7 +50,7 @@ use reqwest_tracing::TracingMiddleware; use serde_json::json; use std::ops::Deref; use tokio::signal::unix::SignalKind; -use tracing_actix_web::TracingLogger; +use tracing_actix_web::{DefaultRootSpanBuilder, TracingLogger}; #[derive(Parser, Debug)] #[command( @@ -294,7 +289,7 @@ fn create_http_server( )) .wrap(middleware::Compress::default()) .wrap(cors_config) - .wrap(TracingLogger::::new()) + .wrap(TracingLogger::::new()) .wrap(ErrorHandlers::new().default_handler(jsonify_plain_text_errors)) .app_data(Data::new(context.clone())) .app_data(Data::new(rate_limit_cell.clone())) diff --git a/src/root_span_builder.rs b/src/root_span_builder.rs deleted file mode 100644 index e062ade21..000000000 --- a/src/root_span_builder.rs +++ /dev/null @@ -1,83 +0,0 @@ -use actix_web::{http::StatusCode, ResponseError}; -use tracing::Span; -use tracing_actix_web::RootSpanBuilder; - -// Code in this module adapted from DefaultRootSpanBuilder -// https://github.com/LukeMathWalker/tracing-actix-web/blob/main/src/root_span_builder.rs -// and root_span! -// https://github.com/LukeMathWalker/tracing-actix-web/blob/main/src/root_span_macro.rs - -pub struct QuieterRootSpanBuilder; - -impl RootSpanBuilder for QuieterRootSpanBuilder { - fn on_request_start(request: &actix_web::dev::ServiceRequest) -> Span { - let request_id = tracing_actix_web::root_span_macro::private::get_request_id(request); - - tracing::info_span!( - "HTTP request", - http.method = %request.method(), - http.scheme = request.connection_info().scheme(), - http.host = %request.connection_info().host(), - http.target = %request.uri().path(), - http.status_code = tracing::field::Empty, - otel.kind = "server", - otel.status_code = tracing::field::Empty, - trace_id = tracing::field::Empty, - request_id = %request_id, - exception.message = tracing::field::Empty, - // Not proper OpenTelemetry, but their terminology is fairly exception-centric - exception.details = tracing::field::Empty, - ) - } - - fn on_request_end( - span: tracing::Span, - outcome: &Result, actix_web::Error>, - ) { - match &outcome { - Ok(response) => { - if let Some(error) = response.response().error() { - // use the status code already constructed for the outgoing HTTP response - handle_error(span, response.status(), error.as_response_error()); - } else { - let code: i32 = response.response().status().as_u16().into(); - span.record("http.status_code", code); - span.record("otel.status_code", "OK"); - } - } - Err(error) => { - let response_error = error.as_response_error(); - handle_error(span, response_error.status_code(), response_error); - } - }; - } -} - -fn handle_error(span: Span, status_code: StatusCode, response_error: &dyn ResponseError) { - let code: i32 = status_code.as_u16().into(); - - span.record("http.status_code", code); - - if status_code.is_client_error() { - span.record("otel.status_code", "OK"); - } else { - span.record("otel.status_code", "ERROR"); - } - - // pre-formatting errors is a workaround for https://github.com/tokio-rs/tracing/issues/1565 - let display_error = format!("{response_error}"); - - tracing::info_span!( - parent: None, - "Error encountered while processing the incoming HTTP request" - ) - .in_scope(|| { - if status_code.is_client_error() { - tracing::warn!("{}", display_error); - } else { - tracing::error!("{}", display_error); - } - }); - - span.record("exception.message", &tracing::field::display(display_error)); -}