Skip to content

Commit d921d5f

Browse files
dpcmbrobbelhdshawkw
committed
tracing-attributes: support const values for target and name (#2941)
Fixes #2960 Co-authored-by: Matthijs Brobbel <m1brobbel@gmail.com> Co-authored-by: Hayden Stainsby <hds@caffeineconcepts.com> Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent e9d3468 commit d921d5f

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

tracing-attributes/src/attr.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub(crate) struct EventArgs {
1717
#[derive(Clone, Default, Debug)]
1818
pub(crate) struct InstrumentArgs {
1919
level: Option<Level>,
20-
pub(crate) name: Option<LitStr>,
21-
target: Option<LitStr>,
20+
pub(crate) name: Option<LitStrOrIdent>,
21+
target: Option<LitStrOrIdent>,
2222
pub(crate) parent: Option<Expr>,
2323
pub(crate) follows_from: Option<Expr>,
2424
pub(crate) skips: HashSet<Ident>,
@@ -87,6 +87,8 @@ impl Parse for InstrumentArgs {
8787
// XXX: apparently we support names as either named args with an
8888
// sign, _or_ as unnamed string literals. That's weird, but
8989
// changing it is apparently breaking.
90+
// This also means that when using idents for name, it must be via
91+
// a named arg, i.e. `#[instrument(name = SOME_IDENT)]`.
9092
if args.name.is_some() {
9193
return Err(input.error("expected only a single `name` argument"));
9294
}
@@ -211,8 +213,32 @@ impl Parse for EventArgs {
211213
}
212214
}
213215

216+
#[derive(Debug, Clone)]
217+
pub(super) enum LitStrOrIdent {
218+
LitStr(LitStr),
219+
Ident(Ident),
220+
}
221+
222+
impl ToTokens for LitStrOrIdent {
223+
fn to_tokens(&self, tokens: &mut TokenStream) {
224+
match self {
225+
LitStrOrIdent::LitStr(target) => target.to_tokens(tokens),
226+
LitStrOrIdent::Ident(ident) => ident.to_tokens(tokens),
227+
}
228+
}
229+
}
230+
231+
impl Parse for LitStrOrIdent {
232+
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
233+
input
234+
.parse::<LitStr>()
235+
.map(LitStrOrIdent::LitStr)
236+
.or_else(|_| input.parse::<Ident>().map(LitStrOrIdent::Ident))
237+
}
238+
}
239+
214240
struct StrArg<T> {
215-
value: LitStr,
241+
value: LitStrOrIdent,
216242
_p: std::marker::PhantomData<T>,
217243
}
218244

tracing-attributes/tests/instrument.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,75 @@ fn impl_trait_return_type() {
252252

253253
handle.assert_finished();
254254
}
255+
256+
#[test]
257+
fn name_ident() {
258+
const MY_NAME: &str = "my_name";
259+
#[instrument(name = MY_NAME)]
260+
fn name() {}
261+
262+
let span_name = expect::span().named(MY_NAME);
263+
264+
let (subscriber, handle) = subscriber::mock()
265+
.new_span(span_name.clone())
266+
.enter(span_name.clone())
267+
.exit(span_name.clone())
268+
.drop_span(span_name)
269+
.only()
270+
.run_with_handle();
271+
272+
with_default(subscriber, || {
273+
name();
274+
});
275+
276+
handle.assert_finished();
277+
}
278+
279+
#[test]
280+
fn target_ident() {
281+
const MY_TARGET: &str = "my_target";
282+
283+
#[instrument(target = MY_TARGET)]
284+
fn target() {}
285+
286+
let span_target = expect::span().named("target").with_target(MY_TARGET);
287+
288+
let (subscriber, handle) = subscriber::mock()
289+
.new_span(span_target.clone())
290+
.enter(span_target.clone())
291+
.exit(span_target.clone())
292+
.drop_span(span_target)
293+
.only()
294+
.run_with_handle();
295+
296+
with_default(subscriber, || {
297+
target();
298+
});
299+
300+
handle.assert_finished();
301+
}
302+
303+
#[test]
304+
fn target_name_ident() {
305+
const MY_NAME: &str = "my_name";
306+
const MY_TARGET: &str = "my_target";
307+
308+
#[instrument(target = MY_TARGET, name = MY_NAME)]
309+
fn name_target() {}
310+
311+
let span_name_target = expect::span().named(MY_NAME).with_target(MY_TARGET);
312+
313+
let (subscriber, handle) = subscriber::mock()
314+
.new_span(span_name_target.clone())
315+
.enter(span_name_target.clone())
316+
.exit(span_name_target.clone())
317+
.drop_span(span_name_target)
318+
.only()
319+
.run_with_handle();
320+
321+
with_default(subscriber, || {
322+
name_target();
323+
});
324+
325+
handle.assert_finished();
326+
}

0 commit comments

Comments
 (0)