pub struct TraceContext { /* private fields */ }
Expand description
Extract and apply Trace-Context headers.
§Specifications
§Examples
use http_types_rs::trace::TraceContext;
let mut res = http_types_rs::Response::new(200);
res.insert_header(
"traceparent",
"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01"
);
let context = TraceContext::from_headers(&res)?.unwrap();
let trace_id = u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16);
let parent_id = u64::from_str_radix("00f067aa0ba902b7", 16);
assert_eq!(context.trace_id(), trace_id.unwrap());
assert_eq!(context.parent_id(), parent_id.ok());
assert!(context.sampled());
Implementations§
Source§impl TraceContext
impl TraceContext
Sourcepub fn new() -> Self
pub fn new() -> Self
Generate a new TraceContext object without a parent.
By default root TraceContext objects are sampled.
To mark it unsampled, call context.set_sampled(false)
.
§Examples
use http_types_rs::trace::TraceContext;
let context = TraceContext::new();
assert_eq!(context.parent_id(), None);
assert!(context.sampled());
Sourcepub fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>>
pub fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>>
Create and return TraceContext object based on traceparent
HTTP header.
§Errors
This function may error if the header is malformed. An error with a
status code of 400: Bad Request
will be generated.
§Examples
use http_types_rs::trace::TraceContext;
let mut res = http_types_rs::Response::new(200);
res.insert_header(
"traceparent",
"00-0af7651916cd43dd8448eb211c80319c-00f067aa0ba902b7-01"
);
let context = TraceContext::from_headers(&res)?.unwrap();
let trace_id = u128::from_str_radix("0af7651916cd43dd8448eb211c80319c", 16);
let parent_id = u64::from_str_radix("00f067aa0ba902b7", 16);
assert_eq!(context.trace_id(), trace_id.unwrap());
assert_eq!(context.parent_id(), parent_id.ok());
assert!(context.sampled());
Sourcepub fn child(&self) -> Self
pub fn child(&self) -> Self
Generate a child of the current TraceContext and return it.
The child will have a new randomly genrated id
and its parent_id
will be set to the
id
of this TraceContext.
Sourcepub fn version(&self) -> u8
pub fn version(&self) -> u8
Return the version of the TraceContext spec used.
You probably don’t need this.
Sourcepub fn trace_id(&self) -> u128
pub fn trace_id(&self) -> u128
Return the trace id of the TraceContext.
All children will have the same trace_id
.
Sourcepub fn sampled(&self) -> bool
pub fn sampled(&self) -> bool
Returns true if the trace is sampled
§Examples
use http_types_rs::trace::TraceContext;
use http_types_rs::Response;
let mut res = Response::new(200);
res.insert_header("traceparent", "00-00000000000000000000000000000001-0000000000000002-01");
let context = TraceContext::from_headers(&res)?.unwrap();
assert!(context.sampled());
Sourcepub fn set_sampled(&mut self, sampled: bool)
pub fn set_sampled(&mut self, sampled: bool)
Change sampled flag
§Examples
use http_types_rs::trace::TraceContext;
let mut context = TraceContext::new();
assert!(context.sampled());
context.set_sampled(false);
assert!(!context.sampled());
Trait Implementations§
Source§impl Debug for TraceContext
impl Debug for TraceContext
Source§impl Display for TraceContext
impl Display for TraceContext
Source§impl Header for TraceContext
impl Header for TraceContext
Source§fn header_name(&self) -> HeaderName
fn header_name(&self) -> HeaderName
Source§fn header_value(&self) -> HeaderValue
fn header_value(&self) -> HeaderValue
Auto Trait Implementations§
impl Freeze for TraceContext
impl RefUnwindSafe for TraceContext
impl Send for TraceContext
impl Sync for TraceContext
impl Unpin for TraceContext
impl UnwindSafe for TraceContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ToHeaderValues for Twhere
T: Header,
impl<T> ToHeaderValues for Twhere
T: Header,
Source§type Iter = IntoIter<HeaderValue>
type Iter = IntoIter<HeaderValue>
Source§fn to_header_values(&self) -> Result<<T as ToHeaderValues>::Iter, Error>
fn to_header_values(&self) -> Result<<T as ToHeaderValues>::Iter, Error>
HeaderValues
.