๐Ÿ›ˆ Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

For the release documentation, please see docs.rs, instead.

tracing_mock::event

Struct ExpectedEvent

Source
pub struct ExpectedEvent { /* private fields */ }
Expand description

An expected event.

For a detailed description and examples, see the documentation for the methods and the event module.

Implementationsยง

Sourceยง

impl ExpectedEvent

Source

pub fn named<I>(self, name: I) -> Self
where I: Into<String>,

Sets a name to expect when matching an event.

By default, an eventโ€™s name takes takes the form: event <file>:<line> where <file> and <line> refer to the location in the source code where the event was generated.

To override the name of an event, it has to be constructed directly, rather than by using the tracing crateโ€™s macros.

In general, there are not many use cases for expecting an event with a particular name, as the value includes the file name and line number. Assertions about event names are therefore quite fragile, since they will change as the source code is modified.

Source

pub fn with_fields<I>(self, fields: I) -> Self
where I: Into<ExpectedFields>,

Adds fields to expect when matching an event.

If an event is recorded with fields that do not match the provided ExpectedFields, this expectation will fail.

If the provided field is not present on the recorded event, or if the value for that field is different, then the expectation will fail.

More information on the available validations is available in the ExpectedFields documentation.

ยงExamples
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_fields(expect::field("field.name").with_value(&"field_value"));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::info!(field.name = "field_value");
});

handle.assert_finished();

A different field value will cause the expectation to fail:

โ“˜
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_fields(expect::field("field.name").with_value(&"field_value"));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::info!(field.name = "different_field_value");
});

handle.assert_finished();
Source

pub fn at_level(self, level: Level) -> Self

Sets the Level to expect when matching an event.

If an event is recorded at a different level, this expectation will fail.

ยงExamples
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .at_level(tracing::Level::WARN);

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::warn!("this message is bad news");
});

handle.assert_finished();

Expecting an event at INFO level will fail if the event is recorded at any other level:

โ“˜
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .at_level(tracing::Level::INFO);

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::warn!("this message is bad news");
});

handle.assert_finished();
Source

pub fn with_target<I>(self, target: I) -> Self
where I: Into<String>,

Sets the target to expect when matching events.

If an event is recorded with a different target, this expectation will fail.

ยงExamples
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_target("some_target");

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::info!(target: "some_target", field = &"value");
});

handle.assert_finished();

The test will fail if the target is different:

โ“˜
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_target("some_target");

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::info!(target: "a_different_target", field = &"value");
});

handle.assert_finished();
Source

pub fn with_ancestry(self, ancenstry: ExpectedAncestry) -> ExpectedEvent

Configures this ExpectedEvent to expect the specified ExpectedAncestry. An eventโ€™s ancestry indicates whether is has a parent or is a root, and whether the parent is explicitly or contextually assigned.

An explicit parent span is one passed to the event! macro in the parent: field. If no parent: field is specified, then the event will have a contextually determined parent or be a contextual root if there is no parent.

If the parent is different from the provided one, this expectation will fail.

ยงExamples

An explicit or contextual can be matched on an ExpectedSpan.

use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let parent = expect::span()
    .named("parent_span")
    .with_target("custom-target")
    .at_level(tracing::Level::INFO);
let event = expect::event()
    .with_ancestry(expect::has_explicit_parent(parent));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    let parent = tracing::info_span!(target: "custom-target", "parent_span");
    tracing::info!(parent: parent.id(), field = &"value");
});

handle.assert_finished();

The functions expect::has_explicit_parent and expect::has_contextual_parent take Into<ExpectedSpan>, so a string passed directly will match on a span with that name, or an ExpectedId can be passed to match a span with that Id.

use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_ancestry(expect::has_explicit_parent("parent_span"));

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    let parent = tracing::info_span!("parent_span");
    tracing::info!(parent: parent.id(), field = &"value");
});

handle.assert_finished();

In the following example, we expect that the matched event is an explicit root:

use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_ancestry(expect::is_explicit_root());

let (collector, handle) = collector::mock()
    .enter(expect::span())
    .event(event)
    .run_with_handle();

with_default(collector, || {
    let _guard = tracing::info_span!("contextual parent").entered();
    tracing::info!(parent: None, field = &"value");
});

handle.assert_finished();

When expect::has_contextual_parent("parent_name") is passed to with_ancestry then the provided string is the name of the contextual parent span to expect.

use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_ancestry(expect::has_contextual_parent("parent_span"));

let (collector, handle) = collector::mock()
    .enter(expect::span())
    .event(event)
    .run_with_handle();

with_default(collector, || {
    let parent = tracing::info_span!("parent_span");
    let _guard = parent.enter();
    tracing::info!(field = &"value");
});

handle.assert_finished();

Matching an event recorded outside of a span, a contextual root:

use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_ancestry(expect::is_contextual_root());

let (collector, handle) = collector::mock()
    .event(event)
    .run_with_handle();

with_default(collector, || {
    tracing::info!(field = &"value");
});

handle.assert_finished();

In the example below, the expectation fails because the event is recorded with an explicit parent, however a contextual parent is expected.

โ“˜
use tracing::collect::with_default;
use tracing_mock::{collector, expect};

let event = expect::event()
    .with_ancestry(expect::has_contextual_parent("parent_span"));

let (collector, handle) = collector::mock()
    .enter(expect::span())
    .event(event)
    .run_with_handle();

with_default(collector, || {
    let parent = tracing::info_span!("parent_span");
    tracing::info!(parent: parent.id(), field = &"value");
});

handle.assert_finished();
Source

pub fn in_scope(self, spans: impl IntoIterator<Item = ExpectedSpan>) -> Self

Validates that the event is emitted within the scope of the provided spans.

The spans must be provided reverse hierarchy order, so the closest span to the event would be first, followed by its parent, and so on.

If the spans provided do not match the hierarchy of the recorded event, the expectation will fail.

Note: This validation currently only works with a MockSubscriber. If used with a MockCollector, the expectation will fail directly as it is unimplemented.

ยงExamples
use tracing_mock::{expect, subscriber};
use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};

let event = expect::event().in_scope([
    expect::span().named("parent_span"),
    expect::span().named("grandparent_span")
]);

let (subscriber, handle) = subscriber::mock()
    .enter(expect::span())
    .enter(expect::span())
    .event(event)
    .run_with_handle();

let _collect = tracing_subscriber::registry()
    .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
    .set_default();

let grandparent = tracing::info_span!("grandparent_span");
let _gp_guard = grandparent.enter();
let parent = tracing::info_span!("parent_span");
let _p_guard = parent.enter();
tracing::info!(field = &"value");    

handle.assert_finished();

The scope must match exactly, otherwise the expectation will fail:

โ“˜
use tracing_mock::{expect, subscriber};
use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};

let event = expect::event().in_scope([
    expect::span().named("parent_span"),
    expect::span().named("grandparent_span")
]);

let (subscriber, handle) = subscriber::mock()
    .enter(expect::span())
    .event(event)
    .run_with_handle();

let _collect = tracing_subscriber::registry()
    .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
    .set_default();

let parent = tracing::info_span!("parent_span");
let _p_guard = parent.enter();
tracing::info!(field = &"value");

handle.assert_finished();

It is also possible to test that an event has no parent spans by passing None to in_scope. If the event is within a span, the test will fail:

โ“˜
use tracing_mock::{expect, subscriber};
use tracing_subscriber::{subscribe::CollectExt, util::SubscriberInitExt, Subscribe};

let event = expect::event().in_scope(None);

let (subscriber, handle) = subscriber::mock()
    .enter(expect::span())
    .event(event)
    .run_with_handle();

let _collect = tracing_subscriber::registry()
    .with(subscriber.with_filter(tracing_subscriber::filter::filter_fn(move |_meta| true)))
    .set_default();

let parent = tracing::info_span!("parent_span");
let _guard = parent.enter();
tracing::info!(field = &"value");    

handle.assert_finished();

Trait Implementationsยง

Sourceยง

impl Debug for ExpectedEvent

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl Default for ExpectedEvent

Sourceยง

fn default() -> ExpectedEvent

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl Display for ExpectedEvent

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl PartialEq for ExpectedEvent

Sourceยง

fn eq(&self, other: &ExpectedEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl Eq for ExpectedEvent

Sourceยง

impl StructuralPartialEq for ExpectedEvent

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> 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> ToString for T
where T: Display + ?Sized,

Sourceยง

fn to_string(&self) -> String

Converts the given value to a String. 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<T> WithCollector for T

Sourceยง

fn with_collector<C>(self, collector: C) -> WithDispatch<Self>
where C: Into<Dispatch>,

Attaches the provided collector to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_collector(self) -> WithDispatch<Self>

Attaches the current default collector to this type, returning a WithDispatch wrapper. Read more