Skip to content

Commit

Permalink
Make more things work with non-'static metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jun 14, 2022
1 parent d85eb31 commit cdfbf8e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 55 deletions.
10 changes: 5 additions & 5 deletions tracing-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use crate::{field, Metadata};
#[derive(Debug)]
pub struct Event<'a> {
fields: &'a field::ValueSet<'a>,
metadata: &'static Metadata<'static>,
metadata: &'a Metadata<'a>,
parent: Parent,
}

impl<'a> Event<'a> {
/// Constructs a new `Event` with the specified metadata and set of values,
/// and observes it with the current collector.
pub fn dispatch(metadata: &'static Metadata<'static>, fields: &'a field::ValueSet<'_>) {
pub fn dispatch(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet<'_>) {
let event = Event::new(metadata, fields);
crate::dispatch::get_default(|current| {
current.event(&event);
Expand All @@ -39,7 +39,7 @@ impl<'a> Event<'a> {
/// Returns a new `Event` in the current span, with the specified metadata
/// and set of values.
#[inline]
pub fn new(metadata: &'static Metadata<'static>, fields: &'a field::ValueSet<'a>) -> Self {
pub fn new(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet<'a>) -> Self {
Event {
fields,
metadata,
Expand All @@ -52,7 +52,7 @@ impl<'a> Event<'a> {
#[inline]
pub fn new_child_of(
parent: impl Into<Option<Id>>,
metadata: &'static Metadata<'static>,
metadata: &'a Metadata<'a>,
fields: &'a field::ValueSet<'a>,
) -> Self {
let parent = match parent.into() {
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<'a> Event<'a> {
/// Returns [metadata] describing this `Event`.
///
/// [metadata]: super::Metadata
pub fn metadata(&self) -> &'static Metadata<'static> {
pub fn metadata(&self) -> &'a Metadata<'a> {
self.metadata
}

Expand Down
2 changes: 1 addition & 1 deletion tracing-error/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl SpanTrace {
///
/// [fields]: tracing::field
/// [`Metadata`]: tracing::Metadata
pub fn with_spans(&self, f: impl FnMut(&'static Metadata<'static>, &str) -> bool) {
pub fn with_spans(&self, f: impl for<'a> FnMut(&'a Metadata<'a>, &str) -> bool) {
self.span.with_collector(|(id, s)| {
if let Some(getcx) = s.downcast_ref::<WithContext>() {
getcx.with_context(s, id, f);
Expand Down
6 changes: 3 additions & 3 deletions tracing-error/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct ErrorSubscriber<C, F = DefaultFields> {
// so that we can downcast to something aware of them without knowing those
// types at the callsite.
pub(crate) struct WithContext(
fn(&Dispatch, &span::Id, f: &mut dyn FnMut(&'static Metadata<'static>, &str) -> bool),
fn(&Dispatch, &span::Id, f: &mut dyn for<'a> FnMut(&'a Metadata<'a>, &str) -> bool),
);

impl<C, F> Subscribe<C> for ErrorSubscriber<C, F>
Expand Down Expand Up @@ -89,7 +89,7 @@ where
fn get_context(
dispatch: &Dispatch,
id: &span::Id,
f: &mut dyn FnMut(&'static Metadata<'static>, &str) -> bool,
f: &mut dyn for<'a> FnMut(&'a Metadata<'a>, &str) -> bool,
) {
let collector = dispatch
.downcast_ref::<C>()
Expand All @@ -115,7 +115,7 @@ impl WithContext {
&self,
dispatch: &'a Dispatch,
id: &span::Id,
mut f: impl FnMut(&'static Metadata<'static>, &str) -> bool,
mut f: impl for<'m> FnMut(&'m Metadata<'m>, &str) -> bool,
) {
(self.0)(dispatch, id, &mut f)
}
Expand Down
20 changes: 7 additions & 13 deletions tracing-opentelemetry/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,17 +631,7 @@ where
#[cfg(not(feature = "tracing-log"))]
let meta = event.metadata();

let target = Key::new("target");

#[cfg(feature = "tracing-log")]
let target = if normalized_meta.is_some() {
target.string(meta.target().to_owned())
} else {
target.string(event.metadata().target())
};

#[cfg(not(feature = "tracing-log"))]
let target = target.string(meta.target());
let target = Key::new("target").string(meta.target().to_owned());

let mut otel_event = otel::Event::new(
String::new(),
Expand All @@ -666,8 +656,12 @@ where
meta.module_path().map(|s| Value::from(s.to_owned())),
),
None => (
event.metadata().file().map(Value::from),
event.metadata().module_path().map(Value::from),
event.metadata().file().map(String::from).map(Value::from),
event
.metadata()
.module_path()
.map(String::from)
.map(Value::from),
),
};

Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ where
/// If this returns `None`, then no span exists for that ID (either it has
/// closed or the ID is invalid).
#[inline]
pub fn metadata(&self, id: &Id) -> Option<&'static Metadata<'static>>
pub fn metadata(&self, id: &Id) -> Option<&'a Metadata<'a>>
where
C: for<'lookup> LookupSpan<'lookup>,
{
Expand Down
17 changes: 10 additions & 7 deletions tracing-subscriber/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub trait SpanData<'a> {
fn id(&self) -> Id;

/// Returns a reference to the span's `Metadata`.
fn metadata(&self) -> &'static Metadata<'static>;
fn metadata(&self) -> &'a Metadata<'a>;

/// Returns a reference to the ID
fn parent(&self) -> Option<&Id>;
Expand Down Expand Up @@ -349,12 +349,12 @@ where
}

/// Returns a static reference to the span's metadata.
pub fn metadata(&self) -> &'static Metadata<'static> {
pub fn metadata(&self) -> &'a Metadata<'a> {
self.data.metadata()
}

/// Returns the span's name,
pub fn name(&self) -> &'static str {
pub fn name(&self) -> &'a str {
self.data.metadata().name()
}

Expand Down Expand Up @@ -571,7 +571,7 @@ mod tests {

#[derive(Default)]
struct RecordingSubscriber {
last_entered_scope: Arc<Mutex<Vec<&'static str>>>,
last_entered_scope: Arc<Mutex<Vec<String>>>,
}

impl<S> Subscribe<S> for RecordingSubscriber
Expand All @@ -580,7 +580,10 @@ mod tests {
{
fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) {
let span = ctx.span(id).unwrap();
let scope = span.scope().map(|span| span.name()).collect::<Vec<_>>();
let scope = span
.scope()
.map(|span| span.name().into())
.collect::<Vec<_>>();
*self.last_entered_scope.lock().unwrap() = scope;
}
}
Expand All @@ -606,7 +609,7 @@ mod tests {

#[derive(Default)]
struct RecordingSubscriber {
last_entered_scope: Arc<Mutex<Vec<&'static str>>>,
last_entered_scope: Arc<Mutex<Vec<String>>>,
}

impl<S> Subscribe<S> for RecordingSubscriber
Expand All @@ -618,7 +621,7 @@ mod tests {
let scope = span
.scope()
.from_root()
.map(|span| span.name())
.map(|span| span.name().into())
.collect::<Vec<_>>();
*self.last_entered_scope.lock().unwrap() = scope;
}
Expand Down
29 changes: 12 additions & 17 deletions tracing-subscriber/src/registry/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> SpanData<'a> for Data<'a> {
idx_to_id(self.inner.key())
}

fn metadata(&self) -> &'static Metadata<'static> {
fn metadata(&self) -> &'a Metadata<'a> {
(*self).inner.metadata
}

Expand Down Expand Up @@ -581,8 +581,8 @@ mod tests {

#[derive(Default)]
struct CloseState {
open: HashMap<&'static str, Weak<()>>,
closed: Vec<(&'static str, Weak<()>)>,
open: HashMap<String, Weak<()>>,
closed: Vec<(String, Weak<()>)>,
}

struct SetRemoved(Arc<()>);
Expand All @@ -597,7 +597,7 @@ mod tests {
let is_removed = Arc::new(());
assert!(
lock.open
.insert(span.name(), Arc::downgrade(&is_removed))
.insert(span.name().into(), Arc::downgrade(&is_removed))
.is_none(),
"test subscriber saw multiple spans with the same name, the test is probably messed up"
);
Expand All @@ -620,7 +620,7 @@ mod tests {
if let Ok(mut lock) = self.inner.lock() {
if let Some(is_removed) = lock.open.remove(name) {
assert!(is_removed.upgrade().is_some());
lock.closed.push((name, is_removed));
lock.closed.push((name.into(), is_removed));
}
}
}
Expand All @@ -644,7 +644,7 @@ mod tests {
}

fn is_closed(&self, span: &str) -> bool {
self.closed.iter().any(|(name, _)| name == &span)
self.closed.iter().any(|(name, _)| name == span)
}
}

Expand Down Expand Up @@ -679,7 +679,7 @@ mod tests {

fn assert_removed(&self, span: &str) {
let lock = self.state.lock().unwrap();
let is_removed = match lock.closed.iter().find(|(name, _)| name == &span) {
let is_removed = match lock.closed.iter().find(|(name, _)| name == span) {
Some((_, is_removed)) => is_removed,
None => panic!(
"expected {} to be removed from the registry, but it was not closed {}",
Expand All @@ -700,7 +700,7 @@ mod tests {

fn assert_not_removed(&self, span: &str) {
let lock = self.state.lock().unwrap();
let is_removed = match lock.closed.iter().find(|(name, _)| name == &span) {
let is_removed = match lock.closed.iter().find(|(name, _)| name == span) {
Some((_, is_removed)) => is_removed,
None if lock.is_open(span) => return,
None => unreachable!(),
Expand All @@ -715,21 +715,16 @@ mod tests {
#[allow(unused)] // may want this for future tests
fn assert_last_closed(&self, span: Option<&str>) {
let lock = self.state.lock().unwrap();
let last = lock.closed.last().map(|(span, _)| span);
assert_eq!(
last,
span.as_ref(),
"expected {:?} to have closed last",
span
);
let last = lock.closed.last().map(|(span, _)| &**span);
assert_eq!(last, span, "expected {:?} to have closed last", span);
}

fn assert_closed_in_order(&self, order: impl AsRef<[&'static str]>) {
let lock = self.state.lock().unwrap();
let order = order.as_ref();
for (i, name) in order.iter().enumerate() {
for (i, &name) in order.iter().enumerate() {
assert_eq!(
lock.closed.get(i).map(|(span, _)| span),
lock.closed.get(i).map(|(span, _)| &**span),
Some(name),
"expected close order: {:?}, actual: {:?}",
order,
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/src/subscribe/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ where
/// If this returns `None`, then no span exists for that ID (either it has
/// closed or the ID is invalid).
#[inline]
pub fn metadata(&self, id: &span::Id) -> Option<&'static Metadata<'static>>
pub fn metadata(&self, id: &span::Id) -> Option<&'a Metadata<'a>>
where
C: for<'lookup> LookupSpan<'lookup>,
{
Expand All @@ -208,7 +208,7 @@ where
///
/// [stored data]: ../registry/struct.SpanRef.html
#[inline]
pub fn span(&self, id: &span::Id) -> Option<registry::SpanRef<'_, C>>
pub fn span(&self, id: &span::Id) -> Option<registry::SpanRef<'a, C>>
where
C: for<'lookup> LookupSpan<'lookup>,
{
Expand Down
11 changes: 7 additions & 4 deletions tracing-subscriber/src/subscribe/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod registry_tests {
let last_event_span = Arc::new(Mutex::new(None));

struct RecordingSubscriber {
last_event_span: Arc<Mutex<Option<&'static str>>>,
last_event_span: Arc<Mutex<Option<String>>>,
}

impl<C> Subscribe<C> for RecordingSubscriber
Expand All @@ -144,7 +144,7 @@ mod registry_tests {
{
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
let span = ctx.event_span(event);
*self.last_event_span.lock().unwrap() = span.map(|s| s.name());
*self.last_event_span.lock().unwrap() = span.map(|s| s.name().into());
}
}

Expand All @@ -158,11 +158,14 @@ mod registry_tests {

let parent = tracing::info_span!("explicit");
tracing::info!(parent: &parent, "explicit span");
assert_eq!(*last_event_span.lock().unwrap(), Some("explicit"));
assert_eq!(last_event_span.lock().unwrap().as_deref(), Some("explicit"));

let _guard = tracing::info_span!("contextual").entered();
tracing::info!("contextual span");
assert_eq!(*last_event_span.lock().unwrap(), Some("contextual"));
assert_eq!(
last_event_span.lock().unwrap().as_deref(),
Some("contextual")
);
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ pub mod __macro_support {
}

#[inline(always)]
fn metadata(&self) -> &Metadata<'static> {
fn metadata(&self) -> &Metadata<'_> {
self.meta
}
}
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ impl Span {
}

/// Returns this span's `Metadata`, if it is enabled.
pub fn metadata(&self) -> Option<&'static Metadata<'static>> {
pub fn metadata(&self) -> Option<&Metadata<'_>> {
self.meta
}

Expand Down

0 comments on commit cdfbf8e

Please sign in to comment.