diff --git a/minitrace-datadog/src/lib.rs b/minitrace-datadog/src/lib.rs index bae5079f..ddd0e673 100644 --- a/minitrace-datadog/src/lib.rs +++ b/minitrace-datadog/src/lib.rs @@ -37,7 +37,7 @@ impl DatadogReporter { spans .iter() .map(move |s| DatadogSpan { - name: s.name, + name: &s.name, service: &self.service_name, trace_type: &self.trace_type, resource: &self.resource, diff --git a/minitrace-jaeger/src/lib.rs b/minitrace-jaeger/src/lib.rs index 5002b779..297ec1a9 100644 --- a/minitrace-jaeger/src/lib.rs +++ b/minitrace-jaeger/src/lib.rs @@ -74,7 +74,7 @@ impl JaegerReporter { .iter() .map(|event| Log { timestamp: (event.timestamp_unix_ns / 1_000) as i64, - fields: [("name".into(), event.name.into())] + fields: [("name".into(), event.name.clone())] .iter() .chain(&event.properties) .map(|(k, v)| Tag::String { diff --git a/minitrace-opentelemetry/src/lib.rs b/minitrace-opentelemetry/src/lib.rs index 0a2c5776..97aacb4e 100644 --- a/minitrace-opentelemetry/src/lib.rs +++ b/minitrace-opentelemetry/src/lib.rs @@ -64,7 +64,7 @@ impl OpenTelemetryReporter { TraceState::default(), ), parent_span_id: span.parent_id.0.to_be_bytes().into(), - name: span.name.into(), + name: span.name.clone(), start_time: UNIX_EPOCH + Duration::from_nanos(span.begin_time_unix_ns), end_time: UNIX_EPOCH + Duration::from_nanos(span.begin_time_unix_ns + span.duration_ns), @@ -94,7 +94,7 @@ impl OpenTelemetryReporter { let mut queue = EvictedQueue::new(u32::MAX); queue.extend(events.iter().map(|event| { Event::new( - event.name, + event.name.clone(), UNIX_EPOCH + Duration::from_nanos(event.timestamp_unix_ns), event .properties diff --git a/minitrace/src/collector/global_collector.rs b/minitrace/src/collector/global_collector.rs index 8d99fd7b..113d0d3f 100644 --- a/minitrace/src/collector/global_collector.rs +++ b/minitrace/src/collector/global_collector.rs @@ -434,7 +434,7 @@ fn amend_local_span( if span.is_event { let event = EventRecord { - name: span.name, + name: span.name.clone(), timestamp_unix_ns: begin_time_unix_ns, properties: span.properties.clone(), }; @@ -453,7 +453,7 @@ fn amend_local_span( parent_id, begin_time_unix_ns, duration_ns: end_time_unix_ns.saturating_sub(begin_time_unix_ns), - name: span.name, + name: span.name.clone(), properties: span.properties.clone(), events: vec![], }); @@ -472,7 +472,7 @@ fn amend_span( if raw_span.is_event { let event = EventRecord { - name: raw_span.name, + name: raw_span.name.clone(), timestamp_unix_ns: begin_time_unix_ns, properties: raw_span.properties.clone(), }; @@ -487,7 +487,7 @@ fn amend_span( parent_id, begin_time_unix_ns, duration_ns: end_time_unix_ns.saturating_sub(begin_time_unix_ns), - name: raw_span.name, + name: raw_span.name.clone(), properties: raw_span.properties.clone(), events: vec![], }); diff --git a/minitrace/src/collector/mod.rs b/minitrace/src/collector/mod.rs index b5865fda..b907d010 100644 --- a/minitrace/src/collector/mod.rs +++ b/minitrace/src/collector/mod.rs @@ -50,7 +50,7 @@ pub struct SpanRecord { pub parent_id: SpanId, pub begin_time_unix_ns: u64, pub duration_ns: u64, - pub name: &'static str, + pub name: Cow<'static, str>, pub properties: Vec<(Cow<'static, str>, Cow<'static, str>)>, pub events: Vec, } @@ -58,7 +58,7 @@ pub struct SpanRecord { /// A record of an event that occurred during the execution of a span. #[derive(Clone, Debug, Default)] pub struct EventRecord { - pub name: &'static str, + pub name: Cow<'static, str>, pub timestamp_unix_ns: u64, pub properties: Vec<(Cow<'static, str>, Cow<'static, str>)>, } diff --git a/minitrace/src/event.rs b/minitrace/src/event.rs index c61c1bea..9cc305cd 100644 --- a/minitrace/src/event.rs +++ b/minitrace/src/event.rs @@ -20,7 +20,7 @@ impl Event { /// /// Event::add_to_parent("event in root", &root, || [("key".into(), "value".into())]); /// ``` - pub fn add_to_parent(name: &'static str, parent: &Span, properties: F) + pub fn add_to_parent(name: impl Into>, parent: &Span, properties: F) where I: IntoIterator, Cow<'static, str>)>, F: FnOnce() -> I, @@ -47,7 +47,7 @@ impl Event { /// /// Event::add_to_local_parent("event in root", || [("key".into(), "value".into())]); /// ``` - pub fn add_to_local_parent(name: &'static str, properties: F) + pub fn add_to_local_parent(name: impl Into>, properties: F) where I: IntoIterator, Cow<'static, str>)>, F: FnOnce() -> I, diff --git a/minitrace/src/future.rs b/minitrace/src/future.rs index a7f22dcb..095ce031 100644 --- a/minitrace/src/future.rs +++ b/minitrace/src/future.rs @@ -30,6 +30,7 @@ //! [`in_span()`]:(FutureExt::in_span) //! [`enter_on_poll()`]:(FutureExt::enter_on_poll) +use std::borrow::Cow; use std::task::Poll; use crate::local::LocalSpan; @@ -98,8 +99,11 @@ pub trait FutureExt: std::future::Future + Sized { /// /// [`Future::poll()`]:(std::future::Future::poll) #[inline] - fn enter_on_poll(self, name: &'static str) -> EnterOnPoll { - EnterOnPoll { inner: self, name } + fn enter_on_poll(self, name: impl Into>) -> EnterOnPoll { + EnterOnPoll { + inner: self, + name: name.into(), + } } } @@ -135,7 +139,7 @@ impl std::future::Future for InSpan { pub struct EnterOnPoll { #[pin] inner: T, - name: &'static str, + name: Cow<'static, str>, } impl std::future::Future for EnterOnPoll { @@ -143,7 +147,7 @@ impl std::future::Future for EnterOnPoll { fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { let this = self.project(); - let _guard = LocalSpan::enter_with_local_parent(this.name); + let _guard = LocalSpan::enter_with_local_parent(this.name.clone()); this.inner.poll(cx) } } diff --git a/minitrace/src/local/local_span.rs b/minitrace/src/local/local_span.rs index 9150392f..23489b94 100644 --- a/minitrace/src/local/local_span.rs +++ b/minitrace/src/local/local_span.rs @@ -40,7 +40,7 @@ impl LocalSpan { /// let child = Span::enter_with_local_parent("child"); /// ``` #[inline] - pub fn enter_with_local_parent(name: &'static str) -> Self { + pub fn enter_with_local_parent(name: impl Into>) -> Self { #[cfg(not(feature = "enable"))] { LocalSpan::default() @@ -107,7 +107,10 @@ impl LocalSpan { #[cfg(feature = "enable")] impl LocalSpan { #[inline] - pub(crate) fn enter_with_stack(name: &'static str, stack: Rc>) -> Self { + pub(crate) fn enter_with_stack( + name: impl Into>, + stack: Rc>, + ) -> Self { let span_handle = { let mut stack = stack.borrow_mut(); stack.enter_span(name) diff --git a/minitrace/src/local/local_span_line.rs b/minitrace/src/local/local_span_line.rs index f92d3544..b7dea471 100644 --- a/minitrace/src/local/local_span_line.rs +++ b/minitrace/src/local/local_span_line.rs @@ -33,7 +33,7 @@ impl SpanLine { } #[inline] - pub fn start_span(&mut self, name: &'static str) -> Option { + pub fn start_span(&mut self, name: impl Into>) -> Option { Some(LocalSpanHandle { span_handle: self.span_queue.start_span(name)?, span_line_epoch: self.epoch, @@ -48,7 +48,7 @@ impl SpanLine { } #[inline] - pub fn add_event(&mut self, name: &'static str, properties: F) + pub fn add_event(&mut self, name: impl Into>, properties: F) where I: IntoIterator, Cow<'static, str>)>, F: FnOnce() -> I, diff --git a/minitrace/src/local/local_span_stack.rs b/minitrace/src/local/local_span_stack.rs index 201cb073..50c52d3e 100644 --- a/minitrace/src/local/local_span_stack.rs +++ b/minitrace/src/local/local_span_stack.rs @@ -33,7 +33,7 @@ impl LocalSpanStack { } #[inline] - pub fn enter_span(&mut self, name: &'static str) -> Option { + pub fn enter_span(&mut self, name: impl Into>) -> Option { let span_line = self.current_span_line()?; span_line.start_span(name) } @@ -50,7 +50,7 @@ impl LocalSpanStack { } #[inline] - pub fn add_event(&mut self, name: &'static str, properties: F) + pub fn add_event(&mut self, name: impl Into>, properties: F) where I: IntoIterator, Cow<'static, str>)>, F: FnOnce() -> I, diff --git a/minitrace/src/local/raw_span.rs b/minitrace/src/local/raw_span.rs index 4d3aee49..42f20e49 100644 --- a/minitrace/src/local/raw_span.rs +++ b/minitrace/src/local/raw_span.rs @@ -1,5 +1,7 @@ // Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0. +use std::borrow::Cow; + use minstant::Instant; use crate::collector::SpanId; @@ -10,7 +12,7 @@ pub struct RawSpan { pub id: SpanId, pub parent_id: SpanId, pub begin_instant: Instant, - pub name: &'static str, + pub name: Cow<'static, str>, pub properties: Properties, pub is_event: bool, @@ -24,14 +26,14 @@ impl RawSpan { id: SpanId, parent_id: SpanId, begin_instant: Instant, - name: &'static str, + name: impl Into>, is_event: bool, ) -> Self { RawSpan { id, parent_id, begin_instant, - name, + name: name.into(), properties: Properties::default(), is_event, end_instant: begin_instant, @@ -53,7 +55,7 @@ impl Clone for RawSpan { id: self.id, parent_id: self.parent_id, begin_instant: self.begin_instant, - name: self.name, + name: self.name.clone(), properties: properties, is_event: self.is_event, end_instant: self.end_instant, diff --git a/minitrace/src/local/span_queue.rs b/minitrace/src/local/span_queue.rs index 7cbb9bfd..f168fe8c 100644 --- a/minitrace/src/local/span_queue.rs +++ b/minitrace/src/local/span_queue.rs @@ -28,7 +28,7 @@ impl SpanQueue { } #[inline] - pub fn start_span(&mut self, name: &'static str) -> Option { + pub fn start_span(&mut self, name: impl Into>) -> Option { if self.span_queue.len() >= self.capacity { return None; } @@ -63,7 +63,7 @@ impl SpanQueue { } #[inline] - pub fn add_event(&mut self, name: &'static str, properties: F) + pub fn add_event(&mut self, name: impl Into>, properties: F) where I: IntoIterator, Cow<'static, str>)>, F: FnOnce() -> I, diff --git a/minitrace/src/span.rs b/minitrace/src/span.rs index 8a49434f..fa85bb71 100644 --- a/minitrace/src/span.rs +++ b/minitrace/src/span.rs @@ -68,7 +68,7 @@ impl Span { /// ``` #[inline] pub fn root( - name: &'static str, + name: impl Into>, parent: SpanContext, #[cfg(test)] collect: GlobalCollect, ) -> Self { @@ -108,7 +108,7 @@ impl Span { /// /// let child = Span::enter_with_parent("child", &root); #[inline] - pub fn enter_with_parent(name: &'static str, parent: &Span) -> Self { + pub fn enter_with_parent(name: impl Into>, parent: &Span) -> Self { #[cfg(not(feature = "enable"))] { Self::noop() @@ -147,7 +147,7 @@ impl Span { /// let child = Span::enter_with_parents("child", [&parent1, &parent2]); #[inline] pub fn enter_with_parents<'a>( - name: &'static str, + name: impl Into>, parents: impl IntoIterator, #[cfg(test)] collect: GlobalCollect, ) -> Self { @@ -185,7 +185,7 @@ impl Span { /// ``` #[inline] pub fn enter_with_local_parent( - name: &'static str, + name: impl Into>, #[cfg(test)] collect: GlobalCollect, ) -> Self { #[cfg(not(feature = "enable"))] @@ -358,7 +358,7 @@ impl Span { #[inline] fn new( collect_token: CollectToken, - name: &'static str, + name: impl Into>, collect_id: Option, collect: GlobalCollect, ) -> Self { @@ -377,7 +377,7 @@ impl Span { } pub(crate) fn enter_with_stack( - name: &'static str, + name: impl Into>, stack: &mut LocalSpanStack, collect: GlobalCollect, ) -> Self { diff --git a/minitrace/src/util/tree.rs b/minitrace/src/util/tree.rs index ffbe54c2..97f95aef 100644 --- a/minitrace/src/util/tree.rs +++ b/minitrace/src/util/tree.rs @@ -15,7 +15,7 @@ use crate::util::RawSpans; #[derive(Debug, PartialOrd, PartialEq, Ord, Eq)] pub struct Tree { - name: &'static str, + name: Cow<'static, str>, children: Vec, properties: Vec<(Cow<'static, str>, Cow<'static, str>)>, } @@ -53,12 +53,22 @@ impl Tree { } pub fn from_raw_spans(raw_spans: RawSpans) -> Vec { - let mut children = HashMap::new(); + let mut children: HashMap< + SpanId, + ( + Cow<'static, str>, + Vec, + Vec<(Cow<'static, str>, Cow<'static, str>)>, + ), + > = HashMap::new(); let spans = raw_spans.into_inner(); - children.insert(SpanId::default(), ("", vec![], vec![])); + children.insert(SpanId::default(), ("".into(), vec![], vec![])); for span in &spans { - children.insert(span.id, (span.name, vec![], span.properties.clone())); + children.insert( + span.id, + (span.name.clone(), vec![], span.properties.clone()), + ); } for span in &spans { children @@ -81,39 +91,40 @@ impl Tree { HashMap< SpanId, ( - &'static str, + Cow<'static, str>, Vec, Vec<(Cow<'static, str>, Cow<'static, str>)>, ), >, >::new(); + for (span_set, token) in span_sets { for item in token.iter() { collect .entry(item.collect_id) .or_default() - .insert(SpanId::default(), ("", vec![], vec![])); + .insert(SpanId::default(), ("".into(), vec![], vec![])); match span_set { SpanSet::Span(span) => { - collect - .entry(item.collect_id) - .or_default() - .insert(span.id, (span.name, vec![], span.properties.clone())); + collect.entry(item.collect_id).or_default().insert( + span.id, + (span.name.clone(), vec![], span.properties.clone()), + ); } SpanSet::LocalSpansInner(spans) => { for span in spans.spans.iter() { - collect - .entry(item.collect_id) - .or_default() - .insert(span.id, (span.name, vec![], span.properties.clone())); + collect.entry(item.collect_id).or_default().insert( + span.id, + (span.name.clone(), vec![], span.properties.clone()), + ); } } SpanSet::SharedLocalSpans(spans) => { for span in spans.spans.iter() { - collect - .entry(item.collect_id) - .or_default() - .insert(span.id, (span.name, vec![], span.properties.clone())); + collect.entry(item.collect_id).or_default().insert( + span.id, + (span.name.clone(), vec![], span.properties.clone()), + ); } } } @@ -193,11 +204,21 @@ impl Tree { } pub fn from_span_records(span_records: Vec) -> Tree { - let mut children = HashMap::new(); + let mut children: HashMap< + SpanId, + ( + Cow<'static, str>, + Vec, + Vec<(Cow<'static, str>, Cow<'static, str>)>, + ), + > = HashMap::new(); - children.insert(SpanId::default(), ("", vec![], vec![])); + children.insert(SpanId::default(), ("".into(), vec![], vec![])); for span in &span_records { - children.insert(span.span_id, (span.name, vec![], span.properties.clone())); + children.insert( + span.span_id, + (span.name.clone(), vec![], span.properties.clone()), + ); } for span in &span_records { children @@ -220,7 +241,7 @@ impl Tree { raw: &mut HashMap< SpanId, ( - &'static str, + Cow<'static, str>, Vec, Vec<(Cow<'static, str>, Cow<'static, str>)>, ), diff --git a/minitrace/tests/lib.rs b/minitrace/tests/lib.rs index 26022aac..63af2df9 100644 --- a/minitrace/tests/lib.rs +++ b/minitrace/tests/lib.rs @@ -12,8 +12,8 @@ use tokio::runtime::Builder; fn four_spans() { { // wide - for _ in 0..2 { - let _span = LocalSpan::enter_with_local_parent("iter-span") + for i in 0..2 { + let _span = LocalSpan::enter_with_local_parent(format!("iter-span-{i}")) .with_property(|| ("tmp_property", "tmp_value")); } } @@ -50,8 +50,8 @@ fn single_thread_single_span() { let expected_graph = r#" root [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] "#; @@ -87,22 +87,22 @@ fn single_thread_multiple_spans() { let expected_graph1 = r#" root1 [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] "#; let expected_graph2 = r#" root2 [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] "#; let expected_graph3 = r#" root3 [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] "#; @@ -168,27 +168,27 @@ fn multiple_threads_single_span() { let expected_graph = r#" root [] cross-thread [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] cross-thread [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] cross-thread [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] cross-thread [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] rec-span [] rec-span [] "#; @@ -235,29 +235,29 @@ fn multiple_threads_multiple_spans() { let expected_graph1 = r#" root1 [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] @@ -266,29 +266,29 @@ root1 [] "#; let expected_graph2 = r#" root2 [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span [] merged [] - iter-span [("tmp_property", "tmp_value")] - iter-span [("tmp_property", "tmp_value")] + iter-span-0 [("tmp_property", "tmp_value")] + iter-span-1 [("tmp_property", "tmp_value")] local [] rec-span [] rec-span []