1 | #![cfg (feature = "env-filter" )] |
2 | |
3 | use tracing::{self, subscriber::with_default, Level}; |
4 | use tracing_mock::*; |
5 | use tracing_subscriber::{filter::EnvFilter, prelude::*}; |
6 | |
7 | #[test] |
8 | #[cfg_attr (not(flaky_tests), ignore)] |
9 | fn field_filter_events() { |
10 | let filter: EnvFilter = "[{thing}]=debug" .parse().expect("filter should parse" ); |
11 | let (subscriber, finished) = subscriber::mock() |
12 | .event( |
13 | expect::event() |
14 | .at_level(Level::INFO) |
15 | .with_fields(expect::field("thing" )), |
16 | ) |
17 | .event( |
18 | expect::event() |
19 | .at_level(Level::DEBUG) |
20 | .with_fields(expect::field("thing" )), |
21 | ) |
22 | .only() |
23 | .run_with_handle(); |
24 | let subscriber = subscriber.with(filter); |
25 | |
26 | with_default(subscriber, || { |
27 | tracing::trace!(disabled = true); |
28 | tracing::info!("also disabled" ); |
29 | tracing::info!(thing = 1); |
30 | tracing::debug!(thing = 2); |
31 | tracing::trace!(thing = 3); |
32 | }); |
33 | |
34 | finished.assert_finished(); |
35 | } |
36 | |
37 | #[test] |
38 | #[cfg_attr (not(flaky_tests), ignore)] |
39 | fn field_filter_spans() { |
40 | let filter: EnvFilter = "[{enabled=true}]=debug" |
41 | .parse() |
42 | .expect("filter should parse" ); |
43 | let (subscriber, finished) = subscriber::mock() |
44 | .enter(expect::span().named("span1" )) |
45 | .event( |
46 | expect::event() |
47 | .at_level(Level::INFO) |
48 | .with_fields(expect::field("something" )), |
49 | ) |
50 | .exit(expect::span().named("span1" )) |
51 | .enter(expect::span().named("span2" )) |
52 | .exit(expect::span().named("span2" )) |
53 | .enter(expect::span().named("span3" )) |
54 | .event( |
55 | expect::event() |
56 | .at_level(Level::DEBUG) |
57 | .with_fields(expect::field("something" )), |
58 | ) |
59 | .exit(expect::span().named("span3" )) |
60 | .only() |
61 | .run_with_handle(); |
62 | let subscriber = subscriber.with(filter); |
63 | |
64 | with_default(subscriber, || { |
65 | tracing::trace!("disabled" ); |
66 | tracing::info!("also disabled" ); |
67 | tracing::info_span!("span1" , enabled = true).in_scope(|| { |
68 | tracing::info!(something = 1); |
69 | }); |
70 | tracing::debug_span!("span2" , enabled = false, foo = "hi" ).in_scope(|| { |
71 | tracing::warn!(something = 2); |
72 | }); |
73 | tracing::trace_span!("span3" , enabled = true, answer = 42).in_scope(|| { |
74 | tracing::debug!(something = 2); |
75 | }); |
76 | }); |
77 | |
78 | finished.assert_finished(); |
79 | } |
80 | |
81 | #[test] |
82 | fn record_after_created() { |
83 | let filter: EnvFilter = "[{enabled=true}]=debug" |
84 | .parse() |
85 | .expect("filter should parse" ); |
86 | let (subscriber, finished) = subscriber::mock() |
87 | .enter(expect::span().named("span" )) |
88 | .exit(expect::span().named("span" )) |
89 | .record( |
90 | expect::span().named("span" ), |
91 | expect::field("enabled" ).with_value(&true), |
92 | ) |
93 | .enter(expect::span().named("span" )) |
94 | .event(expect::event().at_level(Level::DEBUG)) |
95 | .exit(expect::span().named("span" )) |
96 | .only() |
97 | .run_with_handle(); |
98 | let subscriber = subscriber.with(filter); |
99 | |
100 | with_default(subscriber, || { |
101 | let span = tracing::info_span!("span" , enabled = false); |
102 | span.in_scope(|| { |
103 | tracing::debug!("i'm disabled!" ); |
104 | }); |
105 | |
106 | span.record("enabled" , true); |
107 | span.in_scope(|| { |
108 | tracing::debug!("i'm enabled!" ); |
109 | }); |
110 | |
111 | tracing::debug!("i'm also disabled" ); |
112 | }); |
113 | |
114 | finished.assert_finished(); |
115 | } |
116 | |