pub struct TestMetricsCollector { /* private fields */ }Expand description
Test metrics collector for use in integration tests
A simple in-memory metrics collector that records counter increments, histogram values, and gauge settings for test assertions.
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.increment_counter("test.counter", 1);
metrics.record_histogram("test.histogram", 42.0);
metrics.set_gauge("test.gauge", 100.0);
assert_eq!(metrics.get_counter("test.counter"), 1);
assert_eq!(metrics.get_histogram_values("test.histogram"), vec![42.0]);
assert_eq!(metrics.get_gauge("test.gauge"), Some(100.0));Implementations§
Source§impl TestMetricsCollector
impl TestMetricsCollector
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new test metrics collector
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let collector = TestMetricsCollector::new();
assert_eq!(collector.get_counter("any"), 0);Sourcepub fn increment_counter(&self, name: &str, value: u64)
pub fn increment_counter(&self, name: &str, value: u64)
Increment a counter metric
Adds the given value to the counter, creating it if it doesn’t exist.
§Parameters
name- Counter metric namevalue- Value to add (default is 1)
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.increment_counter("my.counter", 1);
metrics.increment_counter("my.counter", 2);
assert_eq!(metrics.get_counter("my.counter"), 3);Sourcepub fn record_histogram(&self, name: &str, value: f64)
pub fn record_histogram(&self, name: &str, value: f64)
Record a value in a histogram metric
Adds the value to the histogram’s recorded values.
§Parameters
name- Histogram metric namevalue- Value to record
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.record_histogram("latency_ms", 100.0);
metrics.record_histogram("latency_ms", 200.0);
let values = metrics.get_histogram_values("latency_ms");
assert_eq!(values.len(), 2);
assert_eq!(values[0], 100.0);
assert_eq!(values[1], 200.0);Sourcepub fn set_gauge(&self, name: &str, value: f64)
pub fn set_gauge(&self, name: &str, value: f64)
Set a gauge metric to a specific value
§Parameters
name- Gauge metric namevalue- Value to set
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.set_gauge("temperature", 98.6);
metrics.set_gauge("temperature", 99.1);
assert_eq!(metrics.get_gauge("temperature"), Some(99.1));Sourcepub fn get_counter(&self, name: &str) -> u64
pub fn get_counter(&self, name: &str) -> u64
Get the current value of a counter metric
Returns 0 if the counter has never been incremented.
§Parameters
name- Counter metric name
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
assert_eq!(metrics.get_counter("test"), 0);
metrics.increment_counter("test", 5);
assert_eq!(metrics.get_counter("test"), 5);Sourcepub fn get_histogram_values(&self, name: &str) -> Vec<f64>
pub fn get_histogram_values(&self, name: &str) -> Vec<f64>
Get all recorded values for a histogram metric
Returns an empty vector if the histogram has no values.
§Parameters
name- Histogram metric name
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
assert!(metrics.get_histogram_values("test").is_empty());
metrics.record_histogram("test", 1.0);
assert_eq!(metrics.get_histogram_values("test"), vec![1.0]);Sourcepub fn get_gauge(&self, name: &str) -> Option<f64>
pub fn get_gauge(&self, name: &str) -> Option<f64>
Get the current value of a gauge metric
Returns None if the gauge has never been set.
§Parameters
name- Gauge metric name
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
assert_eq!(metrics.get_gauge("test"), None);
metrics.set_gauge("test", 42.0);
assert_eq!(metrics.get_gauge("test"), Some(42.0));Sourcepub fn clear(&self)
pub fn clear(&self)
Clear all recorded metrics
Useful for resetting state between test cases.
§Panics
Panics if any internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.increment_counter("test", 5);
metrics.clear();
assert_eq!(metrics.get_counter("test"), 0);Sourcepub fn counter_names(&self) -> Vec<String>
pub fn counter_names(&self) -> Vec<String>
Get all counter names that have been recorded
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.increment_counter("counter1", 1);
metrics.increment_counter("counter2", 1);
let names = metrics.counter_names();
assert_eq!(names.len(), 2);
assert!(names.contains(&"counter1".to_string()));Sourcepub fn histogram_names(&self) -> Vec<String>
pub fn histogram_names(&self) -> Vec<String>
Get all histogram names that have been recorded
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.record_histogram("hist1", 1.0);
metrics.record_histogram("hist2", 2.0);
let names = metrics.histogram_names();
assert_eq!(names.len(), 2);
assert!(names.contains(&"hist1".to_string()));Sourcepub fn gauge_names(&self) -> Vec<String>
pub fn gauge_names(&self) -> Vec<String>
Get all gauge names that have been recorded
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
§Examples
use juncture_tracing::test_utils::TestMetricsCollector;
let metrics = TestMetricsCollector::new();
metrics.set_gauge("gauge1", 1.0);
metrics.set_gauge("gauge2", 2.0);
let names = metrics.gauge_names();
assert_eq!(names.len(), 2);
assert!(names.contains(&"gauge1".to_string()));Sourcepub fn increment_counter_with_labels(
&self,
name: &str,
value: u64,
labels: &[(impl ToString, impl ToString)],
)
pub fn increment_counter_with_labels( &self, name: &str, value: u64, labels: &[(impl ToString, impl ToString)], )
Increment a counter metric with labels
Labels are sorted internally for consistent key matching.
§Panics
Panics if the internal mutex is poisoned (should not happen in normal usage).
Trait Implementations§
Source§impl Clone for TestMetricsCollector
impl Clone for TestMetricsCollector
Source§fn clone(&self) -> TestMetricsCollector
fn clone(&self) -> TestMetricsCollector
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more