Add span backdating to core tracing traits#11
Closed
NaluTripician wants to merge 1 commit into
Closed
Conversation
Add explicit start/end timestamp support to the tracing traits so late-bound (tail-sampled) spans can be reconstructed from an already-completed operation with their original timestamps, rather than being stamped at "now". typespec_client_core (`tracing/mod.rs`): - `Tracer::start_span_at` and `Tracer::start_span_with_parent_at` - `Span::end_at` All three are additive with default implementations that delegate to the existing non-backdating methods, so existing `Tracer`/`Span` implementations keep compiling unchanged (non-breaking, minor-release friendly). `azure_core` re-exports these traits, so its public API gains the methods transitively. azure_core_opentelemetry: - Implement the new methods by mapping to raw OpenTelemetry `SpanBuilder::with_start_time` and `SpanRef::end_with_timestamp`. - Refactor span construction into shared `build_span` / `parent_context` helpers to avoid duplication across the four start_span variants. - Add in-memory-exporter unit tests proving a backdated span (and a backdated parent/child pair) carries the injected PAST timestamps. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Owner
Author
|
Re-targeted to upstream: superseded by Azure#4784 (same commit/branch). The effort reviews on upstream. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Tail-based (late-bound) sampling decides after an operation completes whether it's
worth emitting a span — e.g. only for slow or failed requests. To do that we must be able
to reconstruct a span for an operation that already happened, stamped with the
timestamps it actually had, not "now".
Today the tracing traits in
typespec_client_corehave no hook for this:Tracer::start_spanbuilds the span at "now" and
Span::end()closes it at "now". This PR adds an explicitstart/end timestamp path so backdated spans aren't a Cosmos-only concern.
This is the upstream sub-task (WS4c) of the Cosmos diagnostics effort. It is independent
of the Cosmos work and touches only core crates — no Cosmos crate is modified.
What
typespec_client_core(src/tracing/mod.rs):Tracer::start_span_at(name, kind, attributes, start_time)Tracer::start_span_with_parent_at(name, kind, attributes, parent, start_time)Span::end_at(end_time)azure_core_opentelemetry:SpanBuilder::with_start_time/SpanRef::end_with_timestampAPIs.build_span/parent_contexthelpers so thefour
start_span*variants don't duplicate builder logic.test_open_telemetry_span_backdated,test_open_telemetry_span_backdated_with_parent) that assert the exported span carriesthe injected past timestamps (and that a backdated child is correctly parented).
Trait-design rationale (additive, non-breaking)
The change is deliberately additive: all three new methods have default
implementations that delegate to the existing non-backdating methods (dropping the
timestamp). Consequences:
Tracer/Spanimplementations keep compiling unchanged — adding adefaulted method to a trait is a SemVer-minor-compatible change in Rust. This matches the
guidance that Heath is open to additive trait changes in minor releases.
than failing to compile or panicking. The OpenTelemetry bridge overrides the defaults to
honor the timestamps.
std::time::SystemTimeis used as the timestamp type: it's the natural cross-boundary type(no new dependency in
typespec_client_core) and maps directly onto the OpenTelemetrywith_start_time<T: Into<SystemTime>>andend_with_timestamp(SystemTime)signatures.I considered a single
SpanBuilder-style entry point instead of the paired*_atmethods,but the
_atvariants mirror the existingstart_span/start_span_with_parentnaming,keep the diff minimal, and are the smallest additive surface that covers the tail-sampling
use case (backdated root + backdated attempt children). A builder can still be layered on
later without breaking these.
Verification
Run for the touched core crates (
typespec_client_core,azure_core,azure_core_opentelemetry):cargo fmt— cleancargo clippy(lib/tests) — no warningscargo test—azure_core_opentelemetry18 passed (incl. the 2 new backdating tests);typespec_client_core139 passedScope guardrails
azure_data_cosmos/ driver untouched).Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com