Function foundations::telemetry::tracing::start_trace
source · pub fn start_trace(
root_span_name: impl Into<Cow<'static, str>>,
options: StartTraceOptions
) -> SpanScope
Available on (crate features
logging
or metrics
or telemetry
or tracing
) and crate feature tracing
only.Expand description
Starts a new trace. Ends the current one if it is available and links the new one with it.
Can also be used to stitch traces with the context received from other services, and can force enable or disable tracing of certain code parts by overriding the sampling ratio.
§Examples
use foundations::telemetry::TelemetryContext;
use foundations::telemetry::tracing::{self, test_trace, StartTraceOptions};
// Test context is used for demonstration purposes to show the resulting traces.
let ctx = TelemetryContext::test();
let _scope = ctx.scope();
{
let _root = tracing::span("root");
{
let _span1 = tracing::span("span1");
}
let _new_root_span = tracing::start_trace(
"new root",
Default::default(),
);
let _span2 = tracing::span("span2");
}
assert_eq!(
ctx.traces(Default::default()),
vec![
test_trace! {
"root" => {
"span1",
"[new root ref]"
}
},
test_trace! {
"new root" => {
"span2"
}
}
]
);