Skip to content

refactor!: clean up trace context ids#177

Open
andylokandy wants to merge 12 commits into
0.8-alphafrom
refactor/trace-context-id-cleanup
Open

refactor!: clean up trace context ids#177
andylokandy wants to merge 12 commits into
0.8-alphafrom
refactor/trace-context-id-cleanup

Conversation

@andylokandy
Copy link
Copy Markdown
Collaborator

@andylokandy andylokandy commented May 4, 2026

Summary

  • Replace public tuple-style trace and span IDs with opaque ID APIs and explicit INVALID values
  • Collapse W3C propagation onto SpanContext with TraceFlags, TraceState, traceparent encode/decode, and header-name constants
  • Preserve root spans without remote parents through Option<SpanId> internally instead of a public zero-span sentinel
  • Update OpenTelemetry, Datadog, and Jaeger mappings to consume the new byte-oriented ID APIs
  • Refresh docs, examples, benches, no-feature coverage, and propagation tests for the new 0.8 breaking contract

Design Notes

TraceId and SpanId are no longer public numeric tuple wrappers. Public construction goes through from_bytes, from_hex, or FromStr. All-zero IDs are represented as TraceId::INVALID and SpanId::INVALID; W3C traceparent parse and encode reject those invalid IDs at the propagation boundary.

SpanContext is now the single cheap propagation and parent/link context. It carries trace flags and tracestate directly, and Span remains the live timed object responsible for collection and reporting.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors fastrace’s trace/span identifier model for the upcoming 0.8 contract by replacing public numeric ID wrappers with checked APIs, moving W3C propagation details onto SpanContext, and updating reporters/tests/docs to the new parent/trace-state model.

Changes:

  • Reworked core tracing types so TraceId/SpanId are opaque checked values and root parents are represented with Option<SpanId>.
  • Moved W3C propagation behavior onto SpanContext, including trace flags, tracestate storage, and traceparent encode/decode helpers.
  • Updated collectors, reporters, tests, benches, and docs to consume the new ID/context APIs.

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/statically-disable/src/main.rs Updates no-feature smoke test to use checked ID constructors and root contexts.
README.md Adds W3C Trace Context documentation and new ID construction guidance.
fastrace/tests/lib.rs Adjusts integration tests for root contexts, checked IDs, and cloned link contexts.
fastrace/src/util/tree.rs Switches tree-building sentinel logic from zero span IDs to None.
fastrace/src/span.rs Propagates trace flags/state through collect tokens and root span creation.
fastrace/src/local/local_span.rs Refreshes local-span docs/tests for checked IDs and richer collect tokens.
fastrace/src/local/local_span_stack.rs Updates stack tests/helpers for optional parent IDs and trace metadata.
fastrace/src/local/local_span_line.rs Preserves optional parent IDs plus trace flags/state in current token logic.
fastrace/src/local/local_collector.rs Updates local collector tests/helpers for the new collect token shape.
fastrace/src/lib.rs Refreshes crate docs and prelude exports for the new propagation model.
fastrace/src/collector/mod.rs Re-exports new trace-state/flag types and expands SpanRecord/CollectToken.
fastrace/src/collector/id.rs Introduces opaque checked IDs, trace flags/state, and the new SpanContext APIs.
fastrace/src/collector/global_collector.rs Carries optional parents, trace flags, and tracestate through span post-processing.
fastrace/benches/trace.rs Updates benchmarks to construct roots via checked/root context APIs.
fastrace/benches/compare.rs Updates comparison benchmark to the new root context API.
fastrace-opentelemetry/tests/context.rs Adjusts OTel bridge test for optional span IDs.
fastrace-opentelemetry/src/lib.rs Maps byte-based IDs, trace flags, and tracestate into OpenTelemetry types.
fastrace-opentelemetry/README.md Refreshes OpenTelemetry bridge example formatting.
fastrace-jaeger/src/lib.rs Converts Jaeger export to use byte-based ID helpers and optional parent IDs.
fastrace-datadog/src/lib.rs Converts Datadog export to use byte-based ID helpers and optional parent IDs.
CHANGELOG.md Documents the unreleased 0.8 trace propagation changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 535 to 549
impl serde::Serialize for SpanContext {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.encode_w3c_traceparent().serialize(serializer)
let traceparent = self.encode_traceparent().ok_or_else(|| {
serde::ser::Error::custom("span context has no span id for traceparent")
})?;
traceparent.serialize(serializer)
}
}

impl<'de> serde::Deserialize<'de> for SpanContext {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
SpanContext::decode_w3c_traceparent(&s)
SpanContext::decode_traceparent(&s)
.ok_or_else(|| serde::de::Error::custom("invalid w3c traceparent"))
}
Comment thread fastrace/src/collector/id.rs Outdated
Comment on lines +535 to +540
impl serde::Serialize for SpanContext {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.encode_w3c_traceparent().serialize(serializer)
let traceparent = self.encode_traceparent().ok_or_else(|| {
serde::ser::Error::custom("span context has no span id for traceparent")
})?;
traceparent.serialize(serializer)
Comment on lines 178 to 191
fn map_links(links: Vec<SpanContext>) -> SpanLinks {
let links = links
.into_iter()
.map(|link| {
let trace_flags = if link.sampled {
TraceFlags::SAMPLED
} else {
TraceFlags::default()
};
.filter_map(|link| {
let span_id = link.span_id()?;
let span_context = OtelSpanContext::new(
link.trace_id.0.into(),
link.span_id.0.into(),
trace_flags,
OtelTraceId::from_bytes(link.trace_id().to_bytes()),
OtelSpanId::from_bytes(span_id.to_bytes()),
map_trace_flags(link.trace_flags()),
false,
TraceState::default(),
map_trace_state(link.trace_state()),
);
Link::with_context(span_context)
Some(Link::with_context(span_context))
})
@andylokandy andylokandy changed the title refactor: clean up trace context ids refactor!: clean up trace context ids May 4, 2026
@andylokandy andylokandy changed the base branch from main to 0.8-alpha May 4, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants