forked from awslabs/mountpoint-s3
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request ID to meta request failures and add tests
A side effect of awslabs#669 was that there's now no way to get request IDs for failed requests at the default logging settings, as only DEBUG-level messages include the request IDs. This change adds request IDs to the meta request failure message when available, so that these WARN-level messages still include request IDs. I also added some new infrastructure to test metrics and log messages. For metrics, we build a new `metrics::Recorder` that collects all the metrics and can then be searched to find them. For log messages, we build a `tracing_subscriber::Layer` that collects all tracing events emitted while enabled. In both cases, the new objects aren't thread safe, as both `Recorder`s and `Layer`s are global state. So these tests need to continue to use `rusty_fork` to split into a new process per test. Signed-off-by: James Bornholt <[email protected]>
- Loading branch information
1 parent
9326a48
commit c271be0
Showing
6 changed files
with
395 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use once_cell::sync::Lazy; | ||
use tracing::{Event, Level, Subscriber}; | ||
use tracing_subscriber::field::VisitOutput; | ||
use tracing_subscriber::fmt::format::{DefaultVisitor, Writer}; | ||
use tracing_subscriber::layer::Context; | ||
use tracing_subscriber::Layer; | ||
|
||
static TRACING_TEST_LAYER: Lazy<TracingTestLayer> = Lazy::new(TracingTestLayer::new); | ||
|
||
/// This is a singleton [tracing::Layer] that can be used to write tests for log events. | ||
/// | ||
/// Use it like this: | ||
/// ```rust | ||
/// let _guard = TracingTestLayer::enable(); | ||
/// // ... do work that emits tracing events ... | ||
/// drop(_guard); | ||
/// let events = TracingTestLayer::take_events(); | ||
/// // events is a list of all events emitted | ||
/// ``` | ||
/// | ||
/// THIS IS NOT THREAD SAFE! tracing doesn't give us a good way to separate threads, as tracing | ||
/// subscribers are global state. You almost certainly want to use [rusty_fork] to write tests using | ||
/// this layer. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct TracingTestLayer { | ||
inner: Arc<Inner>, | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
struct Inner { | ||
events: Mutex<Vec<(Level, String)>>, | ||
enabled: AtomicBool, | ||
} | ||
|
||
impl TracingTestLayer { | ||
fn new() -> Self { | ||
Self { | ||
inner: Arc::new(Inner { | ||
events: Mutex::new(Vec::new()), | ||
enabled: AtomicBool::new(false), | ||
}), | ||
} | ||
} | ||
|
||
/// Get a handle to the singleton layer | ||
pub fn get() -> Self { | ||
TRACING_TEST_LAYER.clone() | ||
} | ||
|
||
/// Start collecting tracing events, and stop collecting them when the returned guard drops. | ||
#[must_use = "returns a guard that disables tracing when dropped"] | ||
pub fn enable() -> TracingTestLayerEnableGuard { | ||
TRACING_TEST_LAYER.inner.enabled.store(true, Ordering::SeqCst); | ||
TracingTestLayerEnableGuard {} | ||
} | ||
|
||
/// Take all the collected events | ||
pub fn take_events() -> Vec<(Level, String)> { | ||
TRACING_TEST_LAYER.inner.events.lock().unwrap().drain(..).collect() | ||
} | ||
} | ||
|
||
impl<S: Subscriber> Layer<S> for TracingTestLayer { | ||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { | ||
if self.inner.enabled.load(Ordering::SeqCst) { | ||
let mut msg = String::new(); | ||
let writer = Writer::new(&mut msg); | ||
let visitor = DefaultVisitor::new(writer, true); | ||
visitor.visit(event).unwrap(); | ||
self.inner.events.lock().unwrap().push((*event.metadata().level(), msg)); | ||
} | ||
} | ||
} | ||
|
||
pub struct TracingTestLayerEnableGuard; | ||
|
||
impl Drop for TracingTestLayerEnableGuard { | ||
fn drop(&mut self) { | ||
TRACING_TEST_LAYER.inner.enabled.store(false, Ordering::SeqCst); | ||
} | ||
} |
Oops, something went wrong.