Skip to content

Commit 014c6eb

Browse files
Swatinemhawkw
authored andcommitted
tracing: add static level tests for spans and instrumented fns (#1872)
The current tests for the compile-time maximum level were not being run in `--release` mode, and did not previously assert anything about `Span`s. This also adds some test cases for various `#[instrument]`-ed functions.
1 parent f5ad56a commit 014c6eb

File tree

4 files changed

+230
-102
lines changed

4 files changed

+230
-102
lines changed

.github/workflows/CI.yml

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -127,56 +127,6 @@ jobs:
127127
working-directory: ${{ matrix.subcrate }}
128128
run: cargo hack check --feature-powerset --no-dev-deps
129129

130-
cargo-check-tracing:
131-
runs-on: ubuntu-latest
132-
strategy:
133-
matrix:
134-
featureset:
135-
- ""
136-
- async-await
137-
- async-await std
138-
- async-await log-always
139-
- async-await std log-always
140-
- log-always
141-
- std log-always
142-
- std
143-
fail-fast: false
144-
steps:
145-
- uses: actions/checkout@main
146-
- uses: actions-rs/toolchain@v1
147-
with:
148-
toolchain: stable
149-
profile: minimal
150-
override: true
151-
- name: cargo check
152-
working-directory: tracing
153-
run: cargo check --no-default-features --features "${{ matrix.featureset }}"
154-
155-
cargo-check-subscriber:
156-
runs-on: ubuntu-latest
157-
strategy:
158-
matrix:
159-
featureset:
160-
- ""
161-
- fmt
162-
- fmt ansi
163-
- fmt json
164-
- fmt json ansi
165-
- fmt registry
166-
- fmt env-filter
167-
- registry
168-
- env-filter
169-
steps:
170-
- uses: actions/checkout@main
171-
- uses: actions-rs/toolchain@v1
172-
with:
173-
toolchain: stable
174-
profile: minimal
175-
override: true
176-
- name: cargo check
177-
working-directory: tracing-subscriber
178-
run: cargo check --no-default-features --features "${{ matrix.featureset }}"
179-
180130
test-versions:
181131
# Test against the stable, beta, and nightly Rust toolchains on ubuntu-latest.
182132
needs: check
@@ -289,45 +239,6 @@ jobs:
289239
command: test
290240
args: --all --features "valuable"
291241

292-
293-
features-stable:
294-
# Feature flag tests that run on stable Rust.
295-
# TODO(david): once tracing's MSRV goes up to Rust 1.51, we should be able to switch to
296-
# using cargo's V2 feature resolver (https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions)
297-
# and avoid cd'ing into each crate's directory.
298-
needs: check
299-
runs-on: ubuntu-latest
300-
steps:
301-
- uses: actions/checkout@main
302-
- uses: actions-rs/toolchain@v1
303-
with:
304-
toolchain: stable
305-
profile: minimal
306-
override: true
307-
- name: "Test log support"
308-
run: cargo test
309-
working-directory: "tracing/test-log-support"
310-
- name: "Test static max level"
311-
run: cargo test
312-
working-directory: "tracing/test_static_max_level_features"
313-
- name: "Test tracing-core no-std support"
314-
run: cargo test --no-default-features
315-
working-directory: tracing-core
316-
- name: "Test tracing no-std support"
317-
run: cargo test --lib --tests --no-default-features
318-
working-directory: tracing
319-
# this skips running doctests under the `--no-default-features` flag,
320-
# as rustdoc isn't aware of cargo's feature flags.
321-
- name: "Test tracing-subscriber no-std support"
322-
run: cargo test --lib --tests --no-default-features
323-
working-directory: tracing-subscriber
324-
- name: "Test tracing-subscriber with liballoc only"
325-
run: cargo test --lib --tests --no-default-features --features "alloc"
326-
working-directory: tracing-subscriber
327-
- name: "Test tracing-subscriber with no default features"
328-
run: cargo test --lib --tests --no-default-features --features "std"
329-
working-directory: tracing-subscriber
330-
331242
style:
332243
# Check style.
333244
needs: check
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
name: Check Features
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- "v0.1.x"
8+
pull_request: {}
9+
10+
env:
11+
# Disable incremental compilation.
12+
#
13+
# Incremental compilation is useful as part of an edit-build-test-edit cycle,
14+
# as it lets the compiler avoid recompiling code that hasn't changed. However,
15+
# on CI, we're not making small edits; we're almost always building the entire
16+
# project from scratch. Thus, incremental compilation on CI actually
17+
# introduces *additional* overhead to support making future builds
18+
# faster...but no future builds will ever occur in any given CI environment.
19+
#
20+
# See https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
21+
# for details.
22+
CARGO_INCREMENTAL: 0
23+
# Allow more retries for network requests in cargo (downloading crates) and
24+
# rustup (installing toolchains). This should help to reduce flaky CI failures
25+
# from transient network timeouts or other issues.
26+
CARGO_NET_RETRY: 10
27+
RUSTUP_MAX_RETRIES: 10
28+
# Don't emit giant backtraces in the CI logs.
29+
RUST_BACKTRACE: short
30+
31+
jobs:
32+
cargo-hack:
33+
runs-on: ubuntu-latest
34+
strategy:
35+
matrix:
36+
# cargo hack --feature-powerset will have a significant permutation
37+
# number, we can't just use --all as it increases the runtime
38+
# further than what we would like to
39+
subcrate:
40+
- tracing-attributes
41+
- tracing-core
42+
- tracing-futures
43+
- tracing-log
44+
- tracing-macros
45+
- tracing-serde
46+
- tracing-tower
47+
# tracing and tracing-subscriber have too many features to be checked by
48+
# cargo-hack --feature-powerset, combinatorics there is exploding.
49+
#- tracing
50+
#- tracing-subscriber
51+
steps:
52+
- uses: actions/checkout@main
53+
- uses: actions-rs/toolchain@v1
54+
with:
55+
toolchain: stable
56+
profile: minimal
57+
override: true
58+
- name: Install cargo-hack
59+
run: |
60+
curl -LsSf https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-x86_64-unknown-linux-gnu.tar.gz | tar xzf - -C ~/.cargo/bin
61+
- name: cargo hack check
62+
working-directory: ${{ matrix.subcrate }}
63+
run: cargo hack check --feature-powerset --no-dev-deps
64+
65+
cargo-check-tracing:
66+
runs-on: ubuntu-latest
67+
strategy:
68+
matrix:
69+
featureset:
70+
- ""
71+
- log-always
72+
- std log-always
73+
- std
74+
fail-fast: false
75+
steps:
76+
- uses: actions/checkout@main
77+
- uses: actions-rs/toolchain@v1
78+
with:
79+
toolchain: stable
80+
profile: minimal
81+
override: true
82+
- name: cargo check
83+
working-directory: tracing
84+
run: cargo check --no-default-features --features "${{ matrix.featureset }}"
85+
86+
cargo-check-subscriber:
87+
runs-on: ubuntu-latest
88+
strategy:
89+
matrix:
90+
featureset:
91+
- ""
92+
- fmt
93+
- fmt ansi
94+
- fmt json
95+
- fmt json ansi
96+
- fmt registry
97+
- fmt env-filter
98+
- registry
99+
- env-filter
100+
steps:
101+
- uses: actions/checkout@main
102+
- uses: actions-rs/toolchain@v1
103+
with:
104+
toolchain: stable
105+
profile: minimal
106+
override: true
107+
- name: cargo check
108+
working-directory: tracing-subscriber
109+
run: cargo check --no-default-features --features "${{ matrix.featureset }}"
110+
111+
features-stable:
112+
# Feature flag tests that run on stable Rust.
113+
# TODO(david): once tracing's MSRV goes up to Rust 1.51, we should be able to switch to
114+
# using cargo's V2 feature resolver (https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions)
115+
# and avoid cd'ing into each crate's directory.
116+
runs-on: ubuntu-latest
117+
steps:
118+
- uses: actions/checkout@main
119+
- uses: actions-rs/toolchain@v1
120+
with:
121+
toolchain: stable
122+
profile: minimal
123+
override: true
124+
- name: "Test log support"
125+
run: cargo test
126+
working-directory: "tracing/test-log-support"
127+
- name: "Test static max level"
128+
run: cargo test
129+
working-directory: "tracing/test_static_max_level_features"
130+
- name: "Test static max level (release)"
131+
run: cargo test --release
132+
working-directory: "tracing/test_static_max_level_features"
133+
- name: "Test tracing-core no-std support"
134+
run: cargo test --no-default-features
135+
working-directory: tracing
136+
- name: "Test tracing no-std support"
137+
run: cargo test --no-default-features
138+
working-directory: tracing
139+
# this skips running doctests under the `--no-default-features` flag,
140+
# as rustdoc isn't aware of cargo's feature flags.
141+
- name: "Test tracing-subscriber with all features disabled"
142+
run: cargo test --lib --tests --no-default-features
143+
working-directory: tracing-subscriber

tracing/test_static_max_level_features/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ edition = "2018"
1313
[dependencies.tracing]
1414
path = ".."
1515
features = ["max_level_debug", "release_max_level_info"]
16+
17+
[dev-dependencies]
18+
tokio-test = "0.2.0"

tracing/test_static_max_level_features/tests/test.rs

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1+
#[path = "../../../tracing-futures/tests/support.rs"]
2+
// this is only needed for `block_on_future`
3+
#[allow(dead_code)]
4+
mod support;
5+
use support::*;
6+
7+
use std::future::Future;
8+
use std::pin::Pin;
19
use std::sync::{Arc, Mutex};
210
use tracing::span::{Attributes, Record};
3-
use tracing::{debug, error, info, span, trace, warn, Event, Id, Level, Metadata, Subscriber};
11+
use tracing::{
12+
debug, error, info, instrument, span, trace, warn, Subscriber, Event, Id, Level, Metadata,
13+
};
14+
415

516
struct State {
617
last_level: Mutex<Option<Level>>,
718
}
819

9-
struct TestSubscriber(Arc<State>);
20+
struct TestCollector(Arc<State>);
21+
const EXPECTED_DEBUG: Option<Level> = if cfg!(debug_assertions) {
22+
Some(Level::DEBUG)
23+
} else {
24+
None
25+
};
1026

1127
impl Subscriber for TestSubscriber {
1228
fn enabled(&self, _: &Metadata) -> bool {
1329
true
1430
}
1531

16-
fn new_span(&self, _span: &Attributes) -> Id {
32+
fn new_span(&self, span: &Attributes) -> Id {
33+
*self.0.last_level.lock().unwrap() = Some(span.metadata().level().clone());
1734
span::Id::from_u64(42)
1835
}
1936

@@ -30,9 +47,15 @@ impl Subscriber for TestSubscriber {
3047
fn exit(&self, _span: &Id) {}
3148
}
3249

50+
#[track_caller]
51+
fn last(state: &State, expected: Option<Level>) {
52+
let lvl = state.last_level.lock().unwrap().take();
53+
assert_eq!(lvl, expected);
54+
}
55+
3356
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
3457
#[test]
35-
fn test_static_max_level_features() {
58+
fn test_static_max_level_events() {
3659
let me = Arc::new(State {
3760
last_level: Mutex::new(None),
3861
});
@@ -45,25 +68,73 @@ fn test_static_max_level_features() {
4568
info!("");
4669
last(&a, Some(Level::INFO));
4770
debug!("");
48-
last(&a, Some(Level::DEBUG));
71+
last(&a, EXPECTED_DEBUG);
4972
trace!("");
5073
last(&a, None);
74+
});
75+
}
5176

77+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
78+
#[test]
79+
fn test_static_max_level_spans() {
80+
let me = Arc::new(State {
81+
last_level: Mutex::new(None),
82+
});
83+
let a = me.clone();
84+
85+
tracing::subscriber::with_default(TestSubscriber(me), || {
5286
span!(Level::ERROR, "");
53-
last(&a, None);
87+
last(&a, Some(Level::ERROR));
5488
span!(Level::WARN, "");
55-
last(&a, None);
89+
last(&a, Some(Level::WARN));
5690
span!(Level::INFO, "");
57-
last(&a, None);
91+
last(&a, Some(Level::INFO));
5892
span!(Level::DEBUG, "");
59-
last(&a, None);
93+
last(&a, EXPECTED_DEBUG);
6094
span!(Level::TRACE, "");
6195
last(&a, None);
6296
});
6397
}
6498

65-
fn last(state: &State, expected: Option<Level>) {
66-
let mut lvl = state.last_level.lock().unwrap();
67-
assert_eq!(*lvl, expected);
68-
*lvl = None;
99+
#[instrument(level = "debug")]
100+
#[inline(never)] // this makes it a bit easier to look at the asm output
101+
fn instrumented_fn() {}
102+
103+
#[instrument(level = "debug")]
104+
async fn instrumented_async_fn() {}
105+
106+
#[allow(clippy::manual_async_fn)]
107+
#[instrument(level = "debug")]
108+
fn instrumented_manual_async() -> impl Future<Output = ()> {
109+
async move {}
110+
}
111+
112+
#[instrument(level = "debug")]
113+
fn instrumented_manual_box_pin() -> Pin<Box<dyn Future<Output = ()>>> {
114+
Box::pin(async move {})
115+
}
116+
117+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
118+
#[test]
119+
fn test_static_max_level_instrument() {
120+
let me = Arc::new(State {
121+
last_level: Mutex::new(None),
122+
});
123+
let a = me.clone();
124+
125+
tracing::subscriber::with_default(TestSubscriber(me), || {
126+
block_on_future(async {
127+
instrumented_fn();
128+
last(&a, EXPECTED_DEBUG);
129+
130+
instrumented_async_fn().await;
131+
last(&a, EXPECTED_DEBUG);
132+
133+
instrumented_manual_async().await;
134+
last(&a, EXPECTED_DEBUG);
135+
136+
instrumented_manual_box_pin().await;
137+
last(&a, EXPECTED_DEBUG);
138+
})
139+
});
69140
}

0 commit comments

Comments
 (0)