Struct TraceContext

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

A TraceContext object

Implementations§

Source§

impl TraceContext

Source

pub fn extract(headers: &HeaderMap) -> Result<Self, ParseIntError>

Create and return TraceContext object based on traceparent HTTP header.

§Examples
let mut headers = http::HeaderMap::new();
headers.insert("traceparent", "00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01".parse().unwrap());
 
let context = trace_context::TraceContext::extract(&headers).unwrap();
 
assert_eq!(context.trace_id(), u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16).unwrap());
assert_eq!(context.parent_id(), u64::from_str_radix("00f067aa0ba902b7",
16).ok());
assert_eq!(context.sampled(), true);
Source

pub fn new_root() -> Self

Source

pub fn inject(&self, headers: &mut HeaderMap)

Add the traceparent header to the http headers

§Examples
let mut input_headers = http::HeaderMap::new();
input_headers.insert("traceparent", "00-00000000000000000000000000000001-0000000000000002-01".parse().unwrap());

let parent = trace_context::TraceContext::extract(&input_headers).unwrap();

let mut output_headers = http::HeaderMap::new();
parent.inject(&mut output_headers);

let child = trace_context::TraceContext::extract(&output_headers).unwrap();

assert_eq!(child.version(), parent.version());
assert_eq!(child.trace_id(), parent.trace_id());
assert_eq!(child.parent_id(), Some(parent.id()));
assert_eq!(child.flags(), parent.flags());
Source

pub fn child(&self) -> Self

Source

pub fn id(&self) -> u64

Source

pub fn version(&self) -> u8

Source

pub fn trace_id(&self) -> u128

Source

pub fn parent_id(&self) -> Option<u64>

Source

pub fn flags(&self) -> u8

Source

pub fn sampled(&self) -> bool

Returns true if the trace is sampled

§Examples
let mut headers = http::HeaderMap::new();
headers.insert("traceparent", "00-00000000000000000000000000000001-0000000000000002-01".parse().unwrap());
let context = trace_context::TraceContext::extract(&headers).unwrap();
assert_eq!(context.sampled(), true);
Source

pub fn set_sampled(&mut self, sampled: bool)

Change sampled flag

§Examples
let mut context = trace_context::TraceContext::new_root();
assert_eq!(context.sampled(), true);
context.set_sampled(false);
assert_eq!(context.sampled(), false);

Trait Implementations§

Source§

impl Debug for TraceContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for TraceContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.