1 use tracing::subscriber::with_default;
2 use tracing_attributes::instrument;
3 use tracing_mock::{expect, span::NewSpan, subscriber};
4
5 #[instrument(fields(foo = "bar", dsa = true, num = 1))]
fn_no_param()6 fn fn_no_param() {}
7
8 #[instrument(fields(foo = "bar"))]
fn_param(param: u32)9 fn fn_param(param: u32) {}
10
11 #[instrument(fields(foo = "bar", empty))]
fn_empty_field()12 fn fn_empty_field() {}
13
14 #[instrument(fields(len = s.len()))]
fn_expr_field(s: &str)15 fn fn_expr_field(s: &str) {}
16
17 #[instrument(fields(s.len = s.len(), s.is_empty = s.is_empty()))]
fn_two_expr_fields(s: &str)18 fn fn_two_expr_fields(s: &str) {
19 let _ = s;
20 }
21
22 #[instrument(fields(%s, s.len = s.len()))]
fn_clashy_expr_field(s: &str)23 fn fn_clashy_expr_field(s: &str) {
24 let _ = s;
25 }
26
27 #[instrument(fields(s = "s"))]
fn_clashy_expr_field2(s: &str)28 fn fn_clashy_expr_field2(s: &str) {
29 let _ = s;
30 }
31
32 #[instrument(fields(s = &s))]
fn_string(s: String)33 fn fn_string(s: String) {
34 let _ = s;
35 }
36
37 #[instrument(fields(keywords.impl.type.fn = _arg), skip(_arg))]
38 fn fn_keyword_ident_in_field(_arg: &str) {}
39
40 #[derive(Debug)]
41 struct HasField {
42 my_field: &'static str,
43 }
44
45 impl HasField {
46 #[instrument(fields(my_field = self.my_field), skip(self))]
self_expr_field(&self)47 fn self_expr_field(&self) {}
48 }
49
50 #[test]
fields()51 fn fields() {
52 let span = expect::span().with_fields(
53 expect::field("foo")
54 .with_value(&"bar")
55 .and(expect::field("dsa").with_value(&true))
56 .and(expect::field("num").with_value(&1))
57 .only(),
58 );
59 run_test(span, || {
60 fn_no_param();
61 });
62 }
63
64 #[test]
expr_field()65 fn expr_field() {
66 let span = expect::span().with_fields(
67 expect::field("s")
68 .with_value(&"hello world")
69 .and(expect::field("len").with_value(&"hello world".len()))
70 .only(),
71 );
72 run_test(span, || {
73 fn_expr_field("hello world");
74 });
75 }
76
77 #[test]
two_expr_fields()78 fn two_expr_fields() {
79 let span = expect::span().with_fields(
80 expect::field("s")
81 .with_value(&"hello world")
82 .and(expect::field("s.len").with_value(&"hello world".len()))
83 .and(expect::field("s.is_empty").with_value(&false))
84 .only(),
85 );
86 run_test(span, || {
87 fn_two_expr_fields("hello world");
88 });
89 }
90
91 #[test]
clashy_expr_field()92 fn clashy_expr_field() {
93 let span = expect::span().with_fields(
94 // Overriding the `s` field should record `s` as a `Display` value,
95 // rather than as a `Debug` value.
96 expect::field("s")
97 .with_value(&tracing::field::display("hello world"))
98 .and(expect::field("s.len").with_value(&"hello world".len()))
99 .only(),
100 );
101 run_test(span, || {
102 fn_clashy_expr_field("hello world");
103 });
104
105 let span = expect::span().with_fields(expect::field("s").with_value(&"s").only());
106 run_test(span, || {
107 fn_clashy_expr_field2("hello world");
108 });
109 }
110
111 #[test]
self_expr_field()112 fn self_expr_field() {
113 let span =
114 expect::span().with_fields(expect::field("my_field").with_value(&"hello world").only());
115 run_test(span, || {
116 let has_field = HasField {
117 my_field: "hello world",
118 };
119 has_field.self_expr_field();
120 });
121 }
122
123 #[test]
parameters_with_fields()124 fn parameters_with_fields() {
125 let span = expect::span().with_fields(
126 expect::field("foo")
127 .with_value(&"bar")
128 .and(expect::field("param").with_value(&1u32))
129 .only(),
130 );
131 run_test(span, || {
132 fn_param(1);
133 });
134 }
135
136 #[test]
empty_field()137 fn empty_field() {
138 let span = expect::span().with_fields(expect::field("foo").with_value(&"bar").only());
139 run_test(span, || {
140 fn_empty_field();
141 });
142 }
143
144 #[test]
string_field()145 fn string_field() {
146 let span = expect::span().with_fields(expect::field("s").with_value(&"hello world").only());
147 run_test(span, || {
148 fn_string(String::from("hello world"));
149 });
150 }
151
152 #[test]
keyword_ident_in_field_name()153 fn keyword_ident_in_field_name() {
154 let span = expect::span().with_fields(
155 expect::field("keywords.impl.type.fn")
156 .with_value(&"test")
157 .only(),
158 );
159 run_test(span, || fn_keyword_ident_in_field("test"));
160 }
161
run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F)162 fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
163 let (subscriber, handle) = subscriber::mock()
164 .new_span(span)
165 .enter(expect::span())
166 .exit(expect::span())
167 .only()
168 .run_with_handle();
169
170 with_default(subscriber, fun);
171 handle.assert_finished();
172 }
173