Skip to content

Commit

Permalink
wip: fix tracing macro exports
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed May 21, 2024
1 parent 5c7b06f commit 7bc2fea
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 120 deletions.
4 changes: 2 additions & 2 deletions contrib/dyn_templates/src/engine/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Engine for Tera {

// Finally try to tell Tera about all of the templates.
if let Err(e) = tera.add_template_files(files) {
error_span!("Tera templating initialization failed" => {
span_error!("templating", "Tera templating initialization failed" => {
let mut error = Some(&e as &dyn Error);
while let Some(err) = error {
error!("{err}");
Expand All @@ -48,7 +48,7 @@ impl Engine for Tera {
match Tera::render(self, template, &tera_ctx) {
Ok(string) => Some(string),
Err(e) => {
error_span!("failed to render Tera template" [template] => {
span_error!("templating", template, "failed to render Tera template" => {
let mut error = Some(&e as &dyn Error);
while let Some(err) = error {
error!("{err}");
Expand Down
2 changes: 1 addition & 1 deletion contrib/dyn_templates/src/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Fairing for TemplateFairing {
let cm = rocket.state::<ContextManager>()
.expect("Template ContextManager registered in on_ignite");

info_span!("templating" => {
span_info!("templating" => {
info!(directory = %Source::from(&*cm.context().root));
info!(engines = ?Engines::ENABLED_EXTENSIONS);
});
Expand Down
2 changes: 1 addition & 1 deletion contrib/dyn_templates/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl Template {
})?;

let value = self.value.map_err(|e| {
error_span!("template context failed to serialize" => e.trace_error());
span_error!("templating", "template context failed to serialize" => e.trace_error());
Status::InternalServerError
})?;

Expand Down
4 changes: 2 additions & 2 deletions contrib/sync_db_pools/lib/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<K: 'static, C: Poolable> ConnectionPool<K, C> {
let config = match Config::from(database, &rocket) {
Ok(config) => config,
Err(e) => {
error_span!("database configuration error" [database] => e.trace_error());
span_error!("database configuration error", database => e.trace_error());
return Err(rocket);
}
};
Expand All @@ -82,7 +82,7 @@ impl<K: 'static, C: Poolable> ConnectionPool<K, C> {
_marker: PhantomData,
})),
Err(Error::Config(e)) => {
error_span!("database configuration error" [database] => e.trace_error());
span_error!("database configuration error", database => e.trace_error());
Err(rocket)
}
Err(Error::Pool(reason)) => {
Expand Down
21 changes: 11 additions & 10 deletions core/codegen/src/attribute/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ fn query_decls(route: &Route) -> Option<TokenStream> {
)*

if !__e.is_empty() {
::rocket::info_span!("query string failed to match route declaration" => {
for _err in __e { ::rocket::info!("{_err}"); }
});
::rocket::trace::span_info!("codegen",
"query string failed to match route declaration" =>
{ for _err in __e { ::rocket::trace::info!("{_err}"); } }
);

return #Outcome::Forward((#__data, #Status::UnprocessableEntity));
}
Expand All @@ -127,15 +128,15 @@ fn request_guard_decl(guard: &Guard) -> TokenStream {
let #ident: #ty = match <#ty as #FromRequest>::from_request(#__req).await {
#Outcome::Success(__v) => __v,
#Outcome::Forward(__e) => {
::rocket::info!(name: "forward", parameter = stringify!(#ident),
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
type_name = stringify!(#ty), status = __e.code,
"request guard forwarding");

return #Outcome::Forward((#__data, __e));
},
#[allow(unreachable_code)]
#Outcome::Error((__c, __e)) => {
::rocket::info!(name: "failure", parameter = stringify!(#ident),
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
type_name = stringify!(#ty), reason = %#display_hack!(__e),
"request guard failed");

Expand All @@ -154,7 +155,7 @@ fn param_guard_decl(guard: &Guard) -> TokenStream {

// Returned when a dynamic parameter fails to parse.
let parse_error = quote!({
::rocket::info!(name: "forward", parameter = #name,
::rocket::trace::info!(name: "forward", parameter = #name,
type_name = stringify!(#ty), reason = %#display_hack!(__error),
"path guard forwarding");

Expand All @@ -172,7 +173,7 @@ fn param_guard_decl(guard: &Guard) -> TokenStream {
#_Err(__error) => return #parse_error,
},
#_None => {
::rocket::error!(
::rocket::trace::error!(
"Internal invariant broken: dyn param {} not found.\n\
Please report this to the Rocket issue tracker.\n\
https://github.com/rwf2/Rocket/issues", #i);
Expand Down Expand Up @@ -202,15 +203,15 @@ fn data_guard_decl(guard: &Guard) -> TokenStream {
let #ident: #ty = match <#ty as #FromData>::from_data(#__req, #__data).await {
#Outcome::Success(__d) => __d,
#Outcome::Forward((__d, __e)) => {
::rocket::info!(name: "forward", parameter = stringify!(#ident),
::rocket::trace::info!(name: "forward", parameter = stringify!(#ident),
type_name = stringify!(#ty), status = __e.code,
"data guard forwarding");

return #Outcome::Forward((__d, __e));
}
#[allow(unreachable_code)]
#Outcome::Error((__c, __e)) => {
::rocket::info!(name: "failure", parameter = stringify!(#ident),
::rocket::trace::info!(name: "failure", parameter = stringify!(#ident),
type_name = stringify!(#ty), reason = %#display_hack!(__e),
"data guard failed");

Expand Down Expand Up @@ -246,7 +247,7 @@ fn internal_uri_macro_decl(route: &Route) -> TokenStream {
/// Rocket generated URI macro.
macro_rules! #inner_macro_name {
($($token:tt)*) => {{
rocket::rocket_internal_uri!(#route_uri, (#(#uri_args),*), $($token)*)
::rocket::rocket_internal_uri!(#route_uri, (#(#uri_args),*), $($token)*)
}};
}

Expand Down
44 changes: 22 additions & 22 deletions core/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::sync::Arc;
use figment::Profile;

use crate::listener::Endpoint;
use crate::trace::Trace;
use crate::{Ignite, Orbit, Phase, Rocket};
use crate::trace::Trace;

/// An error that occurs during launch.
///
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Error {
match result {
Ok(_) => process::ExitCode::SUCCESS,
Err(e) => {
error_span!("aborting launch due to error" => e.trace_error());
span_error!("error", "aborting launch due to error" => e.trace_error());
process::ExitCode::SUCCESS
}
}
Expand Down Expand Up @@ -200,14 +200,14 @@ impl fmt::Display for ServerError<'_> {
pub(crate) fn log_server_error(error: &(dyn StdError + 'static)) {
let mut error: &(dyn StdError + 'static) = error;
if error.downcast_ref::<hyper::Error>().is_some() {
warn_span!("minor server error" ["{}", ServerError(error)] => {
span_warn!("request error", "{}", ServerError(error) => {
while let Some(source) = error.source() {
error = source;
warn!("{}", ServerError(error));
}
});
} else {
error_span!("server error" ["{}", ServerError(error)] => {
span_error!("server error", "{}", ServerError(error) => {
while let Some(source) = error.source() {
error = source;
error!("{}", ServerError(error));
Expand All @@ -216,31 +216,16 @@ pub(crate) fn log_server_error(error: &(dyn StdError + 'static)) {
}
}

#[doc(hidden)]
#[macro_export]
macro_rules! display_hack {
($v:expr) => ({
#[allow(unused_imports)]
use $crate::error::display_hack_impl::{DisplayHack, DefaultDisplay as _};

#[allow(unreachable_code)]
DisplayHack($v).display()
})
}

#[doc(hidden)]
pub use display_hack as display_hack;

#[doc(hidden)]
pub mod display_hack_impl {
use super::*;
use crate::util::Formatter;

/// The *magic*.
///
/// This type implements a `display()` method for all types that are either
/// `fmt::Display` _or_ `fmt::Debug`, using the former when available. It
/// does so by using a "specialization" hack: it has a blanket
/// This type implements a `display()` method using an internal `T` that is
/// either `fmt::Display` _or_ `fmt::Debug`, using the former when
/// available. It does so by using a "specialization" hack: it has a blanket
/// DefaultDisplay trait impl for all types that are `fmt::Debug` and a
/// "specialized" inherent impl for all types that are `fmt::Display`.
///
Expand Down Expand Up @@ -271,3 +256,18 @@ pub mod display_hack_impl {
}
}
}

#[doc(hidden)]
#[macro_export]
macro_rules! display_hack {
($v:expr) => ({
#[allow(unused_imports)]
use $crate::error::display_hack_impl::{DisplayHack, DefaultDisplay as _};

#[allow(unreachable_code)]
DisplayHack($v).display()
})
}

#[doc(hidden)]
pub use display_hack as display_hack;
2 changes: 1 addition & 1 deletion core/lib/src/fairing/ad_hoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use futures::future::{Future, BoxFuture, FutureExt};
use parking_lot::Mutex;

use crate::route::RouteUri;
use crate::{Rocket, Request, Response, Data, Build, Orbit};
use crate::fairing::{Fairing, Kind, Info, Result};
use crate::{Rocket, Request, Response, Data, Build, Orbit};

/// A ad-hoc fairing that can be created from a function or closure.
///
Expand Down
10 changes: 0 additions & 10 deletions core/lib/src/fairing/fairings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ impl Fairings {
iter!(self, self.active().collect::<HashSet<_>>().into_iter())
.map(|v| v.1)
.collect()
// .into_iter()
// .map(|i| )
// if !active_fairings.is_empty() {
// tracing::info_span!("fairings").in_scope(|| {
// for (_, fairing) in iter!(self, active_fairings.into_iter()) {
// let (name, kind) = (fairing.info().name, fairing.info().kind);
// info!(name: "fairing", name, %kind)
// }
// });
// }
}

pub fn add(&mut self, fairing: Box<dyn Fairing>) {
Expand Down
10 changes: 5 additions & 5 deletions core/lib/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Rocket<Build> {
// are visible; we use the final config to set a max log-level in ignite
self.figment = Figment::from(provider);
crate::trace::init(Config::try_from(&self.figment).ok().as_ref());
trace_span!("reconfigure" => self.figment().trace_trace());
span_trace!("reconfigure" => self.figment().trace_trace());

self
}
Expand Down Expand Up @@ -564,14 +564,14 @@ impl Rocket<Build> {
// Log everything we know: config, routes, catchers, fairings.
// TODO: Store/print managed state type names?
let fairings = self.fairings.unique_set();
info_span!("config" [profile = %self.figment().profile()] => {
span_info!("config", profile = %self.figment().profile() => {
config.trace_info();
self.figment().trace_debug();
});

info_span!("routes" [count = self.routes.len()] => self.routes().trace_all_info());
info_span!("catchers" [count = self.catchers.len()] => self.catchers().trace_all_info());
info_span!("fairings" [count = fairings.len()] => fairings.trace_all_info());
span_info!("routes", count = self.routes.len() => self.routes().trace_all_info());
span_info!("catchers", count = self.catchers.len() => self.catchers().trace_all_info());
span_info!("fairings", count = fairings.len() => fairings.trace_all_info());

// Ignite the rocket.
let rocket: Rocket<Ignite> = Rocket(Igniting {
Expand Down
6 changes: 3 additions & 3 deletions core/lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Rocket<Orbit> {
Request::from_hyp(rocket, parts, connection).unwrap_or_else(|e| e)
});

debug_span!("request headers" => request.inner().headers().iter().trace_all_debug());
span_debug!("request headers" => request.inner().headers().iter().trace_all_debug());
let mut response = request.into_response(
stream,
|rocket, request, data| Box::pin(rocket.preprocess(request, data)),
Expand All @@ -54,7 +54,7 @@ impl Rocket<Orbit> {

// TODO: Should upgrades be handled in dispatch?
response.inner().trace_info();
debug_span!("response headers" => response.inner().headers().iter().trace_all_debug());
span_debug!("response headers" => response.inner().headers().iter().trace_all_debug());
let io_handler = response.make_io_handler(Rocket::extract_io_handler);
if let (Some((proto, handler)), Some(upgrade)) = (io_handler, upgrade) {
let upgrade = upgrade.map_ok(IoStream::from).map_err(io::Error::other);
Expand Down Expand Up @@ -92,7 +92,7 @@ async fn io_handler_task<S>(proto: String, stream: S, mut handler: ErasedIoHandl
Err(e) => return warn!(error = %e, "i/o upgrade failed"),
};

info!("i/o upgrade succeeded");
debug!("i/o upgrade succeeded");
if let Err(e) = handler.take().io(stream).await {
match e.kind() {
io::ErrorKind::BrokenPipe => warn!("i/o handler closed"),
Expand Down
8 changes: 4 additions & 4 deletions core/lib/src/shield/shield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
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::*;
use crate::shield::{Frame, Hsts, NoSniff, Permission, Policy};
use crate::trace::{Trace, TraceAll};

/// A [`Fairing`] that injects browser security and privacy headers into all
/// outgoing responses.
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Fairing for Shield {
self.force_hsts.store(true, Ordering::Release);
}

info_span!("shield" [policies = self.policies.len()] => {
span_info!("shield", policies = self.policies.len() => {
self.policies.values().trace_all_info();

if force_hsts {
Expand All @@ -211,7 +211,7 @@ impl Fairing for Shield {
// the header is not already in the response.
for header in self.policies.values() {
if response.headers().contains(header.name()) {
warn_span!("shield refusing to overwrite existing response header" => {
span_warn!("shield", "shield refusing to overwrite existing response header" => {
header.trace_warn();
});

Expand Down
Loading

0 comments on commit 7bc2fea

Please sign in to comment.