Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt Tracing #1410

Closed
wants to merge 63 commits into from
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f5a8681
WIP
hawkw Jul 21, 2020
debb008
filtering, nicer formatting
hawkw Jul 21, 2020
e0a0bfd
more trace
hawkw Jul 21, 2020
6e0e902
fix filtering
hawkw Jul 21, 2020
47540a9
mounting works
hawkw Jul 21, 2020
88cee8c
WIP
hawkw Jul 22, 2020
d920738
skip span names for "rocket-ier" formatitng
hawkw Jul 22, 2020
bce8f14
this is cute, right?
hawkw Jul 22, 2020
e3398e5
fix contrib
hawkw Jul 23, 2020
86c5ef5
use span names if there's no message
hawkw Jul 23, 2020
88feb8e
start removing underscore macros
hawkw Jul 23, 2020
cbe5581
less obnoxious bold, nicer debug/trace
hawkw Jul 23, 2020
43c95db
don't constantly screw up colors
hawkw Jul 23, 2020
1eec8f6
unpatch
hawkw Jul 25, 2020
2589dee
add docs & reexports
hawkw Aug 11, 2020
533d792
cleanup, remove logger module
hawkw Aug 11, 2020
e7b55d7
whoops
hawkw Aug 11, 2020
2dbd927
Merge branch 'master' into eliza/trace
hawkw Aug 11, 2020
4bd3da2
trailing whitespace
hawkw Aug 13, 2020
9905887
trailing whitespace again
hawkw Aug 13, 2020
118e13e
fixup
hawkw Aug 13, 2020
03533ce
use `macro_use` imports everywhere
hawkw Aug 13, 2020
9e6adc6
use spans for all indentation-based messages
hawkw Aug 13, 2020
ec5066b
update codegen
hawkw Aug 13, 2020
6144caa
add spans to generated catcher/route fns
hawkw Aug 13, 2020
e1b67db
make catcher spans info
hawkw Aug 13, 2020
cd124d8
bold span names when no message is present
hawkw Aug 13, 2020
bc492ba
add more info to generated spans
hawkw Aug 14, 2020
88e965e
Replace `log` with `tracing` and `tracing-subscriber`.
hawkw Oct 31, 2020
6fbbf94
style: remove most usage of span.enter()
jebrosen Nov 1, 2020
c8e4b6b
fix: macros are weird sometimes
jebrosen Nov 1, 2020
eeeab88
fix: use of moved value
jebrosen Nov 1, 2020
41237b6
Merge remote-tracking branch 'jebrosen/adopt-tracing' into eliza/trace
hawkw Nov 2, 2020
9f86214
allow libtest to capture trace logs
hawkw Nov 2, 2020
9e95437
remove workaround
hawkw Nov 2, 2020
9776ca3
remove workarounds, add `log` compat
hawkw Nov 3, 2020
fa5f30d
examples for a custom subscriber, env_logger
hawkw Nov 3, 2020
73ca07c
Merge branch 'master' into eliza/trace
hawkw Nov 4, 2020
e68d66d
use structured fields in more places
hawkw Nov 4, 2020
4e9a308
post-rebase fixup
hawkw Nov 4, 2020
a868cab
use `in_scope` for errors
hawkw Nov 4, 2020
cb1b024
allow adding directives to Rocket's filter
hawkw Nov 4, 2020
6de1180
make examples a little fancier
hawkw Nov 4, 2020
4ddaedf
fix trace in contrib
hawkw Nov 4, 2020
0025a58
propagate spans to spawned threads
hawkw Nov 4, 2020
f15d432
put all request processing inside request span
hawkw Nov 4, 2020
dd77f67
add another span in 'Fairings::pretty_print_counts'
jebrosen Nov 4, 2020
d5006de
feat: use rocket::trace::info in fairings, manual_routes examples
jebrosen Nov 4, 2020
4bf5f33
remove trailing whitespace
jebrosen Nov 5, 2020
50a224d
fix(codegen): log parameter names in a way that will work for any kin…
jebrosen Nov 12, 2020
55faca7
fix compile error in manual_routes example
jebrosen Nov 12, 2020
732a613
fix(test): missing semicolon
jebrosen Nov 12, 2020
8dba51d
feat: rework log output formatting
jebrosen Nov 13, 2020
39ae1d5
feat: print request IDs
jebrosen Nov 13, 2020
910ad1c
feat/experiment: remove connection span, making 'request' the top-lev…
jebrosen Nov 13, 2020
63e0ef3
handle `log` metadata properly
hawkw Nov 16, 2020
8ac9625
use tracing instead of log in http
jebrosen Nov 15, 2020
2f098a7
call Paint::disable in more cases of 'tracing' initialization failure
jebrosen Nov 17, 2020
2cfad83
add new log level 'support'
jebrosen Nov 18, 2020
d69340c
fixup: add string form to description of 'support' to make consistent…
jebrosen Nov 19, 2020
0d506c1
Use 'in_scope' instead of 'enter' in most places.
jebrosen Nov 19, 2020
c19799b
fix: under feature="log", skip formatting of fields whose names start…
jebrosen Nov 21, 2020
85f2e5b
fix: test the 'log' feature individually
jebrosen Nov 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ members = [
"examples/tls",
"examples/fairings",
"examples/hello_2018",
]
]
17 changes: 11 additions & 6 deletions contrib/codegen/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,20 @@ pub fn database_attr(attr: TokenStream, input: TokenStream) -> Result<TokenStrea
match pool {
Ok(Ok(p)) => Ok(rocket.manage(#pool_type(p))),
Err(config_error) => {
::rocket::logger::error(
&format!("Database configuration failure: '{}'", #name));
::rocket::logger::error_(&format!("{}", config_error));
let span = ::rocket::trace::error_span!(
"db_config_error",
database = %#name,
"Database configuration failure"
);
::rocket::trace::error!(parent: &span, %error);
Err(rocket)
},
Ok(Err(pool_error)) => {
::rocket::logger::error(
&format!("Failed to initialize pool for '{}'", #name));
::rocket::logger::error_(&format!("{:?}", pool_error));
let span = ::rocket::trace::error_span!(
"db_pool_error"
database = %#name,
"Failed to initialize pool");
::rocket::trace::error!(parent: &span, error = ?pool_error);
Err(rocket)
},
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ memcache_pool = ["databases", "memcache", "r2d2-memcache"]
tokio = { version = "0.2.0", optional = true }
rocket_contrib_codegen = { version = "0.5.0-dev", path = "../codegen", optional = true }
rocket = { version = "0.5.0-dev", path = "../../core/lib/", default-features = false }
log = "0.4"
tracing = { version = "0.1", default-features = false }

# Serialization and templating dependencies.
serde = { version = "1.0", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions contrib/lib/src/compression/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ impl Fairing for Compression {
if let Value::String(s) = ex {
let mt = MediaType::parse_flexible(s);
if mt.is_none() {
warn_!("Ignoring invalid media type '{:?}'", s);
warn!("Ignoring invalid media type '{:?}'", s);
}
mt
} else {
warn_!("Ignoring non-string media type '{:?}'", ex);
warn!("Ignoring non-string media type '{:?}'", ex);
None
}
}).collect();
}
None => {
warn_!(
warn!(
"Exclusions is not an array; using default compression exclusions '{:?}'",
ctxt.exclusions
);
Expand All @@ -134,7 +134,7 @@ impl Fairing for Compression {
Err(ConfigError::Missing(_)) => { /* ignore missing */ }
Err(e) => {
e.pretty_print();
warn_!(
warn!(
"Using default compression exclusions '{:?}'",
ctxt.exclusions
);
Expand Down
13 changes: 8 additions & 5 deletions contrib/lib/src/helmet/helmet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ impl SpaceHelmet {
for policy in self.policies.values() {
let name = policy.name();
if response.headers().contains(name.as_str()) {
warn!("Space Helmet: response contains a '{}' header.", name);
warn_!("Refusing to overwrite existing header.");
let span = warn_span!(
"Space Helmet: response contains already contains this header",
header = %name,
);
warn!(parent: &span, "Refusing to overwrite existing header.");
continue
}

Expand Down Expand Up @@ -206,9 +209,9 @@ impl Fairing for SpaceHelmet {
&& !cargo.config().environment.is_dev()
&& !self.is_enabled::<Hsts>()
{
warn_!("Space Helmet: deploying with TLS without enabling HSTS.");
warn_!("Enabling default HSTS policy.");
info_!("To disable this warning, configure an HSTS policy.");
let span = warn_span!("Space Helmet: deploying with TLS without enabling HSTS.");
warn!(parent: &span, "Enabling default HSTS policy.");
info!(parent: &span, "To disable this warning, configure an HSTS policy.");
self.force_hsts.store(true, Ordering::Relaxed);
}
}
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a, T: Deserialize<'a>> FromTransformedData<'a> for Json<T> {
match serde_json::from_str(&string) {
Ok(v) => Success(Json(v)),
Err(e) => {
error_!("Couldn't parse JSON body: {:?}", e);
error!("Couldn't parse JSON body: {:?}", e);
if e.is_data() {
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
} else {
Expand All @@ -166,7 +166,7 @@ impl<'r, T: Serialize> Responder<'r, 'static> for Json<T> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
let string = serde_json::to_string(&self.0)
.map_err(|e| {
error_!("JSON failed to serialize: {:?}", e);
error!("JSON failed to serialize: {:?}", e);
Status::InternalServerError
})?;

Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! This crate is expected to grow with time, bringing in outside crates to be
//! officially supported by Rocket.

#[allow(unused_imports)] #[macro_use] extern crate log;
#[allow(unused_imports)] #[macro_use] extern crate tracing;
#[allow(unused_imports)] #[macro_use] extern crate rocket;

#[cfg(feature="json")] #[macro_use] pub mod json;
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<'a, T: Deserialize<'a>> FromTransformedData<'a> for MsgPack<T> {
match rmp_serde::from_slice(&buf) {
Ok(val) => Success(MsgPack(val)),
Err(e) => {
error_!("Couldn't parse MessagePack body: {:?}", e);
error!("Couldn't parse MessagePack body: {:?}", e);
match e {
TypeMismatch(_) | OutOfRange | LengthMismatch(_) => {
Failure((Status::UnprocessableEntity, e))
Expand All @@ -158,7 +158,7 @@ impl<'r, T: Serialize> Responder<'r, 'static> for MsgPack<T> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
let buf = rmp_serde::to_vec(&self.0)
.map_err(|e| {
error_!("MsgPack failed to serialize: {:?}", e);
error!("MsgPack failed to serialize: {:?}", e);
Status::InternalServerError
})?;

Expand Down
8 changes: 4 additions & 4 deletions contrib/lib/src/templates/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ impl Context {
for path in glob::glob(glob_path).unwrap().filter_map(Result::ok) {
let (name, data_type_str) = split_path(&root, &path);
if let Some(info) = templates.get(&*name) {
warn_!("Template name '{}' does not have a unique path.", name);
info_!("Existing path: {:?}", info.path);
info_!("Additional path: {:?}", path);
warn_!("Using existing path for template '{}'.", name);
let span = warn_span!("invalid_template_path", "Template name '{}' does not have a unique path.", name);
info!(parent: &span, "Existing path: {:?}", info.path);
info!(parent: &span, "Additional path: {:?}", path);
warn!(parent: &span, "Using existing path for template '{}'.", name);
continue;
}

Expand Down
18 changes: 10 additions & 8 deletions contrib/lib/src/templates/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ mod context {

let watcher = match watcher {
Ok(watcher) => Some(Mutex::new((watcher, rx))),
Err(e) => {
warn!("Failed to enable live template reloading: {}", e);
debug_!("Reload error: {:?}", e);
warn_!("Live template reloading is unavailable.");
Err(error) => {
let span = warn_span!("Failed to enable live template reloading", %error);
debug!(parent: &span, reload_error = ?error);
warn!(parent: &span, "Live template reloading is unavailable.");
None
}
};
Expand Down Expand Up @@ -98,14 +98,16 @@ mod context {
}

if changed {
info_!("Change detected: reloading templates.");
let span = info_span!("Change detected: reloading templates.");
let _entered = span.enter();
let mut ctxt = self.context_mut();
if let Some(mut new_ctxt) = Context::initialize(ctxt.root.clone()) {
custom_callback(&mut new_ctxt.engines);
*ctxt = new_ctxt;
info!("reloaded!");
} else {
warn_!("An error occurred while reloading templates.");
warn_!("The previous templates will remain active.");
warn!("An error occurred while reloading templates.");
warn!("The previous templates will remain active.");
};
}
});
Expand Down Expand Up @@ -159,7 +161,7 @@ impl Fairing for TemplateFairing {
Err(ConfigError::Missing(_)) => { /* ignore missing */ }
Err(e) => {
e.pretty_print();
warn_!("Using default templates directory '{:?}'", template_root);
warn!("Using default templates directory '{:?}'", template_root);
}
};

Expand Down
12 changes: 6 additions & 6 deletions contrib/lib/src/templates/handlebars_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ impl Engine for Handlebars<'static> {
for &(name, info) in templates {
let path = &info.path;
if let Err(e) = hb.register_template_file(name, path) {
error!("Error in Handlebars template '{}'.", name);
info_!("{}", e);
info_!("Template path: '{}'.", path.to_string_lossy());
let span = error_span!("Error in Handlebars template", template = %name);
info!(parent: &span, template.error = %e);
info!(parent: &span, template.path = path.to_string_lossy());
return None;
}
}
Expand All @@ -24,14 +24,14 @@ impl Engine for Handlebars<'static> {

fn render<C: Serialize>(&self, name: &str, context: C) -> Option<String> {
if self.get_template(name).is_none() {
error_!("Handlebars template '{}' does not exist.", name);
error!(template = %name, "Handlebars template does not exist.");
return None;
}

match Handlebars::render(self, name, &context) {
Ok(string) => Some(string),
Err(e) => {
error_!("Error rendering Handlebars template '{}': {}", name, e);
Err(error) => {
error!(template = %name, %error, "Error rendering Handlebars template");
None
}
}
Expand Down
6 changes: 3 additions & 3 deletions contrib/lib/src/templates/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ impl<'a, 'r> FromRequest<'a, 'r> for Metadata<'a> {
.succeeded()
.and_then(|cm| Some(request::Outcome::Success(Metadata(cm.inner()))))
.unwrap_or_else(|| {
error_!("Uninitialized template context: missing fairing.");
info_!("To use templates, you must attach `Template::fairing()`.");
info_!("See the `Template` documentation for more information.");
let span = error_span!("Uninitialized template context: missing fairing.");
info!(parent: &span, "To use templates, you must attach `Template::fairing()`.");
info!(parent: &span, "See the `Template` documentation for more information.");
request::Outcome::Failure((Status::InternalServerError, ()))
})
}
Expand Down
22 changes: 11 additions & 11 deletions contrib/lib/src/templates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ impl Template {
where S: Into<Cow<'static, str>>, C: Serialize
{
let ctxt = cargo.state::<ContextManager>().map(ContextManager::context).or_else(|| {
warn!("Uninitialized template context: missing fairing.");
info!("To use templates, you must attach `Template::fairing()`.");
info!("See the `Template` documentation for more information.");
let span = warn_span!("Uninitialized template context: missing fairing.");
info!(parent: &span, "To use templates, you must attach `Template::fairing()`.");
info!(parent: &span, "See the `Template` documentation for more information.");
None
})?;

Expand All @@ -362,19 +362,19 @@ impl Template {
let name = &*self.name;
let info = ctxt.templates.get(name).ok_or_else(|| {
let ts: Vec<_> = ctxt.templates.keys().map(|s| s.as_str()).collect();
error_!("Template '{}' does not exist.", name);
info_!("Known templates: {}", ts.join(","));
info_!("Searched in '{:?}'.", ctxt.root);
let span = error_span!("Template does not exist.", template = %name);
info!(parent: &span, "Known templates: {}", ts.join(","));
info!(parent: &span, "Searched in '{:?}'.", ctxt.root);
Status::InternalServerError
})?;

let value = self.value.ok_or_else(|| {
error_!("The provided template context failed to serialize.");
error!("The provided template context failed to serialize.");
Status::InternalServerError
})?;

let string = ctxt.engines.render(name, &info, value).ok_or_else(|| {
error_!("Template '{}' failed to render.", name);
error!(template = %name, "Template failed to render.");
Status::InternalServerError
})?;

Expand All @@ -389,9 +389,9 @@ impl<'r> Responder<'r, 'static> for Template {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
let (render, content_type) = {
let ctxt = req.managed_state::<ContextManager>().ok_or_else(|| {
error_!("Uninitialized template context: missing fairing.");
info_!("To use templates, you must attach `Template::fairing()`.");
info_!("See the `Template` documentation for more information.");
let span = error_span!("Uninitialized template context: missing fairing.");
info!(parent: &span, "To use templates, you must attach `Template::fairing()`.");
info!(parent: &span, "See the `Template` documentation for more information.");
Status::InternalServerError
})?.context();

Expand Down
19 changes: 11 additions & 8 deletions contrib/lib/src/templates/tera_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ impl Engine for Tera {

// Finally try to tell Tera about all of the templates.
if let Err(e) = tera.add_template_files(tera_templates) {
error!("Failed to initialize Tera templating.");
let span = error_span!("Failed to initialize Tera templating.");
let _entered = span.enter();

let mut error = Some(&e as &dyn Error);
while let Some(err) = error {
info_!("{}", err);
error!(error = %err);
error = err.source();
}

Expand All @@ -36,17 +37,19 @@ impl Engine for Tera {
}

fn render<C: Serialize>(&self, name: &str, context: C) -> Option<String> {
let span = info_span!("Rendering Tera template", template = %name);
let _entered = span.enter();
if self.get_template(name).is_err() {
error_!("Tera template '{}' does not exist.", name);
error!("Tera template '{}' does not exist.", name);
return None;
};

let tera_ctx = match Context::from_serialize(context) {
Ok(ctx) => ctx,
Err(_) => {
error_!(
error!(
"Error generating context when rendering Tera template '{}'.",
name
name,
);
return None;
}
Expand All @@ -55,11 +58,11 @@ impl Engine for Tera {
match Tera::render(self, name, &tera_ctx) {
Ok(string) => Some(string),
Err(e) => {
error_!("Error rendering Tera template '{}'.", name);

let span = error_span!("Error rendering Tera template", template = %name);
let _entered = span.enter();
let mut error = Some(&e as &dyn Error);
while let Some(err) = error {
error_!("{}", err);
error!(error = %err);
error = err.source();
}

Expand Down
6 changes: 4 additions & 2 deletions core/codegen/src/attribute/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ pub fn _catch(
let user_catcher_fn_name = catch.function.sig.ident.clone();
let generated_struct_name = user_catcher_fn_name.prepend(CATCH_STRUCT_PREFIX);
let generated_fn_name = user_catcher_fn_name.prepend(CATCH_FN_PREFIX);
let generated_span_name = user_catcher_fn_name.to_string();
let (vis, catcher_status) = (&catch.function.vis, &catch.status);
let status_code = Optional(catcher_status.as_ref().map(|s| s.0.code));

// Variables names we'll use and reuse.
define_vars_and_mods!(catch.function.span().into() =>
req, status, _Box, Request, Response, ErrorHandlerFuture, Status);
req, status, log, _Box, Request, Response, ErrorHandlerFuture, Status);

// Determine the number of parameters that will be passed in.
if catch.function.sig.inputs.len() > 2 {
Expand Down Expand Up @@ -125,13 +126,14 @@ pub fn _catch(
#status: #Status,
#req: &'_b #Request
) -> #ErrorHandlerFuture<'_b> {
use #log::Instrument as _;
#_Box::pin(async move {
let __response = #catcher_response;
#Response::build()
.status(#status)
.merge(__response)
.ok()
})
}.instrument(#log::info_span!(#generated_span_name, status = %#status)))
}

/// Rocket code generated static catcher info.
Expand Down
Loading