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

[tracing-core] Remove `static requirement from Event #1048

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5868a6d
[WIP] Metadata name is Cow<'static, str>
dvdplm Sep 28, 2020
f40de7b
Fetch patch from git
dvdplm Sep 29, 2020
ed04e30
Clean up MockSpan
dvdplm Sep 29, 2020
645c133
Don't clone the name in Metadata::name
dvdplm Sep 30, 2020
5a69f07
Use Cow in assert_last_closed
dvdplm Sep 30, 2020
f922cc1
If Metadata is always used with a 'static lifetime it might make sens…
dvdplm Sep 30, 2020
fa4a808
cleanup
dvdplm Sep 30, 2020
183f75f
Merge branch 'master' into dp-use-cow
dvdplm Oct 5, 2020
97e5b31
Metadata.target is Cow – breaks AsLog impl
dvdplm Oct 6, 2020
028ae65
Make it build (ty Ben)
dvdplm Oct 6, 2020
a3661f8
Make tests pass
dvdplm Oct 6, 2020
f0a6443
Undo changes to TestTracer and patched opentelemetry, not needed
dvdplm Oct 6, 2020
a9a0568
Remove patch
dvdplm Oct 6, 2020
2ee5da9
cleanup
dvdplm Oct 6, 2020
d8afcbe
cleanup
dvdplm Oct 6, 2020
381d574
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 7, 2020
8913759
Store file as Cow
dvdplm Oct 7, 2020
b250109
Store module_path as Cow
dvdplm Oct 7, 2020
e29d3ca
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 16, 2020
79ffec0
WIP Event.metadata is Cow
dvdplm Oct 16, 2020
33ccbf5
Cleanup
dvdplm Oct 18, 2020
649fb74
Cleanup
dvdplm Oct 18, 2020
856a1d8
Event takes &'a Metadata<'b>
dvdplm Oct 18, 2020
9e3f4fe
No need to be Clone
dvdplm Oct 18, 2020
f7274dc
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 19, 2020
b3bec86
Feature gate usage of Cow in Metadata
dvdplm Oct 19, 2020
fda189b
Add constructor for dynamic data
dvdplm Oct 19, 2020
c46c352
No need for extra lifetime
dvdplm Oct 19, 2020
7b0dfe5
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 19, 2020
280f0bf
Use impl Into<Cow<'a, str>
dvdplm Oct 20, 2020
4225d96
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 22, 2020
e4c99b3
Merge branch 'dp-target-is-cow' into dp-event-non-static-metadata
dvdplm Oct 22, 2020
f42a644
Merge remote-tracking branch 'upstream/master' into dp-target-is-cow
dvdplm Oct 23, 2020
421e6d1
Merge branch 'dp-target-is-cow' into dp-event-non-static-metadata
dvdplm Oct 23, 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 examples/examples/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Collect for CounterCollector {
values.record(&mut self.visitor())
}

fn event(&self, event: &Event<'_>) {
fn event(&self, event: &Event<'_, '_>) {
event.record(&mut self.visitor())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/serde-yak-shave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Collect for JsonSubscriber {
println!("{}", json);
}

fn event(&self, event: &Event<'_>) {
fn event(&self, event: &Event<'_, '_>) {
let json = json!({
"event": event.as_serde(),
});
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/sloggish/sloggish_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Collect for SloggishSubscriber {
}
}

fn event(&self, event: &tracing::Event<'_>) {
fn event(&self, event: &tracing::Event<'_, '_>) {
let mut stderr = self.stderr.lock();
let indent = self.stack.lock().unwrap().len();
self.print_indent(&mut stderr, indent).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tracing-core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub trait Collect: 'static {
/// [visitor]: super::field::Visit
/// [`record` method]: super::event::Event::record
/// [`dispatch` method]: super::event::Event::dispatch
fn event(&self, event: &Event<'_>);
fn event<'a>(&'a self, event: &'a Event<'a, '_>);

/// Records that a span has been entered.
///
Expand Down
10 changes: 5 additions & 5 deletions tracing-core/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ impl Dispatch {
/// [`Collect`]: super::collect::Collect
/// [`event`]: super::collect::Collect::event
#[inline]
pub fn event(&self, event: &Event<'_>) {
pub fn event<'a>(&'a self, event: &'a Event<'a, '_>) {
self.collector().event(event)
}

Expand Down Expand Up @@ -829,7 +829,7 @@ impl Collect for NoCollector {
span::Id::from_u64(0xDEAD)
}

fn event(&self, _event: &Event<'_>) {}
fn event(&self, _event: &Event<'_, '_>) {}

fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {}

Expand Down Expand Up @@ -993,7 +993,7 @@ mod test {

fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

fn event(&self, _: &Event<'_>) {
fn event(&self, _: &Event<'_, '_>) {
static EVENTS: AtomicUsize = AtomicUsize::new(0);
assert_eq!(
EVENTS.fetch_add(1, Ordering::Relaxed),
Expand Down Expand Up @@ -1049,7 +1049,7 @@ mod test {

fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

fn event(&self, _: &Event<'_>) {}
fn event(&self, _: &Event<'_, '_>) {}

fn enter(&self, _: &span::Id) {}

Expand Down Expand Up @@ -1082,7 +1082,7 @@ mod test {

fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}

fn event(&self, _: &Event<'_>) {}
fn event(&self, _: &Event<'_, '_>) {}

fn enter(&self, _: &span::Id) {}

Expand Down
18 changes: 9 additions & 9 deletions tracing-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ use crate::{field, Metadata};
/// [span]: ../span
/// [fields]: ../field
#[derive(Debug)]
pub struct Event<'a> {
pub struct Event<'a, 'b> {
fields: &'a field::ValueSet<'a>,
metadata: &'static Metadata<'static>,
metadata: &'a Metadata<'b>,
parent: Parent,
}

impl<'a> Event<'a> {
impl<'a, 'b: 'a> Event<'a, 'b> {
/// 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<'_>, 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<'b>, fields: &'a field::ValueSet<'a>) -> Self {
Event {
metadata,
fields,
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<'b>,
fields: &'a field::ValueSet<'a>,
) -> Self {
let parent = match parent.into() {
Expand All @@ -70,7 +70,7 @@ impl<'a> Event<'a> {
/// and observes it with the current collector and an explicit parent.
pub fn child_of(
parent: impl Into<Option<Id>>,
metadata: &'static Metadata<'static>,
metadata: &'a Metadata<'b>,
fields: &'a field::ValueSet<'_>,
) {
let event = Self::new_child_of(parent, metadata, fields);
Expand All @@ -95,8 +95,8 @@ impl<'a> Event<'a> {
/// Returns [metadata] describing this `Event`.
///
/// [metadata]: super::Metadata
pub fn metadata(&self) -> &'static Metadata<'static> {
self.metadata
pub fn metadata(&'a self) -> &'a Metadata<'_> {
&self.metadata
}

/// Returns true if the new event should be a root.
Expand Down
116 changes: 106 additions & 10 deletions tracing-core/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Metadata describing trace data.
use super::{callsite, field};
#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
use core::{
cmp, fmt,
str::FromStr,
Expand Down Expand Up @@ -60,21 +62,33 @@ use core::{
/// [callsite identifier]: super::callsite::Identifier
pub struct Metadata<'a> {
/// The name of the span described by this metadata.
name: &'static str,
#[cfg(feature = "alloc")]
name: Cow<'a, str>,
#[cfg(not(feature = "alloc"))]
name: &'a str,

/// The part of the system that the span that this metadata describes
/// occurred in.
#[cfg(feature = "alloc")]
target: Cow<'a, str>,
#[cfg(not(feature = "alloc"))]
target: &'a str,

/// The level of verbosity of the described span.
level: Level,

/// The name of the Rust module where the span occurred, or `None` if this
/// could not be determined.
#[cfg(feature = "alloc")]
module_path: Option<Cow<'a, str>>,
#[cfg(not(feature = "alloc"))]
module_path: Option<&'a str>,

/// The name of the source code file where the span occurred, or `None` if
/// this could not be determined.
#[cfg(feature = "alloc")]
file: Option<Cow<'a, str>>,
#[cfg(not(feature = "alloc"))]
file: Option<&'a str>,

/// The line number in the source code file where the span occurred, or
Expand Down Expand Up @@ -122,7 +136,7 @@ impl<'a> Metadata<'a> {
/// Construct new metadata for a span or event, with a name, target, level, field
/// names, and optional source code location.
pub const fn new(
name: &'static str,
name: &'a str,
target: &'a str,
level: Level,
file: Option<&'a str>,
Expand All @@ -131,8 +145,31 @@ impl<'a> Metadata<'a> {
fields: field::FieldSet,
kind: Kind,
) -> Self {
#[cfg(feature = "alloc")]
let file = {
if let Some(file) = file {
Some(Cow::Borrowed(file))
} else {
None
}
};
#[cfg(feature = "alloc")]
let module_path = {
if let Some(module_path) = module_path {
Some(Cow::Borrowed(module_path))
} else {
None
}
};

Metadata {
#[cfg(feature = "alloc")]
name: Cow::Borrowed(name),
#[cfg(not(feature = "alloc"))]
name,
#[cfg(feature = "alloc")]
target: Cow::Borrowed(target),
#[cfg(not(feature = "alloc"))]
target,
level,
module_path,
Expand All @@ -143,6 +180,32 @@ impl<'a> Metadata<'a> {
}
}

/// Construct new metadata for a span or event, with a name, target, level, field
/// names, and optional source code location using dynamically allocated data.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
pub fn from_cow(
name: impl Into<Cow<'a, str>>,
target: impl Into<Cow<'a, str>>,
level: Level,
file: Option<impl Into<Cow<'a, str>>>,
line: Option<u32>,
module_path: Option<impl Into<Cow<'a, str>>>,
fields: field::FieldSet,
kind: Kind,
) -> Self {
Metadata {
name: name.into(),
target: target.into(),
level,
module_path: module_path.map(Into::into),
file: file.map(Into::into),
line,
fields,
kind,
}
}

/// Returns the names of the fields on the described span or event.
pub fn fields(&self) -> &field::FieldSet {
&self.fields
Expand All @@ -154,29 +217,29 @@ impl<'a> Metadata<'a> {
}

/// Returns the name of the span.
pub fn name(&self) -> &'static str {
self.name
pub fn name(&self) -> &str {
&self.name
}

/// Returns a string describing the part of the system where the span or
/// event that this metadata describes occurred.
///
/// Typically, this is the module path, but alternate targets may be set
/// when spans or events are constructed.
pub fn target(&self) -> &'a str {
self.target
pub fn target(&self) -> &str {
&self.target
}

/// Returns the path to the Rust module where the span occurred, or
/// `None` if the module path is unknown.
pub fn module_path(&self) -> Option<&'a str> {
self.module_path
pub fn module_path(&'a self) -> Option<&'a str> {
self.module_path.as_ref().map(|p| p.as_ref())
}

/// Returns the name of the source code file where the span
/// occurred, or `None` if the file is unknown
pub fn file(&self) -> Option<&'a str> {
self.file
pub fn file(&'a self) -> Option<&'a str> {
self.file.as_ref().map(|f| f.as_ref())
}

/// Returns the line number in the source code file where the span
Expand Down Expand Up @@ -799,6 +862,12 @@ impl PartialOrd<Level> for LevelFilter {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "alloc")]
use crate::{
callsite::{Callsite, Identifier},
field::FieldSet,
Interest,
};
use core::mem;

#[test]
Expand Down Expand Up @@ -863,4 +932,31 @@ mod tests {
assert_eq!(expected, repr, "repr changed for {:?}", filter)
}
}

#[cfg(feature = "alloc")]
#[test]
fn create_metadata_from_dynamic_data() {
struct TestCallsite;
static CS1: TestCallsite = TestCallsite;

impl Callsite for TestCallsite {
fn set_interest(&self, _interest: Interest) {}
fn metadata(&self) -> &Metadata<'_> {
unimplemented!("not needed for this test")
}
}
let callsite_id = Identifier(&CS1);
let field_set = FieldSet::new(&["one", "fine", "day"], callsite_id);

let _metadata = Metadata::from_cow(
"a name".to_string(),
"a target",
Level::TRACE,
Some("a file".to_string()),
None,
None::<Cow<'_, str>>,
field_set,
Kind::EVENT,
);
}
}
4 changes: 2 additions & 2 deletions tracing-core/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl Collect for TestCollectorA {
}
fn record(&self, _: &span::Id, _: &span::Record<'_>) {}
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}
fn event(&self, _: &Event<'_>) {}
fn event(&self, _: &Event<'_, '_>) {}
fn enter(&self, _: &span::Id) {}
fn exit(&self, _: &span::Id) {}
}
Expand All @@ -24,7 +24,7 @@ impl Collect for TestCollectorB {
}
fn record(&self, _: &span::Id, _: &span::Record<'_>) {}
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}
fn event(&self, _: &Event<'_>) {}
fn event(&self, _: &Event<'_, '_>) {}
fn enter(&self, _: &span::Id) {}
fn exit(&self, _: &span::Id) {}
}
4 changes: 2 additions & 2 deletions tracing-journald/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! [`tracing-subscriber::Subscriber`][subscriber] implementation for logging `tracing` spans
//! and events to [`systemd-journald`][journald], on Linux distributions that
//! use `systemd`.
//!
//!
//! *Compiler support: [requires `rustc` 1.42+][msrv]*
//!
//! [msrv]: #supported-rust-versions
Expand Down Expand Up @@ -154,7 +154,7 @@ where
});
}

fn on_event(&self, event: &Event, ctx: Context<C>) {
fn on_event(&self, event: &Event<'_, '_>, ctx: Context<C>) {
let mut buf = Vec::with_capacity(256);

// Record span fields
Expand Down
Loading