Struct Span

Source
pub struct Span {}
Expand description

A thread-safe span.

Implementations§

Source§

impl Span

Source

pub fn noop() -> Self

Create a place-holder span that never starts recording.

§Examples
use minitrace::prelude::*;

let mut root = Span::noop();
Source

pub fn root(name: impl Into<Cow<'static, str>>, parent: SpanContext) -> Self

Create a new trace and return its root span.

Once dropped, the root span automatically submits all associated child spans to the reporter.

§Examples
use minitrace::prelude::*;

let mut root = Span::root("root", SpanContext::random());
Source

pub fn enter_with_parent( name: impl Into<Cow<'static, str>>, parent: &Span, ) -> Self

Create a new child span associated with the specified parent span.

§Examples
use minitrace::prelude::*;

let root = Span::root("root", SpanContext::random());

let child = Span::enter_with_parent("child", &root);
Source

pub fn enter_with_parents<'a>( name: impl Into<Cow<'static, str>>, parents: impl IntoIterator<Item = &'a Span>, ) -> Self

Create a new child span associated with multiple parent spans.

This function is particularly useful when a single operation amalgamates multiple requests. It enables the creation of a unique child span that is interconnected with all the parent spans related to the requests, thereby obviating the need to generate individual child spans for each parent span.

The newly created child span, and its children, will have a replica for each trace of parent spans.

§Examples
use minitrace::prelude::*;

let parent1 = Span::root("parent1", SpanContext::random());
let parent2 = Span::root("parent2", SpanContext::random());

let child = Span::enter_with_parents("child", [&parent1, &parent2]);
Source

pub fn enter_with_local_parent(name: impl Into<Cow<'static, str>>) -> Self

Create a new child span associated with the current local span in the current thread.

If no local span is active, this function returns a no-op span.

§Examples
use minitrace::prelude::*;

let root = Span::root("root", SpanContext::random());
let _g = root.set_local_parent();

let child = Span::enter_with_local_parent("child");
Source

pub fn set_local_parent(&self) -> LocalParentGuard

Sets the current Span as the local parent for the current thread.

This method is used to establish a Span as the local parent within the current scope.

A local parent is necessary for creating a LocalSpan using LocalSpan::enter_with_local_parent(). If no local parent is set, enter_with_local_parent() will not perform any action.

§Examples
use minitrace::prelude::*;

let root = Span::root("root", SpanContext::random());
let _guard = root.set_local_parent(); // root is now the local parent

// Now we can create a LocalSpan with root as the local parent.
let _span = LocalSpan::enter_with_local_parent("a child span");
Source

pub fn with_property<K, V, F>(self, property: F) -> Self
where K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>, F: FnOnce() -> (K, V),

Add a single property to the Span and return the modified Span.

A property is an arbitrary key-value pair associated with a span.

§Examples
use minitrace::prelude::*;

let root = Span::root("root", SpanContext::random()).with_property(|| ("key", "value"));
Source

pub fn with_properties<K, V, I, F>(self, properties: F) -> Self
where K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>, I: IntoIterator<Item = (K, V)>, F: FnOnce() -> I,

Add multiple properties to the Span and return the modified Span.

§Examples
use minitrace::prelude::*;

let root = Span::root("root", SpanContext::random())
    .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
Source

pub fn push_child_spans(&self, local_spans: LocalSpans)

Attach a collection of LocalSpan instances as child spans to the current span.

This method allows you to associate previously collected LocalSpan instances with the current span. This is particularly useful when the LocalSpan instances were initiated before their parent span, and were collected manually using a LocalCollector.

§Examples
use minitrace::local::LocalCollector;
use minitrace::prelude::*;

// Collect local spans manually without a parent
let collector = LocalCollector::start();
let span = LocalSpan::enter_with_local_parent("a child span");
drop(span);
let local_spans = collector.collect();

// Attach the local spans to a parent
let root = Span::root("root", SpanContext::random());
root.push_child_spans(local_spans);
Source

pub fn elapsed(&self) -> Option<Duration>

Returns the elapsed time since the span was created. If the Span is a noop span, this function will return None.

§Examples
use minitrace::prelude::*;
use std::time::Duration;

let mut root = Span::root("root", SpanContext::random());

// ...

if root
    .elapsed()
    .map(|elapsed| elapsed < Duration::from_secs(1))
    .unwrap_or(false)
{
    root.cancel();
}
Source

pub fn cancel(&mut self)

Dismisses the trace, preventing the reporting of any span records associated with it.

This is particularly useful when focusing on the tail latency of a program. For instant, you can dismiss all traces finishes within the 99th percentile.

§Note

This method only dismisses the entire trace when called on the root span. If called on a non-root span, it will only cancel the reporting of that specific span.

§Examples
use minitrace::prelude::*;

let mut root = Span::root("root", SpanContext::random());

root.cancel();

Trait Implementations§

Source§

impl Default for Span

Source§

fn default() -> Span

Returns the “default value” for a type. Read more
Source§

impl Drop for Span

Source§

fn drop(&mut self)

Executes the destructor for this type. 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 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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

fn vzip(self) -> V