Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion fastrace-opentelemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl OpenTelemetryReporter {
trace_id,
span_id,
parent_id,
parent_is_remote,
begin_time_unix_ns,
duration_ns,
name,
Expand All @@ -157,7 +158,7 @@ impl OpenTelemetryReporter {
SpanData {
span_context,
parent_span_id,
parent_span_is_remote: false,
parent_span_is_remote: parent_is_remote,
span_kind,
name,
start_time,
Expand Down
26 changes: 25 additions & 1 deletion fastrace/src/collector/global_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ enum SpanCollection {
spans: SpanSet,
trace_id: TraceId,
parent_id: SpanId,
parent_is_remote: bool,
},
Shared {
spans: Arc<SpanSet>,
trace_id: TraceId,
parent_id: SpanId,
parent_is_remote: bool,
},
}

Expand Down Expand Up @@ -347,12 +349,14 @@ impl GlobalCollector {
spans,
trace_id: item.trace_id,
parent_id: item.parent_id,
parent_is_remote: item.parent_is_remote,
});
} else if !self.config.tail_sampled {
stale_spans.push(SpanCollection::Owned {
spans,
trace_id: item.trace_id,
parent_id: item.parent_id,
parent_is_remote: item.parent_is_remote,
});
}
} else {
Expand All @@ -366,12 +370,14 @@ impl GlobalCollector {
spans: spans.clone(),
trace_id: item.trace_id,
parent_id: item.parent_id,
parent_is_remote: item.parent_is_remote,
});
} else if !self.config.tail_sampled {
stale_spans.push(SpanCollection::Shared {
spans: spans.clone(),
trace_id: item.trace_id,
parent_id: item.parent_id,
parent_is_remote: item.parent_is_remote,
});
}
}
Expand Down Expand Up @@ -430,6 +436,7 @@ impl LocalSpansInner {
self,
parent.trace_id,
parent.span_id,
parent.is_remote,
&mut records,
&mut danglings,
&anchor,
Expand Down Expand Up @@ -458,11 +465,13 @@ fn postprocess_span_collection<'a>(
spans,
trace_id,
parent_id,
parent_is_remote,
} => match spans {
SpanSet::Span(raw_span) => amend_span(
raw_span,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -471,6 +480,7 @@ fn postprocess_span_collection<'a>(
local_spans,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -479,6 +489,7 @@ fn postprocess_span_collection<'a>(
local_spans,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -488,11 +499,13 @@ fn postprocess_span_collection<'a>(
spans,
trace_id,
parent_id,
parent_is_remote,
} => match &**spans {
SpanSet::Span(raw_span) => amend_span(
raw_span,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -501,6 +514,7 @@ fn postprocess_span_collection<'a>(
local_spans,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -509,6 +523,7 @@ fn postprocess_span_collection<'a>(
local_spans,
*trace_id,
*parent_id,
*parent_is_remote,
committed_records,
danglings,
anchor,
Expand All @@ -524,12 +539,18 @@ fn amend_local_span(
local_spans: &LocalSpansInner,
trace_id: TraceId,
parent_id: SpanId,
parent_is_remote: bool,
spans: &mut Vec<SpanRecord>,
dangling: &mut HashMap<SpanId, Vec<DanglingItem>>,
anchor: &Anchor,
) {
for span in local_spans.spans.iter() {
let parent_id = span.parent_id.unwrap_or(parent_id);
let (parent_id, parent_is_remote) = span
.parent_id
.map_or((parent_id, parent_is_remote), |local_parent_id| {
(local_parent_id, false)
});

match span.raw_kind {
RawKind::Span => {
let begin_time_unix_ns = span.begin_instant.as_unix_nanos(anchor);
Expand All @@ -542,6 +563,7 @@ fn amend_local_span(
trace_id,
span_id: span.id,
parent_id,
parent_is_remote,
begin_time_unix_ns,
duration_ns: end_time_unix_ns.saturating_sub(begin_time_unix_ns),
name: span.name.clone(),
Expand Down Expand Up @@ -588,6 +610,7 @@ fn amend_span(
span: &RawSpan,
trace_id: TraceId,
parent_id: SpanId,
parent_is_remote: bool,
spans: &mut Vec<SpanRecord>,
dangling: &mut HashMap<SpanId, Vec<DanglingItem>>,
anchor: &Anchor,
Expand All @@ -600,6 +623,7 @@ fn amend_span(
trace_id,
span_id: span.id,
parent_id,
parent_is_remote,
begin_time_unix_ns,
duration_ns: end_time_unix_ns.saturating_sub(begin_time_unix_ns),
name: span.name.clone(),
Expand Down
29 changes: 28 additions & 1 deletion fastrace/src/collector/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ impl<'de> serde::Deserialize<'de> for SpanId {
pub struct SpanContext {
pub trace_id: TraceId,
pub span_id: SpanId,
// TODO: rename to trace_flags / is_sampled
pub sampled: bool,
pub is_remote: bool,
}

impl SpanContext {
Expand All @@ -154,6 +156,7 @@ impl SpanContext {
trace_id,
span_id,
sampled: true,
is_remote: false,
}
}

Expand All @@ -171,6 +174,7 @@ impl SpanContext {
trace_id: TraceId::random(),
span_id: SpanId(0),
sampled: true,
is_remote: false,
}
}

Expand All @@ -193,6 +197,23 @@ impl SpanContext {
self
}

/// When the `is_remote` flag is `true`, it indicates that this context originated from
/// a remote service via trace context propagation headers.
///
/// The default value is `false`.
///
/// # Examples
///
/// ```
/// use fastrace::prelude::*;
///
/// let span_context = SpanContext::new(TraceId(12), SpanId(34)).is_remote(true);
/// ```
pub fn is_remote(mut self, is_remote: bool) -> Self {
self.is_remote = is_remote;
self
}

/// Creates a `SpanContext` from the given [`Span`]. If the `Span` is a noop span,
/// this function will return `None`.
///
Expand Down Expand Up @@ -221,6 +242,7 @@ impl SpanContext {
trace_id: collect_token.trace_id,
span_id: collect_token.parent_id,
sampled: collect_token.is_sampled,
is_remote: false,
})
}
}
Expand Down Expand Up @@ -255,6 +277,7 @@ impl SpanContext {
trace_id: collect_token.trace_id,
span_id: collect_token.parent_id,
sampled: collect_token.is_sampled,
is_remote: false,
})
}
}
Expand Down Expand Up @@ -295,7 +318,11 @@ impl SpanContext {
if trace_id == 0 || span_id == 0 {
return None;
}
Some(Self::new(TraceId(trace_id), SpanId(span_id)).sampled(sampled))
Some(
Self::new(TraceId(trace_id), SpanId(span_id))
.sampled(sampled)
.is_remote(true),
)
}
_ => None,
}
Expand Down
2 changes: 2 additions & 0 deletions fastrace/src/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct SpanRecord {
pub trace_id: TraceId,
pub span_id: SpanId,
pub parent_id: SpanId,
pub parent_is_remote: bool,
pub begin_time_unix_ns: u64,
pub duration_ns: u64,
pub name: Cow<'static, str>,
Expand All @@ -70,6 +71,7 @@ pub struct CollectTokenItem {
pub collect_id: usize,
pub is_root: bool,
pub is_sampled: bool,
pub parent_is_remote: bool,
}

/// Configuration of the behavior of the global collector.
Expand Down
2 changes: 2 additions & 0 deletions fastrace/src/local/local_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ mod tests {
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let collector2 = LocalCollector::new(Some(token2.into()), stack.clone());
let span2 = stack.borrow_mut().enter_span("span2").unwrap();
Expand Down Expand Up @@ -298,6 +299,7 @@ span1 []
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let collector2 = LocalCollector::new(Some(token2.into()), stack.clone());
let span2 = stack.borrow_mut().enter_span("span2").unwrap();
Expand Down
2 changes: 2 additions & 0 deletions fastrace/src/local/local_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ mod tests {
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let collector = LocalCollector::new(Some(token.into()), stack.clone());

Expand Down Expand Up @@ -264,6 +265,7 @@ span1 []
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let collector = LocalCollector::new(Some(token.into()), stack.clone());

Expand Down
6 changes: 6 additions & 0 deletions fastrace/src/local/local_span_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl SpanLine {
collect_id: item.collect_id,
is_root: item.is_root,
is_sampled: item.is_sampled,
parent_is_remote: false,
})
.collect()
})
Expand Down Expand Up @@ -175,13 +176,15 @@ span1 []
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let token2 = CollectTokenItem {
trace_id: TraceId(1235),
parent_id: SpanId::default(),
collect_id: 43,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let token = [token1, token2].into_iter().collect();
let mut span_line = SpanLine::new(16, 1, Some(token));
Expand All @@ -199,13 +202,15 @@ span1 []
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
},
CollectTokenItem {
trace_id: TraceId(1235),
parent_id: span_line.span_queue.current_parent_id().unwrap(),
collect_id: 43,
is_root: false,
is_sampled: true,
parent_is_remote: false,
}
]);
span_line.finish_span(span);
Expand Down Expand Up @@ -250,6 +255,7 @@ span []
collect_id: 42,
is_root: false,
is_sampled: true,
parent_is_remote: false,
};
let mut span_line1 = SpanLine::new(16, 1, Some(item.into()));
let mut span_line2 = SpanLine::new(16, 2, None);
Expand Down
Loading