Skip to main content

TestTrace

Struct TestTrace 

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

Main test trace collector.

Collects trace entries during test execution and can output them as structured JSON or console summary.

Implementations§

Source§

impl TestTrace

Source

pub fn new(name: impl Into<String>) -> Self

Creates a new test trace with the given name.

§Example
let mut trace = TestTrace::new("client-server-flow");
Source

pub fn builder(name: impl Into<String>) -> TestTraceBuilder

Creates a trace builder for advanced configuration.

Source

pub fn name(&self) -> &str

Returns the trace name.

Source

pub fn current_span_id(&self) -> Option<&str>

Returns the current span ID, if any.

Source

pub fn log_request( &mut self, method: impl Into<String>, params: Option<&impl Serialize>, ) -> String

Logs a request.

Returns a correlation ID that can be used to match with the response.

Source

pub fn log_response( &mut self, correlation_id: &str, result: Option<&impl Serialize>, error: Option<&impl Serialize>, )

Logs a response.

Uses the correlation ID from the matching request to calculate duration.

Source

pub fn log_response_with_duration( &mut self, method: impl Into<String>, duration: Duration, result: Option<&impl Serialize>, error: Option<&impl Serialize>, )

Logs a response with explicit duration.

Source

pub fn start_span(&mut self, name: impl Into<String>) -> String

Starts a new span for tracking a nested operation.

Returns the span ID.

Source

pub fn end_span(&mut self, error: Option<&str>)

Ends the current span.

If error is provided, marks the span as failed.

Source

pub fn end_span_by_id(&mut self, span_id: &str, error: Option<&str>)

Ends a specific span by ID.

Source

pub fn log(&mut self, level: TraceLevel, message: impl Into<String>)

Logs a message at the specified level.

Source

pub fn log_with_data( &mut self, level: TraceLevel, message: impl Into<String>, data: impl Serialize, )

Logs a message with additional data.

Source

pub fn debug(&mut self, message: impl Into<String>)

Logs a debug message.

Source

pub fn info(&mut self, message: impl Into<String>)

Logs an info message.

Source

pub fn warn(&mut self, message: impl Into<String>)

Logs a warning message.

Source

pub fn error(&mut self, message: impl Into<String>)

Logs an error message.

Source

pub fn metric( &mut self, name: impl Into<String>, value: f64, unit: Option<&str>, )

Records a metric value.

Source

pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<Value>)

Adds custom metadata to the trace.

Source

pub fn build_output(&self) -> TestTraceOutput

Builds the complete trace output.

Source

pub fn save(&self, path: impl AsRef<Path>) -> Result<()>

Saves the trace to a JSON file.

Creates parent directories if they don’t exist.

§Errors

Returns an error if the file cannot be written.

Source

pub fn auto_save( &self, cleanup: Option<&TraceRetentionConfig>, ) -> Result<PathBuf>

Saves the trace with automatic file naming to the trace directory.

The file is saved to FASTMCP_TEST_TRACE_DIR/{name}_{timestamp}.json. Creates the directory if it doesn’t exist.

Optionally runs cleanup to remove old trace files.

§Arguments
  • cleanup - If Some, runs cleanup with the given retention config
§Returns

The path to the saved file.

§Errors

Returns an error if the file cannot be written.

§Example
let trace = TestTrace::new("my-test");
// ... record trace entries ...

// Save with default cleanup
let path = trace.auto_save(Some(&TraceRetentionConfig::default()))?;
println!("Trace saved to: {}", path.display());

// Save without cleanup
let path = trace.auto_save(None)?;
Source

pub fn to_json(&self) -> Result<String>

Returns the trace as a JSON string.

§Errors

Returns an error if serialization fails.

Source

pub fn print_summary(&self)

Prints a summary to stderr.

Source

pub fn entries(&self) -> &[TraceEntry]

Returns all entries.

Source

pub fn entry_count(&self) -> usize

Returns the number of entries.

Trait Implementations§

Source§

impl Debug for TestTrace

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> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more