use super::{ScopeField, ScopeFrame, ScopeGuard, ScopeKind};
#[derive(Debug)]
pub struct ScopeFrameBuilder {
fields: Vec<ScopeField>,
kind: ScopeKind,
tail_capacity: u16,
traceparent_sampled: Option<bool>,
span_identity: Option<(&'static str, &'static str)>,
}
impl Default for ScopeFrameBuilder {
fn default() -> Self {
Self::new()
}
}
impl ScopeFrameBuilder {
#[must_use]
pub fn new() -> Self {
Self {
fields: Vec::new(),
kind: ScopeKind::Scope,
tail_capacity: 64,
traceparent_sampled: None,
span_identity: None,
}
}
#[must_use]
pub fn context(mut self) -> Self {
self.kind = ScopeKind::Context;
self.tail_capacity = 0;
self
}
#[must_use]
pub fn tail_capacity(mut self, capacity: u16) -> Self {
self.tail_capacity = capacity;
self
}
#[must_use]
pub fn trace_id(mut self, value: impl Into<String>) -> Self {
self.fields.push(ScopeField::TraceId(value.into()));
self
}
#[must_use]
pub fn span_id(mut self, value: impl Into<String>) -> Self {
self.fields.push(ScopeField::SpanId(value.into()));
self
}
#[must_use]
pub fn parent_span_id(mut self, value: impl Into<String>) -> Self {
self.fields.push(ScopeField::ParentSpanId(value.into()));
self
}
#[must_use]
pub fn label(mut self, name: &'static str, value: impl Into<String>) -> Self {
self.fields.push(ScopeField::Label(name, value.into()));
self
}
#[must_use]
pub fn traceparent_sampled(mut self, sampled: bool) -> Self {
self.traceparent_sampled = Some(sampled);
self
}
#[must_use]
pub fn span_identity(mut self, name: &'static str, target: &'static str) -> Self {
self.span_identity = Some((name, target));
self
}
pub fn push(self) -> ScopeGuard {
let frame = self.into_frame();
ScopeGuard::enter_with_frame(frame)
}
#[must_use]
pub fn into_frame(self) -> ScopeFrame {
let mut frame = ScopeFrame::new(self.fields, self.kind, self.tail_capacity);
if let Some(sampled) = self.traceparent_sampled {
frame.set_traceparent_sampled(sampled);
}
if let Some((name, target)) = self.span_identity {
frame.set_span_identity(name, target);
}
frame
}
}