Skip to content

Commit 9c6ffce

Browse files
hdsdavidbarskyhawkw
committed
mock: add README to tracing-mock (#2362)
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. Specifically **I** want to have access to `tracing-mock` within parts of `tokio` to write tests that ensure that the correct calling location is picked up for all `spawn*` functions when the `tracing` feature is enabled. This change starts that process by adding a README for `tracing-mock`. The README follows the standard format for all `tracing` crates and includes 2 examples. The README is included in the `lib.rs` docs using `#[doc = include_str!(...)]`, so that the same documentation is included in the crate, and the examples are tested when running doctests. The README describes steps when using the `tracing` 1.0 from crates.io and `tracing-mock` from the `v0.1.x` branch. The tests target the current branch. Co-authored-by: David Barsky <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
1 parent dc636df commit 9c6ffce

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

tracing-mock/README.md

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
![Tracing — Structured, application-level diagnostics][splash]
2+
3+
[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg
4+
5+
# tracing-mock
6+
7+
Utilities for testing [`tracing`][tracing] and crates that uses it.
8+
9+
[![Documentation (master)][docs-master-badge]][docs-master-url]
10+
[![MIT licensed][mit-badge]][mit-url]
11+
[![Build Status][actions-badge]][actions-url]
12+
[![Discord chat][discord-badge]][discord-url]
13+
14+
[Documentation][docs-master-url] | [Chat][discord-url]
15+
16+
[docs-master-badge]: https://img.shields.io/badge/docs-master-blue
17+
[docs-master-url]: https://tracing-rs.netlify.com/tracing_mock
18+
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
19+
[mit-url]: https://github.com/tokio-rs/tracing/blob/master/tracing-mock/LICENSE
20+
[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg
21+
[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI
22+
[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white
23+
[discord-url]: https://discord.gg/EeF3cQw
24+
25+
## Overview
26+
27+
[`tracing`] is a framework for instrumenting Rust programs to collect
28+
structured, event-based diagnostic information. `tracing-mock` provides
29+
tools for making assertions about what `tracing` diagnostics are emitted
30+
by code under test.
31+
32+
*Compiler support: [requires `rustc` 1.49+][msrv]*
33+
34+
[msrv]: #supported-rust-versions
35+
36+
## Usage
37+
38+
`tracing-mock` crate provides a mock
39+
[`Collector`](https://tracing-rs.netlify.app/tracing/#collectors)
40+
that allows asserting on the order and contents of
41+
[spans](https://tracing-rs.netlify.app/tracing/#spans) and
42+
[events](https://tracing-rs.netlify.app/tracing/#events).
43+
44+
As `tracing-mock` isn't available on [crates.io](https://crates.io/)
45+
yet, you must import it via git. When using `tracing-mock` with the
46+
`tracing` `0.1` ecosystem, it is important that you also override the
47+
source of any `tracing` crates that are transient dependencies. For
48+
example, the `Cargo.toml` for your test crate could contain:
49+
50+
```toml
51+
[dependencies]
52+
lib-under-test = "1.0" # depends on `tracing`
53+
54+
[dev-dependencies]
55+
tracing-mock = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }
56+
tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x", version = "0.1" }
57+
58+
[patch.crates-io]
59+
tracing = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }
60+
tracing-core = { git = "https://github.com/tokio-rs/tracing", branch = "v0.1.x" }
61+
```
62+
63+
## Examples
64+
65+
The following examples are for the `master` branch. For examples that
66+
will work with `tracing` from [crates.io], please check the
67+
[v0.1.x](https://github.com/tokio-rs/tracing/tree/v0.1.x/tracing-mock)
68+
branch.
69+
70+
Below is an example that checks that an event contains a message:
71+
72+
```rust
73+
use tracing::collect::with_default;
74+
use tracing_mock::{collector, expect, field};
75+
76+
fn yak_shaving() {
77+
tracing::info!("preparing to shave yaks");
78+
}
79+
80+
let (collector, handle) = collector::mock()
81+
.event(expect::event().with_fields(field::msg("preparing to shave yaks")))
82+
.only()
83+
.run_with_handle();
84+
85+
with_default(collector, || {
86+
yak_shaving();
87+
});
88+
89+
handle.assert_finished();
90+
91+
```
92+
93+
Below is a slightly more complex example. `tracing-mock` asserts that, in order:
94+
- a span is created with a single field/value pair
95+
- the span is entered
96+
- an event is created with the field `number_of_yaks`, a corresponding
97+
value of 3, and the message "preparing to shave yaks", and nothing else
98+
- an event is created with the field `all_yaks_shaved`, a corresponding value
99+
of `true`, and the message "yak shaving completed"
100+
- the span is exited
101+
- no further traces are received
102+
103+
```rust
104+
use tracing::collect::with_default;
105+
use tracing_mock::{collector, expect, field};
106+
107+
#[tracing::instrument]
108+
fn yak_shaving(number_of_yaks: u32) {
109+
tracing::info!(number_of_yaks, "preparing to shave yaks");
110+
111+
let number_shaved = number_of_yaks; // shave_all
112+
tracing::info!(
113+
all_yaks_shaved = number_shaved == number_of_yaks,
114+
"yak shaving completed."
115+
);
116+
}
117+
118+
let yak_count: u32 = 3;
119+
let span = expect::span().named("yak_shaving");
120+
121+
let (collector, handle) = collector::mock()
122+
.new_span(
123+
span.clone()
124+
.with_field(expect::field("number_of_yaks").with_value(&yak_count).only()),
125+
)
126+
.enter(span.clone())
127+
.event(
128+
expect::event().with_fields(
129+
expect::field("number_of_yaks")
130+
.with_value(&yak_count)
131+
.and(field::msg("preparing to shave yaks"))
132+
.only(),
133+
),
134+
)
135+
.event(
136+
expect::event().with_fields(
137+
expect::field("all_yaks_shaved")
138+
.with_value(&true)
139+
.and(field::msg("yak shaving completed."))
140+
.only(),
141+
),
142+
)
143+
.exit(span.clone())
144+
.only()
145+
.run_with_handle();
146+
147+
with_default(collector, || {
148+
yak_shaving(yak_count);
149+
});
150+
151+
handle.assert_finished();
152+
```
153+
154+
## Supported Rust Versions
155+
156+
Tracing is built against the latest stable release. The minimum supported
157+
version is 1.49. The current Tracing version is not guaranteed to build on Rust
158+
versions earlier than the minimum supported version.
159+
160+
Tracing follows the same compiler support policies as the rest of the Tokio
161+
project. The current stable Rust compiler and the three most recent minor
162+
versions before it will always be supported. For example, if the current stable
163+
compiler version is 1.45, the minimum supported version will not be increased
164+
past 1.42, three minor versions prior. Increasing the minimum supported compiler
165+
version is not considered a semver breaking change as long as doing so complies
166+
with this policy.
167+
168+
## License
169+
170+
This project is licensed under the [MIT license][mit-url].
171+
172+
### Contribution
173+
174+
Unless you explicitly state otherwise, any contribution intentionally submitted
175+
for inclusion in Tracing by you, shall be licensed as MIT, without any additional
176+
terms or conditions.

tracing-mock/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![doc = include_str!("../README.md")]
12
use std::{
23
pin::Pin,
34
task::{Context, Poll},

0 commit comments

Comments
 (0)