Skip to main content

SpanCache

Struct SpanCache 

Source
pub struct SpanCache<P: EnabledPredicate = LevelPredicate> { /* private fields */ }
Expand description

A tracing::Subscriber that funnels closed spans to live consumers.

Open spans live in a sharded Box<[Mutex<Slab<SpanRecord>>]>, with the lane count set by CacheConfig::lane_count (default crate::DEFAULT_LANE_COUNT). The shard is picked by a thread-local key; the slab gives an O(1) cache-friendly index, and the user-facing tracing::span::Id packs (shard, slab_idx+2) into a single u64 so SPAN_STACK push/pop and trait-method dispatch don’t need a separate lookup. When a span closes it moves to a per-thread buffer and is flushed to the Driver via a spillway channel.

The cache itself holds no closed spans — the driver fans each one out to SpanCache::subscribe receivers and then drops it. Late events (an event whose parent span has already been fanned out) have nowhere to land and are dropped.

SpanRecord.id is an actual_id (separate from the tracing id), monotonic within a thread’s ID_BATCH-sized reservation; across threads they interleave, so subscriber-observed order is driver-commit (close-time) order, not strict global open order.

Create with SpanCache::new / SpanCache::with_predicate (defaults) or SpanCache::with_config / SpanCache::with_predicate_and_config (custom batch sizes & lane count). Each returns (SpanCache, Driver); spawn the Driver as a background task to fan closed spans out to subscribers.

Implementations§

Source§

impl SpanCache<LevelPredicate>

Source

pub fn new(capacity: usize) -> (Self, Driver)

Default predicate (TRACE), default config.

Source

pub fn with_config(capacity: usize, config: CacheConfig) -> (Self, Driver)

Default predicate (TRACE) with a custom CacheConfig.

Source§

impl<P: EnabledPredicate> SpanCache<P>

Source

pub fn with_predicate(capacity: usize, predicate: P) -> (Self, Driver)

Custom predicate, default CacheConfig.

Source

pub fn with_predicate_and_config( capacity: usize, predicate: P, config: CacheConfig, ) -> (Self, Driver)

Custom predicate and custom CacheConfig.

Source

pub fn lane_count(&self) -> usize

Number of in-flight slab shards this cache uses.

Source

pub fn actual_id_for(&self, tracing_id: u64) -> Option<u64>

Resolve the actual_id (i.e. the SpanRecord::id published on the fan-out stream) for an in-flight span addressed by its tracing::span::Id u64. Lock-free Acquire load from the per-shard sidecar — does not touch the slab Mutex.

Source

pub fn subscribe(&self, capacity: u64) -> Receiver<SpanRecord>

Register a new subscriber. The returned Receiver yields every closed span the cache produces from this call onward, in the driver’s commit (close-time) order. The cache holds no history — if you need to see spans from before the call, subscribe earlier.

Replaces the previous page(after_id, _) API, which keyed on open-order actual_id and so silently dropped spans whenever close order diverged from open order — the norm under async workloads.

capacity is the soft cap on in-flight spans for this subscriber. When the receiver falls behind by that much, the driver logs and drops a whole batch — slow consumers don’t back up the pipeline. Drop the receiver to unsubscribe; the driver prunes the sender lazily on the next fan-out.

Source

pub fn flush_pending(&self)

Drains the calling thread’s two PENDING buffers (spans + events) into their respective spillway channels. Must be called before Driver::drain_sync in tests to ensure all recently-closed spans and emitted events are delivered.

Trait Implementations§

Source§

impl<P: EnabledPredicate> Subscriber for SpanCache<P>

Source§

fn max_level_hint(&self) -> Option<LevelFilter>

Returns the highest verbosity level that this Subscriber will enable, or None, if the subscriber does not implement level-based filtering or chooses not to implement this method. Read more
Source§

fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest

Registers a new callsite with this subscriber, returning whether or not the subscriber is interested in being notified about the callsite. Read more
Source§

fn enabled(&self, metadata: &Metadata<'_>) -> bool

Returns true if a span or event with the specified metadata would be recorded. Read more
Source§

fn event_enabled(&self, event: &Event<'_>) -> bool

Determine if an Event should be recorded. Read more
Source§

fn new_span(&self, attrs: &Attributes<'_>) -> Id

Visit the construction of a new span, returning a new span ID for the span being constructed. Read more
Source§

fn record(&self, span: &Id, values: &Record<'_>)

Record a set of values on a span. Read more
Source§

fn record_follows_from(&self, _span: &Id, _follows: &Id)

Adds an indication that span follows from the span with the id follows. Read more
Source§

fn event(&self, event: &Event<'_>)

Records that an Event has occurred. Read more
Source§

fn enter(&self, span: &Id)

Records that a span has been entered. Read more
Source§

fn exit(&self, _span: &Id)

Records that a span has been exited. Read more
Source§

fn try_close(&self, id: Id) -> bool

Notifies the subscriber that a span ID has been dropped, and returns true if there are now 0 IDs that refer to that span. Read more
Source§

fn on_register_dispatch(&self, subscriber: &Dispatch)

Invoked when this subscriber becomes a Dispatch. Read more
Source§

fn clone_span(&self, id: &Id) -> Id

Notifies the subscriber that a span ID has been cloned. Read more
Source§

fn drop_span(&self, _id: Id)

👎Deprecated since 0.1.2:

use Subscriber::try_close instead

This method is deprecated. Read more
Source§

fn current_span(&self) -> Current

Returns a type representing this subscriber’s view of the current span. Read more
Source§

unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()>

If self is the same type as the provided TypeId, returns an untyped *const pointer to that type. Otherwise, returns None. Read more

Auto Trait Implementations§

§

impl<P = LevelPredicate> !Freeze for SpanCache<P>

§

impl<P = LevelPredicate> !RefUnwindSafe for SpanCache<P>

§

impl<P> Send for SpanCache<P>

§

impl<P> Sync for SpanCache<P>

§

impl<P> Unpin for SpanCache<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for SpanCache<P>
where P: UnsafeUnpin,

§

impl<P = LevelPredicate> !UnwindSafe for SpanCache<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more