Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: add README to tracing-mock #2362

Merged
merged 23 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c8fa221
mock: add README to tracing-mock
hds Oct 25, 2022
0ca7f8e
Fix format
hds Nov 2, 2022
651523b
Update tracing-mock/README.md
hds Nov 3, 2022
149c80e
Update tracing-mock/README.md
hds Nov 3, 2022
3332e6e
Update tracing-mock/README.md
hds Nov 3, 2022
b0ce9dc
Update tracing-mock/README.md
hds Nov 3, 2022
6214e13
Suggestions from @davidbarsky
hds Nov 3, 2022
655309a
Added missing whitespace before Examples heading
hds Nov 3, 2022
1e6a2ff
Check for drop span in README example
hds Nov 3, 2022
888b76a
Call out that we use `.done` in the README example
hds Nov 3, 2022
71a4d5e
Merge branch 'master' into hds/tracing-mock-readme
hawkw Nov 4, 2022
2385de2
include README in lib.rs and run tests from there
hds Nov 4, 2022
24a2624
Merge branch 'master' of github.com:tokio-rs/tracing into hds/tracing…
hds Nov 4, 2022
121f79c
replace deprecated drop_span with close_span
hds Nov 7, 2022
d0fec28
updated README description to say span is closed
hds Nov 7, 2022
cf94610
Update tracing-mock/README.md
hds Nov 8, 2022
cad1fb1
Changes based on suggestions from @hawkw
hds Nov 8, 2022
4686cda
Import examples refers to tracing 0.1 ecosystem
hds Nov 8, 2022
06cc572
Merge branch 'master' into hds/tracing-mock-readme
hawkw Nov 8, 2022
bc1a7a7
Merge branch 'master' into hds/tracing-mock-readme
hawkw Nov 11, 2022
8e8f41a
Update tracing-mock/README.md
hds Nov 15, 2022
05bdb42
adapted examples to `expect::thing`
hds Nov 15, 2022
0124328
Merge branch 'master' into hds/tracing-mock-readme
hds Nov 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tracing-mock/Cargo.toml
Expand Up @@ -18,7 +18,7 @@ rust-version = "1.49.0"
publish = false

[dependencies]
tracing = { path = "../tracing", version = "0.2", default-features = false }
tracing = { path = "../tracing", version = "0.2" }
tracing-core = { path = "../tracing-core", version = "0.2", default-features = false }
tokio-test = { version = "0.4.2", optional = true }

Expand Down
176 changes: 176 additions & 0 deletions tracing-mock/README.md
@@ -0,0 +1,176 @@
![Tracing — Structured, application-level diagnostics][splash]

[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg

# tracing-mock

Utilities for testing [`tracing`][tracing] and crates that uses it.

[![Documentation (master)][docs-master-badge]][docs-master-url]
[![MIT licensed][mit-badge]][mit-url]
[![Build Status][actions-badge]][actions-url]
[![Discord chat][discord-badge]][discord-url]

[Documentation][docs-master-url] | [Chat][discord-url]

[docs-master-badge]: https://img.shields.io/badge/docs-master-blue
[docs-master-url]: https://tracing-rs.netlify.com/tracing_mock
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: LICENSE
hds marked this conversation as resolved.
Show resolved Hide resolved
[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg
[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI
[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white
[discord-url]: https://discord.gg/EeF3cQw

## Overview

[`tracing`] is a framework for instrumenting Rust programs to collect
structured, event-based diagnostic information. `tracing-mock` provides
mock `tracing` objects that are useful in testing `tracing` itself
and crates that use `tracing`.
hds marked this conversation as resolved.
Show resolved Hide resolved

*Compiler support: [requires `rustc` 1.49+][msrv]*

[msrv]: #supported-rust-versions

## Usage

`tracing-mock` crate provides a mock `Subscriber` that allows asserting on
the order and contents of spans and events.
hawkw marked this conversation as resolved.
Show resolved Hide resolved

As `tracing-mock` isn't available on [crates.io](https://crates.io/)
yet, you must import it via git. It is important that you also override
the source of any `tracing` crates that are transient dependencies. For
example, the `Cargo.toml` for your test crate could contain:

```toml
[dependencies]
lib-under-test = 1.0 # depends on `tracing`
hds marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
tracing-mock = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }
tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }

[patch.crates-io]
tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }
hds marked this conversation as resolved.
Show resolved Hide resolved
tracing-core = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }
```
hds marked this conversation as resolved.
Show resolved Hide resolved

hds marked this conversation as resolved.
Show resolved Hide resolved
## Examples

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

```rust
use tracing::subscriber::with_default;
use tracing_mock::{event, field, subscriber};

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

#[test]
fn traced_event() {
let (subscriber, handle) = subscriber::mock()
.event(event::mock().with_fields(field::msg("preparing to shave yaks")))
.done()
.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
- the span is dropped
- no further traces are received

```rust
use tracing::subscriber::with_default;
use tracing_mock::{event, field, span, subscriber};

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

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

#[test]
fn yak_shaving_traced() {
let yak_count: u32 = 3;
let span = span::mock().named("yak_shaving");

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

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

handle.assert_finished();
}
```

The full code for both examples can be found in the readme.rs tests.
hds marked this conversation as resolved.
Show resolved Hide resolved

## Supported Rust Versions

Tracing is built against the latest stable release. The minimum supported
version is 1.49. 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.45, the minimum supported version will not be increased
past 1.42, 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](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.
78 changes: 78 additions & 0 deletions tracing-mock/tests/readme.rs
@@ -0,0 +1,78 @@
use tracing::collect::with_default;
hds marked this conversation as resolved.
Show resolved Hide resolved
use tracing_mock::{collector, event, field, span};

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

// If this test gets updated, the `tracing-mock` README.md must be updated too.
#[test]
fn prepare_yak_shaving_traced() {
let (subscriber, handle) = collector::mock()
.event(event::mock().with_fields(field::msg("preparing to shave yaks")))
.done()
.run_with_handle();

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

handle.assert_finished();
}

mod yak_shave {
pub fn shave_all(number_of_yaks: u32) -> u32 {
number_of_yaks
}
}

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

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

// If this test gets updated, the `tracing-mock` README.md must be updated too.
#[test]
fn yak_shaving_traced() {
let yak_count: u32 = 3;
let span = span::mock().named("yak_shaving");

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

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

handle.assert_finished();
}