Skip to content

Commit

Permalink
wip: fix trace exports
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed May 16, 2024
1 parent fcc354c commit d3ba026
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 52 deletions.
2 changes: 1 addition & 1 deletion core/lib/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion core/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ pub use figment;
pub use time;
pub use tracing;

#[doc(hidden)]
#[macro_use]
pub mod trace;
#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E = crate::Error> = std::result::Result<T, E>;

Expand Down
35 changes: 10 additions & 25 deletions core/lib/src/shield/shield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -193,32 +191,19 @@ impl Fairing for Shield {
&& rocket.figment().profile() != Config::DEBUG_PROFILE
&& !self.is_enabled::<Hsts>();

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>) {
Expand Down
33 changes: 28 additions & 5 deletions core/lib/src/trace/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@ 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) => ({
$crate::tracing::span!($crate::tracing::Level::$level, $n $d (, $d ($f)* )?)
.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 {
Expand All @@ -49,6 +51,8 @@ macro_rules! span {
}};
}

#[doc(hidden)]
#[macro_export]
macro_rules! event {
($level:expr, $($args:tt)*) => {{
match $level {
Expand All @@ -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,
);
15 changes: 11 additions & 4 deletions core/lib/src/trace/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Option<&'a crate::Config>>>(_config: T) {
#[cfg(all(feature = "trace", debug_assertions))]
Expand Down
16 changes: 9 additions & 7 deletions core/lib/src/trace/subscriber/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ impl<K: private::FmtKind> RocketFmt<K> {
}
}

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<'_>,
Expand Down Expand Up @@ -142,9 +142,11 @@ impl<K: private::FmtKind> RocketFmt<K> {
})
}

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();

Expand All @@ -163,7 +165,7 @@ impl<K: private::FmtKind> RocketFmt<K> {
})
}

pub fn print<F: RecordFields>(
pub(crate) fn print<F: RecordFields>(
&self,
prefix: &dyn fmt::Display,
cont_prefix: &dyn fmt::Display,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/src/trace/subscriber/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
4 changes: 2 additions & 2 deletions core/lib/src/trace/subscriber/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ impl RequestId {

impl RequestIdLayer {
thread_local! {
pub static CURRENT_REQUEST_ID: Cell<Option<RequestId>> = Cell::new(None);
static CURRENT_REQUEST_ID: Cell<Option<RequestId>> = Cell::new(None);
}

fn current() -> Option<RequestId> {
pub fn current() -> Option<RequestId> {
Self::CURRENT_REQUEST_ID.get()
}
}
Expand Down
7 changes: 5 additions & 2 deletions core/lib/src/trace/traceable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
);
}
}

Expand Down

0 comments on commit d3ba026

Please sign in to comment.