🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

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

Module tracing_mock::span

source ·
Expand description

Define expectations to match and validate spans.

The ExpectedSpan and NewSpan structs define expectations for spans to be matched by the mock collector API in the collector module.

Expected spans should be created with expect::span and a chain of method calls describing the assertions made about the span. Expectations about the lifecycle of the span can be set on the MockCollector.

§Examples

use tracing_mock::{collector, expect};

let span = expect::span()
    .named("interesting_span")
    .at_level(tracing::Level::INFO);

let (collector, handle) = collector::mock()
    .enter(span.clone())
    .exit(span)
    .run_with_handle();

tracing::collect::with_default(collector, || {
   let span = tracing::info_span!("interesting_span");
    let _guard = span.enter();
});

handle.assert_finished();

The following example asserts the name, level, parent, and fields of the span:

use tracing_mock::{collector, expect};

let span = expect::span()
    .named("interesting_span")
    .at_level(tracing::Level::INFO);
let new_span = span
    .clone()
    .with_fields(expect::field("field.name").with_value(&"field_value"))
    .with_explicit_parent(Some("parent_span"));

let (collector, handle) = collector::mock()
    .new_span(expect::span().named("parent_span"))
    .new_span(new_span)
    .enter(span.clone())
    .exit(span)
    .run_with_handle();

tracing::collect::with_default(collector, || {
    let parent = tracing::info_span!("parent_span");

    let span = tracing::info_span!(
        parent: parent.id(),
        "interesting_span",
        field.name = "field_value",
    );
    let _guard = span.enter();
});

handle.assert_finished();

All expectations must be met for the test to pass. For example, the following test will fail due to a mismatch in the spans’ names:

use tracing_mock::{collector, expect};

let span = expect::span()
    .named("interesting_span")
    .at_level(tracing::Level::INFO);

let (collector, handle) = collector::mock()
    .enter(span.clone())
    .exit(span)
    .run_with_handle();

tracing::collect::with_default(collector, || {
   let span = tracing::info_span!("another_span");
   let _guard = span.enter();
});

handle.assert_finished();

Structs§

Functions§