Skip to content

Commit d3ba026

Browse files
committed
wip: fix trace exports
1 parent fcc354c commit d3ba026

File tree

13 files changed

+71
-52
lines changed

13 files changed

+71
-52
lines changed

core/lib/src/config/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::config::{ShutdownConfig, Ident, CliColors};
1010
use crate::request::{self, Request, FromRequest};
1111
use crate::http::uncased::Uncased;
1212
use crate::data::Limits;
13-
use crate::trace::traceable::Traceable;
13+
use crate::trace::Traceable;
1414

1515
/// Rocket server configuration.
1616
///

core/lib/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77
use figment::Profile;
88

99
use crate::listener::Endpoint;
10-
use crate::trace::traceable::Traceable;
10+
use crate::trace::Traceable;
1111
use crate::{Ignite, Orbit, Phase, Rocket};
1212

1313
/// An error that occurs during launch.

core/lib/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ pub use figment;
122122
pub use time;
123123
pub use tracing;
124124

125-
#[doc(hidden)]
126125
#[macro_use]
127126
pub mod trace;
128127
#[macro_use]

core/lib/src/lifecycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use futures::future::{FutureExt, Future};
22

33
use crate::{route, catcher, Rocket, Orbit, Request, Response, Data};
4-
use crate::trace::traceable::Traceable;
4+
use crate::trace::Traceable;
55
use crate::util::Formatter;
66
use crate::data::IoHandler;
77
use crate::http::{Method, Status, Header};

core/lib/src/rocket.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use figment::{Figment, Provider};
1111
use futures::TryFutureExt;
1212

1313
use crate::shutdown::{Stages, Shutdown};
14-
use crate::trace::traceable::{Traceable, TraceableCollection};
14+
use crate::trace::{Traceable, TraceableCollection};
1515
use crate::{sentinel, shield::Shield, Catcher, Config, Route};
1616
use crate::listener::{Bind, DefaultListener, Endpoint, Listener};
1717
use crate::router::Router;

core/lib/src/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::error::log_server_error;
1717
use crate::data::{IoStream, RawStream};
1818
use crate::util::{spawn_inspect, FutureExt, ReaderStream};
1919
use crate::http::Status;
20-
use crate::trace::traceable::{Traceable, TraceableCollection};
20+
use crate::trace::{Traceable, TraceableCollection};
2121

2222
type Result<T, E = crate::Error> = std::result::Result<T, E>;
2323

core/lib/src/shield/shield.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{Rocket, Request, Response, Orbit, Config};
55
use crate::fairing::{Fairing, Info, Kind};
66
use crate::http::{Header, uncased::UncasedStr};
77
use crate::shield::*;
8-
use crate::trace::traceable::*;
8+
use crate::trace::*;
99

1010
/// A [`Fairing`] that injects browser security and privacy headers into all
1111
/// outgoing responses.
@@ -59,10 +59,8 @@ use crate::trace::traceable::*;
5959
///
6060
/// If TLS is configured and enabled when the application is launched in a
6161
/// non-debug profile, HSTS is automatically enabled with its default policy and
62-
/// a warning is logged.
63-
///
64-
/// To get rid of this warning, explicitly [`Shield::enable()`] an [`Hsts`]
65-
/// policy.
62+
/// a warning is logged. To get rid of this warning, explicitly
63+
/// [`Shield::enable()`] an [`Hsts`] policy.
6664
pub struct Shield {
6765
/// Enabled policies where the key is the header name.
6866
policies: HashMap<&'static UncasedStr, Header<'static>>,
@@ -193,32 +191,19 @@ impl Fairing for Shield {
193191
&& rocket.figment().profile() != Config::DEBUG_PROFILE
194192
&& !self.is_enabled::<Hsts>();
195193

194+
if force_hsts {
195+
self.force_hsts.store(true, Ordering::Release);
196+
}
197+
196198
info_span!("shield" [policies = self.policies.len()] => {
197199
self.policies.values().trace_all_info();
198200

199201
if force_hsts {
200-
warn!("Detected TLS-enabled liftoff without enabling HSTS.");
201-
warn!("Shield has enabled a default HSTS policy.");
202-
info!("To remove this warning, configure an HSTS policy.");
202+
warn!("Detected TLS-enabled liftoff without enabling HSTS.\n\
203+
Shield has enabled a default HSTS policy.\n\
204+
To remove this warning, configure an HSTS policy.");
203205
}
204206
})
205-
206-
// trace::collection_info!("shield", force_hsts => self.polices.values(), {
207-
// warn!("Detected TLS-enabled liftoff without enabling HSTS.");
208-
// warn!("Shield has enabled a default HSTS policy.");
209-
// info!("To remove this warning, configure an HSTS policy.");
210-
// });
211-
212-
// // tracing::info_span!("shield", force_hsts).in_scope(|| {
213-
// // self.polices.values().trace();
214-
// // for header in self.policies.values() {
215-
// // info!(name: "header", name = header.name().as_str(), value = header.value());
216-
// // }
217-
//
218-
// warn!("Detected TLS-enabled liftoff without enabling HSTS.");
219-
// warn!("Shield has enabled a default HSTS policy.");
220-
// info!("To remove this warning, configure an HSTS policy.");
221-
// });
222207
}
223208

224209
async fn on_response<'r>(&self, _: &'r Request<'_>, response: &mut Response<'r>) {

core/lib/src/trace/macros.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@ macro_rules! declare_macro {
44
);
55

66
([$d:tt] $name:ident $level:ident) => (
7+
#[doc(hidden)]
78
#[macro_export]
89
macro_rules! $name {
910
($d ($t:tt)*) => ($crate::tracing::$level!($d ($t)*));
1011
}
12+
13+
// pub use $name as $name;
1114
);
1215
}
1316

14-
declare_macro!(error error, info info, trace trace, debug debug, warn warn);
15-
1617
macro_rules! declare_span_macro {
1718
($($name:ident $level:ident),* $(,)?) => (
1819
$(declare_span_macro!([$] $name $level);)*
1920
);
2021

2122
([$d:tt] $name:ident $level:ident) => (
23+
#[doc(hidden)]
2224
#[macro_export]
2325
macro_rules! $name {
2426
($n:literal $d ([ $d ($f:tt)* ])? => $in_scope:expr) => ({
2527
$crate::tracing::span!($crate::tracing::Level::$level, $n $d (, $d ($f)* )?)
2628
.in_scope(|| $in_scope);
2729
})
2830
}
31+
32+
#[doc(inline)]
33+
pub use $name as $name;
2934
);
3035
}
3136

32-
declare_span_macro!(error_span ERROR, warn_span WARN,
33-
info_span INFO, trace_span TRACE, debug_span DEBUG);
34-
3537
macro_rules! span {
3638
($level:expr, $($args:tt)*) => {{
3739
match $level {
@@ -49,6 +51,8 @@ macro_rules! span {
4951
}};
5052
}
5153

54+
#[doc(hidden)]
55+
#[macro_export]
5256
macro_rules! event {
5357
($level:expr, $($args:tt)*) => {{
5458
match $level {
@@ -64,3 +68,22 @@ macro_rules! event {
6468
$crate::tracing::event!(name: $n, target: concat!("rocket::", $n), $level, $($args)*);
6569
}};
6670
}
71+
72+
#[doc(inline)]
73+
pub use event as event;
74+
75+
declare_macro!(
76+
error error,
77+
info info,
78+
trace trace,
79+
debug debug,
80+
warn warn
81+
);
82+
83+
declare_span_macro!(
84+
error_span ERROR,
85+
warn_span WARN,
86+
info_span INFO,
87+
trace_span TRACE,
88+
debug_span DEBUG,
89+
);

core/lib/src/trace/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#[macro_use]
2-
pub mod macros;
2+
mod macros;
3+
mod traceable;
4+
35
#[cfg(feature = "trace")]
6+
#[cfg_attr(nightly, doc(cfg(feature = "trace")))]
47
pub mod subscriber;
5-
pub mod level;
6-
pub mod traceable;
78

8-
pub use traceable::Traceable;
9+
pub(crate) mod level;
10+
11+
#[doc(inline)]
12+
pub use traceable::{Traceable, TraceableCollection};
13+
14+
#[doc(inline)]
15+
pub use macros::*;
916

1017
pub fn init<'a, T: Into<Option<&'a crate::Config>>>(_config: T) {
1118
#[cfg(all(feature = "trace", debug_assertions))]

core/lib/src/trace/subscriber/common.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ impl<K: private::FmtKind> RocketFmt<K> {
103103
}
104104
}
105105

106-
pub fn has_message(&self, meta: &Metadata<'_>) -> bool {
106+
pub(crate) fn has_message(&self, meta: &Metadata<'_>) -> bool {
107107
meta.fields().field("message").is_some()
108108
}
109109

110-
pub fn has_data_fields(&self, meta: &Metadata<'_>) -> bool {
110+
pub(crate) fn has_data_fields(&self, meta: &Metadata<'_>) -> bool {
111111
meta.fields().iter().any(|f| f.name() != "message")
112112
}
113113

114-
pub fn message<'a, F: RecordFields + 'a>(&self,
114+
pub(crate) fn message<'a, F: RecordFields + 'a>(&self,
115115
init_prefix: &'a dyn fmt::Display,
116116
cont_prefix: &'a dyn fmt::Display,
117117
meta: &'a Metadata<'_>,
@@ -142,9 +142,11 @@ impl<K: private::FmtKind> RocketFmt<K> {
142142
})
143143
}
144144

145-
pub fn compact_fields<'a, F>(&self, meta: &'a Metadata<'_>, data: F) -> impl fmt::Display + 'a
146-
where F: RecordFields + 'a
147-
{
145+
pub(crate) fn compact_fields<'a, F: RecordFields + 'a>(
146+
&self,
147+
meta: &'a Metadata<'_>,
148+
data: F
149+
) -> impl fmt::Display + 'a {
148150
let key_style = self.style(meta).bold();
149151
let val_style = self.style(meta).primary();
150152

@@ -163,7 +165,7 @@ impl<K: private::FmtKind> RocketFmt<K> {
163165
})
164166
}
165167

166-
pub fn print<F: RecordFields>(
168+
pub(crate) fn print<F: RecordFields>(
167169
&self,
168170
prefix: &dyn fmt::Display,
169171
cont_prefix: &dyn fmt::Display,

core/lib/src/trace/subscriber/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ mod compact;
44
mod common;
55
mod request_id;
66

7-
pub use visit::{RecordDisplay, Data};
87
pub use pretty::Pretty;
98
pub use compact::Compact;
109
pub use common::RocketFmt;
1110
pub use request_id::{RequestId, RequestIdLayer};
1211

1312
pub(crate) use common::Handle;
13+
pub(crate) use visit::{RecordDisplay, Data};

core/lib/src/trace/subscriber/request_id.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ impl RequestId {
6161

6262
impl RequestIdLayer {
6363
thread_local! {
64-
pub static CURRENT_REQUEST_ID: Cell<Option<RequestId>> = Cell::new(None);
64+
static CURRENT_REQUEST_ID: Cell<Option<RequestId>> = Cell::new(None);
6565
}
6666

67-
fn current() -> Option<RequestId> {
67+
pub fn current() -> Option<RequestId> {
6868
Self::CURRENT_REQUEST_ID.get()
6969
}
7070
}

core/lib/src/trace/traceable.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,11 @@ impl Traceable for Error {
255255

256256
impl Traceable for Sentry {
257257
fn trace(&self, level: Level) {
258-
let (file, line, column) = self.location;
259-
event!(level, "sentry", "type" = self.type_name, file, line, column);
258+
let (file, line, col) = self.location;
259+
event!(level, "sentry",
260+
type_name = self.type_name,
261+
location = %Formatter(|f| write!(f, "{file}:{line}:{col}"))
262+
);
260263
}
261264
}
262265

0 commit comments

Comments
 (0)