Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lok <[email protected]>
  • Loading branch information
andylokandy committed Jan 24, 2024
1 parent 9bf6e3b commit 038134a
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 69 deletions.
19 changes: 12 additions & 7 deletions minitrace-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ impl Parse for Args {
/// async fn properties(a: u64) {
/// let __span__ = Span::enter_with_local_parent("example::properties").with_properties(|| {
/// [
/// ("k1".into(), "v1".into()),
/// ("a".into(), format!("argument `a` is {a:?}").into()),
/// (std::borrow::Cow::from("k1"), std::borrow::Cow::from("v1")),
/// (
/// std::borrow::Cow::from("a"),
/// std::borrow::Cow::from(format!("argument `a` is {a:?}")),
/// ),
/// ]
/// });
/// async {
Expand Down Expand Up @@ -310,16 +313,18 @@ fn gen_properties(span: proc_macro2::Span, args: &Args) -> proc_macro2::TokenStr

if need_format {
quote_spanned!(span=>
(#k.into(), format!(#v).into())
(std::borrow::Cow::from(#k), std::borrow::Cow::from(format!(#v)))
)
} else {
quote_spanned!(span=>
(#k.into(), #v.into())
(std::borrow::Cow::from(#k), std::borrow::Cow::from(#v))
)
}
});
let properties = Punctuated::<_, Token![,]>::from_iter(properties);
quote_spanned!(span=> #properties)
quote_spanned!(span=>
.with_properties(|| [ #properties ])
)
}

fn unescape_format_string(s: &str) -> (String, bool) {
Expand Down Expand Up @@ -359,7 +364,7 @@ fn gen_block(
} else {
quote_spanned!(block.span()=>
{
let __span__ = minitrace::Span::enter_with_local_parent( #name ).with_properties(|| [ #properties ]);
let __span__ = minitrace::Span::enter_with_local_parent( #name ) #properties;
minitrace::future::FutureExt::in_span(
async move { #block },
__span__,
Expand All @@ -381,7 +386,7 @@ fn gen_block(
}

quote_spanned!(block.span()=>
let __guard__ = minitrace::local::LocalSpan::enter_with_local_parent( #name ).with_properties(|| [ #properties ]);
let __guard__ = minitrace::local::LocalSpan::enter_with_local_parent( #name ) #properties;
#block
)
}
Expand Down
2 changes: 1 addition & 1 deletion minitrace/examples/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() {
let f = async {
let jhs = {
let _span = LocalSpan::enter_with_local_parent("a span")
.with_property(|| ("a property".into(), "a value".into()));
.with_property(|| ("a property", "a value"));
parallel_job()
};

Expand Down
2 changes: 1 addition & 1 deletion minitrace/examples/synchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() {

let _g = root.set_local_parent();
let _span = LocalSpan::enter_with_local_parent("a span")
.with_property(|| ("a property".into(), "a value".into()));
.with_property(|| ("a property", "a value"));

for i in 1..=10 {
func1(i);
Expand Down
6 changes: 2 additions & 4 deletions minitrace/src/local/local_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,8 @@ span1 []
#[test]
fn local_spans_to_span_record() {
let collector = LocalCollector::start();
let span1 = LocalSpan::enter_with_local_parent("span1")
.with_property(|| ("k1".into(), "v1".into()));
let span2 = LocalSpan::enter_with_local_parent("span2")
.with_property(|| ("k2".into(), "v2".into()));
let span1 = LocalSpan::enter_with_local_parent("span1").with_property(|| ("k1", "v1"));
let span2 = LocalSpan::enter_with_local_parent("span2").with_property(|| ("k2", "v2"));
drop(span2);
drop(span1);

Expand Down
37 changes: 19 additions & 18 deletions minitrace/src/local/local_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ impl LocalSpan {
/// ```
/// use minitrace::prelude::*;
///
/// let span = LocalSpan::enter_with_local_parent("a child span")
/// .with_property(|| ("key".into(), "value".into()));
/// let span =
/// LocalSpan::enter_with_local_parent("a child span").with_property(|| ("key", "value"));
/// ```
#[inline]
pub fn with_property<F>(self, property: F) -> Self
where F: FnOnce() -> (Cow<'static, str>, Cow<'static, str>) {
pub fn with_property<K, V, F>(self, property: F) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
F: FnOnce() -> (K, V),
{
self.with_properties(|| [property()])
}

Expand All @@ -79,17 +83,15 @@ impl LocalSpan {
/// ```
/// use minitrace::prelude::*;
///
/// let span = LocalSpan::enter_with_local_parent("a child span").with_properties(|| {
/// [
/// ("key1".into(), "value1".into()),
/// ("key2".into(), "value2".into()),
/// ]
/// });
/// let span = LocalSpan::enter_with_local_parent("a child span")
/// .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
/// ```
#[inline]
pub fn with_properties<I, F>(self, properties: F) -> Self
pub fn with_properties<K, V, I, F>(self, properties: F) -> Self
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
#[cfg(feature = "enable")]
Expand Down Expand Up @@ -159,8 +161,8 @@ mod tests {
{
let _g = LocalSpan::enter_with_stack("span1", stack.clone());
{
let _span = LocalSpan::enter_with_stack("span2", stack)
.with_property(|| ("k1".into(), "v1".into()));
let _span =
LocalSpan::enter_with_stack("span2", stack).with_property(|| ("k1", "v1"));
}
}

Expand All @@ -177,8 +179,7 @@ span1 []

#[test]
fn local_span_noop() {
let _span1 = LocalSpan::enter_with_local_parent("span1")
.with_property(|| ("k1".into(), "v1".into()));
let _span1 = LocalSpan::enter_with_local_parent("span1").with_property(|| ("k1", "v1"));
}

#[test]
Expand All @@ -197,8 +198,8 @@ span1 []
{
let span1 = LocalSpan::enter_with_stack("span1", stack.clone());
{
let _span2 = LocalSpan::enter_with_stack("span2", stack)
.with_property(|| ("k1".into(), "v1".into()));
let _span2 =
LocalSpan::enter_with_stack("span2", stack).with_property(|| ("k1", "v1"));

drop(span1);
}
Expand Down
10 changes: 6 additions & 4 deletions minitrace/src/local/local_span_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ impl SpanLine {
}

#[inline]
pub fn add_properties<I, F>(&mut self, handle: &LocalSpanHandle, properties: F)
pub fn add_properties<K, V, I, F>(&mut self, handle: &LocalSpanHandle, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
if self.epoch == handle.span_line_epoch {
Expand Down Expand Up @@ -111,7 +113,7 @@ mod tests {
let span2 = span_line.start_span("span2").unwrap();
{
let span3 = span_line.start_span("span3").unwrap();
span_line.add_properties(&span3, || [("k1".into(), "v1".into())]);
span_line.add_properties(&span3, || [("k1", "v1")]);
span_line.finish_span(span3);
}
span_line.finish_span(span2);
Expand Down Expand Up @@ -190,7 +192,7 @@ span []
assert_eq!(span_line2.span_line_epoch(), 2);

let span = span_line1.start_span("span").unwrap();
span_line2.add_properties(&span, || [("k1".into(), "v1".into())]);
span_line2.add_properties(&span, || [("k1", "v1")]);
span_line1.finish_span(span);

let raw_spans = span_line1.collect(1).unwrap().0.into_inner();
Expand Down
8 changes: 5 additions & 3 deletions minitrace/src/local/local_span_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ impl LocalSpanStack {
}

#[inline]
pub fn add_properties<I, F>(&mut self, local_span_handle: &LocalSpanHandle, properties: F)
pub fn add_properties<K, V, I, F>(&mut self, local_span_handle: &LocalSpanHandle, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
debug_assert!(self.current_span_line().is_some());
Expand Down Expand Up @@ -355,7 +357,7 @@ span1 []
.into(),
))
.unwrap();
span_stack.add_properties(&span1, || [("k1".into(), "v1".into())]);
span_stack.add_properties(&span1, || [("k1", "v1")]);
let _ = span_stack.unregister_and_collect(span_line2).unwrap();
}
span_stack.exit_span(span1);
Expand Down
17 changes: 9 additions & 8 deletions minitrace/src/local/span_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ impl SpanQueue {
}

#[inline]
pub fn add_properties<I>(&mut self, span_handle: &SpanHandle, properties: I)
where I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)> {
pub fn add_properties<K, V, I>(&mut self, span_handle: &SpanHandle, properties: I)
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
{
debug_assert!(span_handle.index < self.span_queue.len());

let span = &mut self.span_queue[span_handle.index];
span.properties
.extend(properties.into_iter().map(|(k, v)| (k, v)));
.extend(properties.into_iter().map(|(k, v)| (k.into(), v.into())));
}

#[inline]
Expand Down Expand Up @@ -145,13 +149,10 @@ span1 []
let mut queue = SpanQueue::with_capacity(16);
{
let span1 = queue.start_span("span1").unwrap();
queue.add_properties(&span1, [
("k1".into(), "v1".into()),
("k2".into(), "v2".into()),
]);
queue.add_properties(&span1, [("k1", "v1"), ("k2", "v2")]);
{
let span2 = queue.start_span("span2").unwrap();
queue.add_properties(&span2, [("k1".into(), "v1".into())]);
queue.add_properties(&span2, [("k1", "v1")]);
queue.finish_span(span2);
}
queue.finish_span(span1);
Expand Down
39 changes: 21 additions & 18 deletions minitrace/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,15 @@ impl Span {
/// ```
/// use minitrace::prelude::*;
///
/// let root =
/// Span::root("root", SpanContext::random()).with_property(|| ("key".into(), "value".into()));
/// let root = Span::root("root", SpanContext::random()).with_property(|| ("key", "value"));
/// ```
#[inline]
pub fn with_property<F>(self, property: F) -> Self
where F: FnOnce() -> (Cow<'static, str>, Cow<'static, str>) {
pub fn with_property<K, V, F>(self, property: F) -> Self
where
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
F: FnOnce() -> (K, V),
{
self.with_properties(move || [property()])
}

Expand All @@ -266,17 +269,15 @@ impl Span {
/// ```
/// use minitrace::prelude::*;
///
/// let root = Span::root("root", SpanContext::random()).with_properties(|| {
/// [
/// ("key1".into(), "value1".into()),
/// ("key2".into(), "value2".into()),
/// ]
/// });
/// let root = Span::root("root", SpanContext::random())
/// .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
/// ```
#[inline]
pub fn with_properties<I, F>(mut self, properties: F) -> Self
pub fn with_properties<K, V, I, F>(mut self, properties: F) -> Self
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
#[cfg(feature = "enable")]
Expand Down Expand Up @@ -431,14 +432,16 @@ impl Span {
#[cfg(feature = "enable")]
impl SpanInner {
#[inline]
fn add_properties<I, F>(&mut self, properties: F)
fn add_properties<K, V, I, F>(&mut self, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
I: IntoIterator<Item = (K, V)>,
F: FnOnce() -> I,
{
self.raw_span
.properties
.extend(properties().into_iter().map(|(k, v)| (k, v)));
.extend(properties().into_iter().map(|(k, v)| (k.into(), v.into())));
}

#[inline]
Expand Down Expand Up @@ -640,8 +643,8 @@ mod tests {
let routine = |collect| {
let parent_ctx = SpanContext::random();
let root = Span::root("root", parent_ctx, collect);
let child1 = Span::enter_with_parent("child1", &root)
.with_properties(|| [("k1".into(), "v1".into())]);
let child1 =
Span::enter_with_parent("child1", &root).with_properties(|| [("k1", "v1")]);
let grandchild = Span::enter_with_parent("grandchild", &child1);
let child2 = Span::enter_with_parent("child2", &root);

Expand Down Expand Up @@ -709,7 +712,7 @@ root []
[&parent1, &parent2, &parent3, &parent4, &parent5, &child1],
collect,
)
.with_property(|| ("k1".into(), "v1".into()));
.with_property(|| ("k1", "v1"));

crossbeam::scope(move |scope| {
let mut rng = thread_rng();
Expand Down
2 changes: 1 addition & 1 deletion minitrace/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn four_spans() {
// wide
for i in 0..2 {
let _span = LocalSpan::enter_with_local_parent(format!("iter-span-{i}"))
.with_property(|| ("tmp_property".into(), "tmp_value".into()));
.with_property(|| ("tmp_property", "tmp_value"));
}
}

Expand Down
8 changes: 4 additions & 4 deletions test-statically-disable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fn main() {
);

let mut root = Span::root("root", SpanContext::new(TraceId(0), SpanId(0)))
.with_property(|| ("k1".into(), "v1".into()))
.with_properties(|| [("k2".into(), "v2".into())]);
.with_property(|| ("k1", "v1"))
.with_properties(|| [("k2", "v2")]);

Event::add_to_parent("event", &root, || []);
Event::add_to_local_parent("event", || []);
Expand All @@ -35,8 +35,8 @@ fn main() {
Event::add_to_local_parent("event", || []);

let _span1 = LocalSpan::enter_with_local_parent("span1")
.with_property(|| ("k".into(), "v".into()))
.with_properties(|| [("k".into(), "v".into())]);
.with_property(|| ("k", "v"))
.with_properties(|| [("k", "v")]);

let _span2 = LocalSpan::enter_with_local_parent("span2");

Expand Down

0 comments on commit 038134a

Please sign in to comment.