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
impl TestTrace
Sourcepub fn builder(name: impl Into<String>) -> TestTraceBuilder
pub fn builder(name: impl Into<String>) -> TestTraceBuilder
Creates a trace builder for advanced configuration.
Sourcepub fn current_span_id(&self) -> Option<&str>
pub fn current_span_id(&self) -> Option<&str>
Returns the current span ID, if any.
Sourcepub fn log_request(
&mut self,
method: impl Into<String>,
params: Option<&impl Serialize>,
) -> String
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.
Sourcepub fn log_response(
&mut self,
correlation_id: &str,
result: Option<&impl Serialize>,
error: Option<&impl Serialize>,
)
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.
Sourcepub fn log_response_with_duration(
&mut self,
method: impl Into<String>,
duration: Duration,
result: Option<&impl Serialize>,
error: Option<&impl Serialize>,
)
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.
Sourcepub fn start_span(&mut self, name: impl Into<String>) -> String
pub fn start_span(&mut self, name: impl Into<String>) -> String
Starts a new span for tracking a nested operation.
Returns the span ID.
Sourcepub fn end_span(&mut self, error: Option<&str>)
pub fn end_span(&mut self, error: Option<&str>)
Ends the current span.
If error is provided, marks the span as failed.
Sourcepub fn end_span_by_id(&mut self, span_id: &str, error: Option<&str>)
pub fn end_span_by_id(&mut self, span_id: &str, error: Option<&str>)
Ends a specific span by ID.
Sourcepub fn log(&mut self, level: TraceLevel, message: impl Into<String>)
pub fn log(&mut self, level: TraceLevel, message: impl Into<String>)
Logs a message at the specified level.
Sourcepub fn log_with_data(
&mut self,
level: TraceLevel,
message: impl Into<String>,
data: impl Serialize,
)
pub fn log_with_data( &mut self, level: TraceLevel, message: impl Into<String>, data: impl Serialize, )
Logs a message with additional data.
Sourcepub fn metric(
&mut self,
name: impl Into<String>,
value: f64,
unit: Option<&str>,
)
pub fn metric( &mut self, name: impl Into<String>, value: f64, unit: Option<&str>, )
Records a metric value.
Sourcepub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<Value>)
pub fn add_metadata(&mut self, key: impl Into<String>, value: impl Into<Value>)
Adds custom metadata to the trace.
Sourcepub fn build_output(&self) -> TestTraceOutput
pub fn build_output(&self) -> TestTraceOutput
Builds the complete trace output.
Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<()>
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.
Sourcepub fn auto_save(
&self,
cleanup: Option<&TraceRetentionConfig>,
) -> Result<PathBuf>
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)?;Sourcepub fn print_summary(&self)
pub fn print_summary(&self)
Prints a summary to stderr.
Sourcepub fn entries(&self) -> &[TraceEntry]
pub fn entries(&self) -> &[TraceEntry]
Returns all entries.
Sourcepub fn entry_count(&self) -> usize
pub fn entry_count(&self) -> usize
Returns the number of entries.