diff --git a/minitrace-macro/src/lib.rs b/minitrace-macro/src/lib.rs index dffa9098..641d17c7 100644 --- a/minitrace-macro/src/lib.rs +++ b/minitrace-macro/src/lib.rs @@ -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 { @@ -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) { @@ -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__, @@ -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 ) } diff --git a/minitrace/examples/asynchronous.rs b/minitrace/examples/asynchronous.rs index aeb12d0d..ecca53f2 100644 --- a/minitrace/examples/asynchronous.rs +++ b/minitrace/examples/asynchronous.rs @@ -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() }; diff --git a/minitrace/examples/synchronous.rs b/minitrace/examples/synchronous.rs index 44b620db..2650d690 100644 --- a/minitrace/examples/synchronous.rs +++ b/minitrace/examples/synchronous.rs @@ -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); diff --git a/minitrace/src/local/local_collector.rs b/minitrace/src/local/local_collector.rs index c0e8456c..a16e7075 100644 --- a/minitrace/src/local/local_collector.rs +++ b/minitrace/src/local/local_collector.rs @@ -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); diff --git a/minitrace/src/local/local_span.rs b/minitrace/src/local/local_span.rs index d797d754..466cab3a 100644 --- a/minitrace/src/local/local_span.rs +++ b/minitrace/src/local/local_span.rs @@ -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(self, property: F) -> Self - where F: FnOnce() -> (Cow<'static, str>, Cow<'static, str>) { + pub fn with_property(self, property: F) -> Self + where + K: Into>, + V: Into>, + F: FnOnce() -> (K, V), + { self.with_properties(|| [property()]) } @@ -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(self, properties: F) -> Self + pub fn with_properties(self, properties: F) -> Self where - I: IntoIterator, Cow<'static, str>)>, + K: Into>, + V: Into>, + I: IntoIterator, F: FnOnce() -> I, { #[cfg(feature = "enable")] @@ -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")); } } @@ -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] @@ -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); } diff --git a/minitrace/src/local/local_span_line.rs b/minitrace/src/local/local_span_line.rs index 7743cfc8..b7dea471 100644 --- a/minitrace/src/local/local_span_line.rs +++ b/minitrace/src/local/local_span_line.rs @@ -57,9 +57,11 @@ impl SpanLine { } #[inline] - pub fn add_properties(&mut self, handle: &LocalSpanHandle, properties: F) + pub fn add_properties(&mut self, handle: &LocalSpanHandle, properties: F) where - I: IntoIterator, Cow<'static, str>)>, + K: Into>, + V: Into>, + I: IntoIterator, F: FnOnce() -> I, { if self.epoch == handle.span_line_epoch { @@ -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); @@ -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(); diff --git a/minitrace/src/local/local_span_stack.rs b/minitrace/src/local/local_span_stack.rs index 72967a97..50c52d3e 100644 --- a/minitrace/src/local/local_span_stack.rs +++ b/minitrace/src/local/local_span_stack.rs @@ -98,9 +98,11 @@ impl LocalSpanStack { } #[inline] - pub fn add_properties(&mut self, local_span_handle: &LocalSpanHandle, properties: F) + pub fn add_properties(&mut self, local_span_handle: &LocalSpanHandle, properties: F) where - I: IntoIterator, Cow<'static, str>)>, + K: Into>, + V: Into>, + I: IntoIterator, F: FnOnce() -> I, { debug_assert!(self.current_span_line().is_some()); @@ -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); diff --git a/minitrace/src/local/span_queue.rs b/minitrace/src/local/span_queue.rs index 4f9eed0f..f168fe8c 100644 --- a/minitrace/src/local/span_queue.rs +++ b/minitrace/src/local/span_queue.rs @@ -85,13 +85,17 @@ impl SpanQueue { } #[inline] - pub fn add_properties(&mut self, span_handle: &SpanHandle, properties: I) - where I: IntoIterator, Cow<'static, str>)> { + pub fn add_properties(&mut self, span_handle: &SpanHandle, properties: I) + where + K: Into>, + V: Into>, + I: IntoIterator, + { 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] @@ -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); diff --git a/minitrace/src/span.rs b/minitrace/src/span.rs index 216a8784..08529a73 100644 --- a/minitrace/src/span.rs +++ b/minitrace/src/span.rs @@ -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(self, property: F) -> Self - where F: FnOnce() -> (Cow<'static, str>, Cow<'static, str>) { + pub fn with_property(self, property: F) -> Self + where + K: Into>, + V: Into>, + F: FnOnce() -> (K, V), + { self.with_properties(move || [property()]) } @@ -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(mut self, properties: F) -> Self + pub fn with_properties(mut self, properties: F) -> Self where - I: IntoIterator, Cow<'static, str>)>, + K: Into>, + V: Into>, + I: IntoIterator, F: FnOnce() -> I, { #[cfg(feature = "enable")] @@ -431,14 +432,16 @@ impl Span { #[cfg(feature = "enable")] impl SpanInner { #[inline] - fn add_properties(&mut self, properties: F) + fn add_properties(&mut self, properties: F) where - I: IntoIterator, Cow<'static, str>)>, + K: Into>, + V: Into>, + I: IntoIterator, 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] @@ -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); @@ -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(); diff --git a/minitrace/tests/lib.rs b/minitrace/tests/lib.rs index 637fb953..6ffdd03c 100644 --- a/minitrace/tests/lib.rs +++ b/minitrace/tests/lib.rs @@ -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")); } } diff --git a/test-statically-disable/src/main.rs b/test-statically-disable/src/main.rs index 02bc5904..c08c1aeb 100644 --- a/test-statically-disable/src/main.rs +++ b/test-statically-disable/src/main.rs @@ -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", || []); @@ -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");