Skip to main content

TestMetricsCollector

Struct TestMetricsCollector 

Source
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

Source

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);
Source

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 name
  • value - 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);
Source

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 name
  • value - 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);
Source

pub fn set_gauge(&self, name: &str, value: f64)

Set a gauge metric to a specific value

§Parameters
  • name - Gauge metric name
  • value - 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));
Source

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);
Source

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]);
Source

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));
Source

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);
Source

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()));
Source

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()));
Source

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()));
Source

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).

Source

pub fn get_counter_with_labels( &self, name: &str, labels: &[(impl ToString, impl ToString)], ) -> u64

Get counter value for a specific label set

Returns 0 if no counter with those labels has been recorded.

§Panics

Panics if the internal mutex is poisoned (should not happen in normal usage).

Trait Implementations§

Source§

impl Clone for TestMetricsCollector

Source§

fn clone(&self) -> TestMetricsCollector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TestMetricsCollector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TestMetricsCollector

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl MetricsCollector for TestMetricsCollector

Source§

fn inc_counter(&self, name: &str, value: u64)

Increment a counter metric by value.
Source§

fn record_histogram(&self, name: &str, value: f64)

Record value to a histogram metric.
Source§

fn set_gauge(&self, name: &str, value: u64)

Set a gauge metric to value.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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: 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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