Skip to main content

Span

Struct Span 

Source
pub struct Span { /* private fields */ }
Expand description

A single open span. Create one with span or client_span (or Span::new for full control over SpanKind); it becomes the “current” span on this thread until it’s dropped (or Span::end is called explicitly, which is equivalent).

Nesting: creating a Span while another is already active makes the new one its child — the child inherits the parent’s trace_id and sampling decision, and parent_span_id() is the parent’s span_id(). Dropping the child makes the parent “current” again.

Not Send: a Span must be dropped on the thread that created it, since dropping pops a thread-local stack — moving one to another thread and dropping it there would corrupt that thread’s stack.

§Example

use rust_web_server::otel;

let span = otel::span("db.query");
span.set_attribute("db.statement", "SELECT 1");
// ... do the work ...
span.end(); // or just let it drop at the end of scope

Implementations§

Source§

impl Span

Source

pub fn new(name: &str, kind: SpanKind) -> Span

Start a new span with an explicit SpanKind. Prefer span or client_span for the common cases.

Source

pub fn set_attribute(&self, key: &str, value: impl Into<AttributeValue>)

Attach a key/value attribute. Repeated keys are appended, not deduplicated — matching this module’s existing “loosely OTLP” style.

Source

pub fn set_error(&self)

Mark this span as failed (OTLP Status.code = 2, Error).

Source

pub fn record_error(&self, message: &str)

Mark this span as failed and attach an error.message attribute.

Source

pub fn trace_id(&self) -> [u8; 16]

Source

pub fn span_id(&self) -> [u8; 8]

Source

pub fn parent_span_id(&self) -> Option<[u8; 8]>

Source

pub fn end(self)

End the span now. Equivalent to letting it drop at the end of scope — both run the exact same recording logic — but useful when you want the span’s duration to stop before other work continues in the same scope.

Trait Implementations§

Source§

impl Drop for Span

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl !Freeze for Span

§

impl !RefUnwindSafe for Span

§

impl !Send for Span

§

impl !Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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