Skip to content

Commit cbcfbf9

Browse files
hdsdavidbarsky
authored andcommitted
mock: document public APIs in subscriber module (#2446)
There has been interest around publishing `tracing-mock` to crates.io for some time. In order to make this possible, documentation and some code clean up is needed. The `subscriber` module needs documentation and examples. This change adds documentation to the `subscriber` module and all the public APIs within it. This includes doctests on all the methods which serve as examples. The `MockSubscriberBuilder::record` method was removed as its functionality is not implemented. Previously, the `MockSubscriber` would verify the scope of an `ExpectedEvent`, even if `in_scope` hadn't been called. In this case, that would check that an event was not in a span if `in_scope` had not been called. `tracing-subscriber` all adhere to this pattern. However it is different to the behavior of all other expectation methods, where an explicit call is needed to expect something, otherwise nothing is checked. As such, the behavior has been modified to align with the rest of the crate. The previous behavior can be achieved by calling `in_scope(None)` to verify that an event has no scope. The documentation for `in_scope` has been updated with an example for this case. The tests in `tracing-subscriber` which previously verified *implicitly* that an event had no scope (by not calling `in_scope` at all) have *not* been modified. It is my opinion that this implicit behavior was never required. Refs: #539
1 parent e8b3795 commit cbcfbf9

File tree

3 files changed

+778
-85
lines changed

3 files changed

+778
-85
lines changed

tracing-mock/src/event.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! ```
1111
//! use tracing::subscriber::with_default;
12-
//! use tracing_mock::{subscriber, expect};
12+
//! use tracing_mock::{expect, subscriber};
1313
//!
1414
//! let event = expect::event()
1515
//! .at_level(tracing::Level::INFO)
@@ -43,7 +43,7 @@ use std::fmt;
4343
pub struct ExpectedEvent {
4444
pub(super) fields: Option<field::ExpectedFields>,
4545
pub(super) parent: Option<Parent>,
46-
pub(super) in_spans: Vec<span::ExpectedSpan>,
46+
pub(super) in_spans: Option<Vec<span::ExpectedSpan>>,
4747
pub(super) metadata: ExpectedMetadata,
4848
}
4949

@@ -451,30 +451,28 @@ impl ExpectedEvent {
451451
/// recorded event, the expectation will fail.
452452
///
453453
/// **Note**: This validation currently only works with a
454-
/// [`MockSubscriber`]. If used with a [`MockCollector`], the
454+
/// [`MockLayer`]. If used with a [`MockSubscriber`], the
455455
/// expectation will fail directly as it is unimplemented.
456456
///
457457
/// # Examples
458458
///
459459
/// ```
460-
/// use tracing_mock::{expect, subscriber};
461-
/// use tracing_subscriber::{
462-
/// filter::filter_fn, registry, subscribe::CollectExt, util::SubscriberInitExt, Subscribe,
463-
/// };
460+
/// use tracing_mock::{expect, layer};
461+
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
464462
///
465463
/// let event = expect::event().in_scope([
466464
/// expect::span().named("parent_span"),
467465
/// expect::span().named("grandparent_span")
468466
/// ]);
469467
///
470-
/// let (subscriber, handle) = subscriber::mock()
468+
/// let (layer, handle) = layer::mock()
471469
/// .enter(expect::span())
472470
/// .enter(expect::span())
473471
/// .event(event)
474472
/// .run_with_handle();
475473
///
476-
/// let _subscriber = registry()
477-
/// .with(subscriber.with_filter(filter_fn(move |_meta| true)))
474+
/// let _subscriber = tracing_subscriber::registry()
475+
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
478476
/// .set_default();
479477
///
480478
/// let grandparent = tracing::info_span!("grandparent_span");
@@ -489,23 +487,21 @@ impl ExpectedEvent {
489487
/// The scope must match exactly, otherwise the expectation will fail:
490488
///
491489
/// ```should_panic
492-
/// use tracing_mock::{expect, subscriber};
493-
/// use tracing_subscriber::{
494-
/// filter::filter_fn, registry, subscribe::CollectExt, util::SubscriberInitExt, Subscribe,
495-
/// };
490+
/// use tracing_mock::{expect, layer};
491+
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
496492
///
497493
/// let event = expect::event().in_scope([
498494
/// expect::span().named("parent_span"),
499495
/// expect::span().named("grandparent_span")
500496
/// ]);
501497
///
502-
/// let (subscriber, handle) = subscriber::mock()
498+
/// let (layer, handle) = layer::mock()
503499
/// .enter(expect::span())
504500
/// .event(event)
505501
/// .run_with_handle();
506502
///
507-
/// let _subscriber = registry()
508-
/// .with(subscriber.with_filter(filter_fn(move |_meta| true)))
503+
/// let _subscriber = tracing_subscriber::registry()
504+
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
509505
/// .set_default();
510506
///
511507
/// let parent = tracing::info_span!("parent_span");
@@ -514,21 +510,48 @@ impl ExpectedEvent {
514510
///
515511
/// handle.assert_finished();
516512
/// ```
513+
///
514+
/// It is also possible to test that an event has no parent spans
515+
/// by passing `None` to `in_scope`. If the event is within a
516+
/// span, the test will fail:
517+
///
518+
/// ```should_panic
519+
/// use tracing_mock::{expect, layer};
520+
/// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
521+
///
522+
/// let event = expect::event().in_scope(None);
523+
///
524+
/// let (layer, handle) = layer::mock()
525+
/// .enter(expect::span())
526+
/// .event(event)
527+
/// .run_with_handle();
528+
///
529+
/// let _subscriber = tracing_subscriber::registry()
530+
/// .with(layer.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
531+
/// .set_default();
532+
///
533+
/// let parent = tracing::info_span!("parent_span");
534+
/// let _guard = parent.enter();
535+
/// tracing::info!(field = &"value");
536+
///
537+
/// handle.assert_finished();
538+
/// ```
539+
///
540+
/// [`MockLayer`]: struct@crate::layer::MockLayer
517541
/// [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
518-
/// [`MockCollector`]: struct@crate::subscriber::MockCollector
519542
#[cfg(feature = "tracing-subscriber")]
520543
pub fn in_scope(self, spans: impl IntoIterator<Item = span::ExpectedSpan>) -> Self {
521544
Self {
522-
in_spans: spans.into_iter().collect(),
545+
in_spans: Some(spans.into_iter().collect()),
523546
..self
524547
}
525548
}
526549

527550
/// Provides access to the expected scope (spans) for this expected
528551
/// event.
529552
#[cfg(feature = "tracing-subscriber")]
530-
pub(crate) fn scope_mut(&mut self) -> &mut [span::ExpectedSpan] {
531-
&mut self.in_spans[..]
553+
pub(crate) fn scope_mut(&mut self) -> Option<&mut [span::ExpectedSpan]> {
554+
self.in_spans.as_mut().map(|s| &mut s[..])
532555
}
533556

534557
pub(crate) fn check(
@@ -596,8 +619,8 @@ impl fmt::Debug for ExpectedEvent {
596619
s.field("parent", &format_args!("{:?}", parent));
597620
}
598621

599-
if !self.in_spans.is_empty() {
600-
s.field("in_spans", &self.in_spans);
622+
if let Some(in_spans) = &self.in_spans {
623+
s.field("in_spans", in_spans);
601624
}
602625

603626
s.finish()

0 commit comments

Comments
 (0)