tracing-mock

Crates.iotracing-mock
lib.rstracing-mock
version
sourcesrc
created_at2024-11-29 17:22:57.311865
updated_at2024-11-29 17:22:57.311865
descriptionUtilities for testing `tracing` and crates that uses it.
homepagehttps://tokio.rs
repositoryhttps://github.com/tokio-rs/tracing
max_upload_size
id1465843
Cargo.toml error:TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Eliza Weisman (hawkw)

documentation

README

Tracing — Structured, application-level diagnostics

tracing-mock

Utilities for testing tracing and crates that uses it.

Crates.io Documentation Documentation (master) MIT licensed Build Status Discord chat

Documentation | Chat

Overview

tracing is a framework for instrumenting Rust programs to collect structured, event-based diagnostic information. tracing-mock provides tools for making assertions about what tracing diagnostics are emitted by code under test.

Compiler support: requires rustc 1.63+

Usage

The tracing-mock crate provides a mock Subscriber that allows asserting on the order and contents of spans and events.

To get started with tracing-mock, check the documentation in the subscriber module and MockSubscriber struct.

While tracing-mock is in beta, it is recommended that an exact version is specified in the cargo manifest. Otherwise, cargo update will take the latest beta version, which may contain breaking changes compared to previous betas.

To do so, add the following to Cargo.toml:

[dependencies]
tracing-mock = "= 0.1.0-beta.1"

Examples

Below is an example that checks that an event contains a message:

use tracing::subscriber::with_default;
use tracing_mock::{expect, subscriber};

fn yak_shaving() {
    tracing::info!("preparing to shave yaks");
}

let (subscriber, handle) = subscriber::mock()
    .event(expect::event().with_fields(expect::msg("preparing to shave yaks")))
    .only()
    .run_with_handle();

with_default(subscriber, || {
    yak_shaving();
});

handle.assert_finished();

Below is a slightly more complex example. tracing-mock asserts that, in order:

  • a span is created with a single field/value pair
  • the span is entered
  • an event is created with the field number_of_yaks, a corresponding value of 3, and the message "preparing to shave yaks", and nothing else
  • an event is created with the field all_yaks_shaved, a corresponding value of true, and the message "yak shaving completed"
  • the span is exited
  • no further traces are received
use tracing::subscriber::with_default;
use tracing_mock::{expect, subscriber};

#[tracing::instrument]
fn yak_shaving(number_of_yaks: u32) {
    tracing::info!(number_of_yaks, "preparing to shave yaks");

    let number_shaved = number_of_yaks; // shave_all
    tracing::info!(
        all_yaks_shaved = number_shaved == number_of_yaks,
        "yak shaving completed."
    );
}

let yak_count: u32 = 3;
let span = expect::span().named("yak_shaving");

let (subscriber, handle) = subscriber::mock()
    .new_span(
        span.clone()
            .with_fields(expect::field("number_of_yaks").with_value(&yak_count).only()),
    )
    .enter(span.clone())
    .event(
        expect::event().with_fields(
            expect::field("number_of_yaks")
                .with_value(&yak_count)
                .and(expect::msg("preparing to shave yaks"))
                .only(),
        ),
    )
    .event(
        expect::event().with_fields(
            expect::field("all_yaks_shaved")
                .with_value(&true)
                .and(expect::msg("yak shaving completed."))
                .only(),
        ),
    )
    .exit(span.clone())
    .only()
    .run_with_handle();

with_default(subscriber, || {
    yak_shaving(yak_count);
});

handle.assert_finished();

Supported Rust Versions

Tracing is built against the latest stable release. The minimum supported version is 1.63. The current Tracing version is not guaranteed to build on Rust versions earlier than the minimum supported version.

Tracing follows the same compiler support policies as the rest of the Tokio project. The current stable Rust compiler and the three most recent minor versions before it will always be supported. For example, if the current stable compiler version is 1.69, the minimum supported version will not be increased past 1.66, three minor versions prior. Increasing the minimum supported compiler version is not considered a semver breaking change as long as doing so complies with this policy.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tracing by you, shall be licensed as MIT, without any additional terms or conditions.

Commit count: 1413

cargo fmt