Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion objectstore-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub fn init(config: &MetricsConfig) -> Result<(), Error> {
Ok(())
}

pub use mock::with_capturing_test_client;
pub use mock::{with_capturing_test_client, with_capturing_test_client_async};

// ---------------------------------------------------------------------------
// Macros
Expand Down
25 changes: 25 additions & 0 deletions objectstore-metrics/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Provides [`with_capturing_test_client`], which installs a thread-local
//! recorder that captures all emitted metrics as DogStatsD-format strings.

use std::future::Future;
use std::sync::{Arc, Mutex};

use metrics::{Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit};
Expand All @@ -26,6 +27,30 @@ pub fn with_capturing_test_client(f: impl FnOnce()) -> Vec<String> {
recorder.consume()
}

/// Awaits `future` with a thread-local mock recorder installed, then returns all captured
/// metrics as `"name:value|type|#key:value,key:value"` strings.
///
/// The recorder stays installed across await points, so metrics emitted while the future is
/// suspended are captured as well. Since it is thread-local, the future must not migrate between
/// threads: run it on a current-thread runtime, as `#[tokio::test]` does by default.
///
/// # Example
///
/// ```ignore
/// let captured = objectstore_metrics::with_capturing_test_client_async(async {
/// objectstore_metrics::count!("test.counter");
/// })
/// .await;
/// assert!(captured.iter().any(|m| m.starts_with("test.counter:")));
/// ```
pub async fn with_capturing_test_client_async(future: impl Future<Output = ()>) -> Vec<String> {
let recorder = MockRecorder::default();
let guard = metrics::set_default_local_recorder(&recorder);
future.await;
drop(guard);
recorder.consume()
}

/// A metrics recorder that formats and stores every operation as a string.
#[derive(Clone, Default)]
struct MockRecorder {
Expand Down
1 change: 1 addition & 0 deletions objectstore-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ objectstore-options = { workspace = true, features = ["testing"] }
objectstore-test = { workspace = true }
stresstest = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["test-util"] }

[[bin]]
name = "objectstore"
Expand Down
Loading
Loading