From d3ba026062b296eb191ab00d686949d5b1595c5c Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 15 May 2024 18:57:12 -0700 Subject: [PATCH] wip: fix trace exports --- core/lib/src/config/config.rs | 2 +- core/lib/src/error.rs | 2 +- core/lib/src/lib.rs | 1 - core/lib/src/lifecycle.rs | 2 +- core/lib/src/rocket.rs | 2 +- core/lib/src/server.rs | 2 +- core/lib/src/shield/shield.rs | 35 ++++++--------------- core/lib/src/trace/macros.rs | 33 ++++++++++++++++--- core/lib/src/trace/mod.rs | 15 ++++++--- core/lib/src/trace/subscriber/common.rs | 16 +++++----- core/lib/src/trace/subscriber/mod.rs | 2 +- core/lib/src/trace/subscriber/request_id.rs | 4 +-- core/lib/src/trace/traceable.rs | 7 +++-- 13 files changed, 71 insertions(+), 52 deletions(-) diff --git a/core/lib/src/config/config.rs b/core/lib/src/config/config.rs index b5f1984278..134e5867a1 100644 --- a/core/lib/src/config/config.rs +++ b/core/lib/src/config/config.rs @@ -10,7 +10,7 @@ use crate::config::{ShutdownConfig, Ident, CliColors}; use crate::request::{self, Request, FromRequest}; use crate::http::uncased::Uncased; use crate::data::Limits; -use crate::trace::traceable::Traceable; +use crate::trace::Traceable; /// Rocket server configuration. /// diff --git a/core/lib/src/error.rs b/core/lib/src/error.rs index 7349297c14..8d8934f6f8 100644 --- a/core/lib/src/error.rs +++ b/core/lib/src/error.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use figment::Profile; use crate::listener::Endpoint; -use crate::trace::traceable::Traceable; +use crate::trace::Traceable; use crate::{Ignite, Orbit, Phase, Rocket}; /// An error that occurs during launch. diff --git a/core/lib/src/lib.rs b/core/lib/src/lib.rs index b42203dcb1..ef02e622bf 100644 --- a/core/lib/src/lib.rs +++ b/core/lib/src/lib.rs @@ -122,7 +122,6 @@ pub use figment; pub use time; pub use tracing; -#[doc(hidden)] #[macro_use] pub mod trace; #[macro_use] diff --git a/core/lib/src/lifecycle.rs b/core/lib/src/lifecycle.rs index 8b132cfe61..26f17ed664 100644 --- a/core/lib/src/lifecycle.rs +++ b/core/lib/src/lifecycle.rs @@ -1,7 +1,7 @@ use futures::future::{FutureExt, Future}; use crate::{route, catcher, Rocket, Orbit, Request, Response, Data}; -use crate::trace::traceable::Traceable; +use crate::trace::Traceable; use crate::util::Formatter; use crate::data::IoHandler; use crate::http::{Method, Status, Header}; diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index f73992caf4..5eb8c649e1 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -11,7 +11,7 @@ use figment::{Figment, Provider}; use futures::TryFutureExt; use crate::shutdown::{Stages, Shutdown}; -use crate::trace::traceable::{Traceable, TraceableCollection}; +use crate::trace::{Traceable, TraceableCollection}; use crate::{sentinel, shield::Shield, Catcher, Config, Route}; use crate::listener::{Bind, DefaultListener, Endpoint, Listener}; use crate::router::Router; diff --git a/core/lib/src/server.rs b/core/lib/src/server.rs index 2d28c14ea8..6820dda48a 100644 --- a/core/lib/src/server.rs +++ b/core/lib/src/server.rs @@ -17,7 +17,7 @@ use crate::error::log_server_error; use crate::data::{IoStream, RawStream}; use crate::util::{spawn_inspect, FutureExt, ReaderStream}; use crate::http::Status; -use crate::trace::traceable::{Traceable, TraceableCollection}; +use crate::trace::{Traceable, TraceableCollection}; type Result = std::result::Result; diff --git a/core/lib/src/shield/shield.rs b/core/lib/src/shield/shield.rs index 7b25d6f426..905f37d3b9 100644 --- a/core/lib/src/shield/shield.rs +++ b/core/lib/src/shield/shield.rs @@ -5,7 +5,7 @@ use crate::{Rocket, Request, Response, Orbit, Config}; use crate::fairing::{Fairing, Info, Kind}; use crate::http::{Header, uncased::UncasedStr}; use crate::shield::*; -use crate::trace::traceable::*; +use crate::trace::*; /// A [`Fairing`] that injects browser security and privacy headers into all /// outgoing responses. @@ -59,10 +59,8 @@ use crate::trace::traceable::*; /// /// If TLS is configured and enabled when the application is launched in a /// non-debug profile, HSTS is automatically enabled with its default policy and -/// a warning is logged. -/// -/// To get rid of this warning, explicitly [`Shield::enable()`] an [`Hsts`] -/// policy. +/// a warning is logged. To get rid of this warning, explicitly +/// [`Shield::enable()`] an [`Hsts`] policy. pub struct Shield { /// Enabled policies where the key is the header name. policies: HashMap<&'static UncasedStr, Header<'static>>, @@ -193,32 +191,19 @@ impl Fairing for Shield { && rocket.figment().profile() != Config::DEBUG_PROFILE && !self.is_enabled::(); + if force_hsts { + self.force_hsts.store(true, Ordering::Release); + } + info_span!("shield" [policies = self.policies.len()] => { self.policies.values().trace_all_info(); if force_hsts { - warn!("Detected TLS-enabled liftoff without enabling HSTS."); - warn!("Shield has enabled a default HSTS policy."); - info!("To remove this warning, configure an HSTS policy."); + warn!("Detected TLS-enabled liftoff without enabling HSTS.\n\ + Shield has enabled a default HSTS policy.\n\ + To remove this warning, configure an HSTS policy."); } }) - - // trace::collection_info!("shield", force_hsts => self.polices.values(), { - // warn!("Detected TLS-enabled liftoff without enabling HSTS."); - // warn!("Shield has enabled a default HSTS policy."); - // info!("To remove this warning, configure an HSTS policy."); - // }); - - // // tracing::info_span!("shield", force_hsts).in_scope(|| { - // // self.polices.values().trace(); - // // for header in self.policies.values() { - // // info!(name: "header", name = header.name().as_str(), value = header.value()); - // // } - // - // warn!("Detected TLS-enabled liftoff without enabling HSTS."); - // warn!("Shield has enabled a default HSTS policy."); - // info!("To remove this warning, configure an HSTS policy."); - // }); } async fn on_response<'r>(&self, _: &'r Request<'_>, response: &mut Response<'r>) { diff --git a/core/lib/src/trace/macros.rs b/core/lib/src/trace/macros.rs index 05e39a4818..4bd759f183 100644 --- a/core/lib/src/trace/macros.rs +++ b/core/lib/src/trace/macros.rs @@ -4,21 +4,23 @@ macro_rules! declare_macro { ); ([$d:tt] $name:ident $level:ident) => ( + #[doc(hidden)] #[macro_export] macro_rules! $name { ($d ($t:tt)*) => ($crate::tracing::$level!($d ($t)*)); } + + // pub use $name as $name; ); } -declare_macro!(error error, info info, trace trace, debug debug, warn warn); - macro_rules! declare_span_macro { ($($name:ident $level:ident),* $(,)?) => ( $(declare_span_macro!([$] $name $level);)* ); ([$d:tt] $name:ident $level:ident) => ( + #[doc(hidden)] #[macro_export] macro_rules! $name { ($n:literal $d ([ $d ($f:tt)* ])? => $in_scope:expr) => ({ @@ -26,12 +28,12 @@ macro_rules! declare_span_macro { .in_scope(|| $in_scope); }) } + + #[doc(inline)] + pub use $name as $name; ); } -declare_span_macro!(error_span ERROR, warn_span WARN, - info_span INFO, trace_span TRACE, debug_span DEBUG); - macro_rules! span { ($level:expr, $($args:tt)*) => {{ match $level { @@ -49,6 +51,8 @@ macro_rules! span { }}; } +#[doc(hidden)] +#[macro_export] macro_rules! event { ($level:expr, $($args:tt)*) => {{ match $level { @@ -64,3 +68,22 @@ macro_rules! event { $crate::tracing::event!(name: $n, target: concat!("rocket::", $n), $level, $($args)*); }}; } + +#[doc(inline)] +pub use event as event; + +declare_macro!( + error error, + info info, + trace trace, + debug debug, + warn warn +); + +declare_span_macro!( + error_span ERROR, + warn_span WARN, + info_span INFO, + trace_span TRACE, + debug_span DEBUG, +); diff --git a/core/lib/src/trace/mod.rs b/core/lib/src/trace/mod.rs index 3d604fbb8e..82de549621 100644 --- a/core/lib/src/trace/mod.rs +++ b/core/lib/src/trace/mod.rs @@ -1,11 +1,18 @@ #[macro_use] -pub mod macros; +mod macros; +mod traceable; + #[cfg(feature = "trace")] +#[cfg_attr(nightly, doc(cfg(feature = "trace")))] pub mod subscriber; -pub mod level; -pub mod traceable; -pub use traceable::Traceable; +pub(crate) mod level; + +#[doc(inline)] +pub use traceable::{Traceable, TraceableCollection}; + +#[doc(inline)] +pub use macros::*; pub fn init<'a, T: Into>>(_config: T) { #[cfg(all(feature = "trace", debug_assertions))] diff --git a/core/lib/src/trace/subscriber/common.rs b/core/lib/src/trace/subscriber/common.rs index a35be827dd..f99796555f 100644 --- a/core/lib/src/trace/subscriber/common.rs +++ b/core/lib/src/trace/subscriber/common.rs @@ -103,15 +103,15 @@ impl RocketFmt { } } - pub fn has_message(&self, meta: &Metadata<'_>) -> bool { + pub(crate) fn has_message(&self, meta: &Metadata<'_>) -> bool { meta.fields().field("message").is_some() } - pub fn has_data_fields(&self, meta: &Metadata<'_>) -> bool { + pub(crate) fn has_data_fields(&self, meta: &Metadata<'_>) -> bool { meta.fields().iter().any(|f| f.name() != "message") } - pub fn message<'a, F: RecordFields + 'a>(&self, + pub(crate) fn message<'a, F: RecordFields + 'a>(&self, init_prefix: &'a dyn fmt::Display, cont_prefix: &'a dyn fmt::Display, meta: &'a Metadata<'_>, @@ -142,9 +142,11 @@ impl RocketFmt { }) } - pub fn compact_fields<'a, F>(&self, meta: &'a Metadata<'_>, data: F) -> impl fmt::Display + 'a - where F: RecordFields + 'a - { + pub(crate) fn compact_fields<'a, F: RecordFields + 'a>( + &self, + meta: &'a Metadata<'_>, + data: F + ) -> impl fmt::Display + 'a { let key_style = self.style(meta).bold(); let val_style = self.style(meta).primary(); @@ -163,7 +165,7 @@ impl RocketFmt { }) } - pub fn print( + pub(crate) fn print( &self, prefix: &dyn fmt::Display, cont_prefix: &dyn fmt::Display, diff --git a/core/lib/src/trace/subscriber/mod.rs b/core/lib/src/trace/subscriber/mod.rs index d7a2e66d83..ed2d71fb46 100644 --- a/core/lib/src/trace/subscriber/mod.rs +++ b/core/lib/src/trace/subscriber/mod.rs @@ -4,10 +4,10 @@ mod compact; mod common; mod request_id; -pub use visit::{RecordDisplay, Data}; pub use pretty::Pretty; pub use compact::Compact; pub use common::RocketFmt; pub use request_id::{RequestId, RequestIdLayer}; pub(crate) use common::Handle; +pub(crate) use visit::{RecordDisplay, Data}; diff --git a/core/lib/src/trace/subscriber/request_id.rs b/core/lib/src/trace/subscriber/request_id.rs index b40924d11d..5913476c8f 100644 --- a/core/lib/src/trace/subscriber/request_id.rs +++ b/core/lib/src/trace/subscriber/request_id.rs @@ -61,10 +61,10 @@ impl RequestId { impl RequestIdLayer { thread_local! { - pub static CURRENT_REQUEST_ID: Cell> = Cell::new(None); + static CURRENT_REQUEST_ID: Cell> = Cell::new(None); } - fn current() -> Option { + pub fn current() -> Option { Self::CURRENT_REQUEST_ID.get() } } diff --git a/core/lib/src/trace/traceable.rs b/core/lib/src/trace/traceable.rs index e54ff46cc2..e0334c0da4 100644 --- a/core/lib/src/trace/traceable.rs +++ b/core/lib/src/trace/traceable.rs @@ -255,8 +255,11 @@ impl Traceable for Error { impl Traceable for Sentry { fn trace(&self, level: Level) { - let (file, line, column) = self.location; - event!(level, "sentry", "type" = self.type_name, file, line, column); + let (file, line, col) = self.location; + event!(level, "sentry", + type_name = self.type_name, + location = %Formatter(|f| write!(f, "{file}:{line}:{col}")) + ); } }