Skip to content

Commit

Permalink
feat: allow to use String as span name
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lok <[email protected]>
  • Loading branch information
andylokandy committed Oct 26, 2023
1 parent bf4cdce commit 75ff9eb
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 96 deletions.
2 changes: 1 addition & 1 deletion minitrace-datadog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion minitrace-jaeger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions minitrace-opentelemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions minitrace/src/collector/global_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand All @@ -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![],
});
Expand All @@ -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(),
};
Expand All @@ -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![],
});
Expand Down
4 changes: 2 additions & 2 deletions minitrace/src/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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<EventRecord>,
}

/// 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>)>,
}
Expand Down
4 changes: 2 additions & 2 deletions minitrace/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Event {
///
/// Event::add_to_parent("event in root", &root, || [("key".into(), "value".into())]);
/// ```
pub fn add_to_parent<I, F>(name: &'static str, parent: &Span, properties: F)
pub fn add_to_parent<I, F>(name: impl Into<Cow<'static, str>>, parent: &Span, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
Expand All @@ -47,7 +47,7 @@ impl Event {
///
/// Event::add_to_local_parent("event in root", || [("key".into(), "value".into())]);
/// ```
pub fn add_to_local_parent<I, F>(name: &'static str, properties: F)
pub fn add_to_local_parent<I, F>(name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
Expand Down
12 changes: 8 additions & 4 deletions minitrace/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self> {
EnterOnPoll { inner: self, name }
fn enter_on_poll(self, name: impl Into<Cow<'static, str>>) -> EnterOnPoll<Self> {
EnterOnPoll {
inner: self,
name: name.into(),
}
}
}

Expand Down Expand Up @@ -135,15 +139,15 @@ impl<T: std::future::Future> std::future::Future for InSpan<T> {
pub struct EnterOnPoll<T> {
#[pin]
inner: T,
name: &'static str,
name: Cow<'static, str>,
}

impl<T: std::future::Future> std::future::Future for EnterOnPoll<T> {
type Output = T::Output;

fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
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)
}
}
7 changes: 5 additions & 2 deletions minitrace/src/local/local_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<'static, str>>) -> Self {
#[cfg(not(feature = "enable"))]
{
LocalSpan::default()
Expand Down Expand Up @@ -107,7 +107,10 @@ impl LocalSpan {
#[cfg(feature = "enable")]
impl LocalSpan {
#[inline]
pub(crate) fn enter_with_stack(name: &'static str, stack: Rc<RefCell<LocalSpanStack>>) -> Self {
pub(crate) fn enter_with_stack(
name: impl Into<Cow<'static, str>>,
stack: Rc<RefCell<LocalSpanStack>>,
) -> Self {
let span_handle = {
let mut stack = stack.borrow_mut();
stack.enter_span(name)
Expand Down
4 changes: 2 additions & 2 deletions minitrace/src/local/local_span_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl SpanLine {
}

#[inline]
pub fn start_span(&mut self, name: &'static str) -> Option<LocalSpanHandle> {
pub fn start_span(&mut self, name: impl Into<Cow<'static, str>>) -> Option<LocalSpanHandle> {
Some(LocalSpanHandle {
span_handle: self.span_queue.start_span(name)?,
span_line_epoch: self.epoch,
Expand All @@ -48,7 +48,7 @@ impl SpanLine {
}

#[inline]
pub fn add_event<I, F>(&mut self, name: &'static str, properties: F)
pub fn add_event<I, F>(&mut self, name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
Expand Down
4 changes: 2 additions & 2 deletions minitrace/src/local/local_span_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl LocalSpanStack {
}

#[inline]
pub fn enter_span(&mut self, name: &'static str) -> Option<LocalSpanHandle> {
pub fn enter_span(&mut self, name: impl Into<Cow<'static, str>>) -> Option<LocalSpanHandle> {
let span_line = self.current_span_line()?;
span_line.start_span(name)
}
Expand All @@ -50,7 +50,7 @@ impl LocalSpanStack {
}

#[inline]
pub fn add_event<I, F>(&mut self, name: &'static str, properties: F)
pub fn add_event<I, F>(&mut self, name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
Expand Down
10 changes: 6 additions & 4 deletions minitrace/src/local/raw_span.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,

Expand All @@ -24,14 +26,14 @@ impl RawSpan {
id: SpanId,
parent_id: SpanId,
begin_instant: Instant,
name: &'static str,
name: impl Into<Cow<'static, str>>,
is_event: bool,
) -> Self {
RawSpan {
id,
parent_id,
begin_instant,
name,
name: name.into(),
properties: Properties::default(),
is_event,
end_instant: begin_instant,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions minitrace/src/local/span_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SpanQueue {
}

#[inline]
pub fn start_span(&mut self, name: &'static str) -> Option<SpanHandle> {
pub fn start_span(&mut self, name: impl Into<Cow<'static, str>>) -> Option<SpanHandle> {
if self.span_queue.len() >= self.capacity {
return None;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl SpanQueue {
}

#[inline]
pub fn add_event<I, F>(&mut self, name: &'static str, properties: F)
pub fn add_event<I, F>(&mut self, name: impl Into<Cow<'static, str>>, properties: F)
where
I: IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
F: FnOnce() -> I,
Expand Down
12 changes: 6 additions & 6 deletions minitrace/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Span {
/// ```
#[inline]
pub fn root(
name: &'static str,
name: impl Into<Cow<'static, str>>,
parent: SpanContext,
#[cfg(test)] collect: GlobalCollect,
) -> Self {
Expand Down Expand Up @@ -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<Cow<'static, str>>, parent: &Span) -> Self {
#[cfg(not(feature = "enable"))]
{
Self::noop()
Expand Down Expand Up @@ -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<Cow<'static, str>>,
parents: impl IntoIterator<Item = &'a Span>,
#[cfg(test)] collect: GlobalCollect,
) -> Self {
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Span {
/// ```
#[inline]
pub fn enter_with_local_parent(
name: &'static str,
name: impl Into<Cow<'static, str>>,
#[cfg(test)] collect: GlobalCollect,
) -> Self {
#[cfg(not(feature = "enable"))]
Expand Down Expand Up @@ -358,7 +358,7 @@ impl Span {
#[inline]
fn new(
collect_token: CollectToken,
name: &'static str,
name: impl Into<Cow<'static, str>>,
collect_id: Option<usize>,
collect: GlobalCollect,
) -> Self {
Expand All @@ -377,7 +377,7 @@ impl Span {
}

pub(crate) fn enter_with_stack(
name: &'static str,
name: impl Into<Cow<'static, str>>,
stack: &mut LocalSpanStack,
collect: GlobalCollect,
) -> Self {
Expand Down
Loading

0 comments on commit 75ff9eb

Please sign in to comment.