Skip to content

Commit 6d319a0

Browse files
dpcmbrobbelhdshawkw
authored
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 e7aeee8 commit 6d319a0

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>,
@@ -86,6 +86,8 @@ impl Parse for InstrumentArgs {
8686
// XXX: apparently we support names as either named args with an
8787
// sign, _or_ as unnamed string literals. That's weird, but
8888
// changing it is apparently breaking.
89+
// This also means that when using idents for name, it must be via
90+
// a named arg, i.e. `#[instrument(name = SOME_IDENT)]`.
8991
if args.name.is_some() {
9092
return Err(input.error("expected only a single `name` argument"));
9193
}
@@ -198,8 +200,32 @@ impl Parse for EventArgs {
198200
}
199201
}
200202

203+
#[derive(Debug, Clone)]
204+
pub(super) enum LitStrOrIdent {
205+
LitStr(LitStr),
206+
Ident(Ident),
207+
}
208+
209+
impl ToTokens for LitStrOrIdent {
210+
fn to_tokens(&self, tokens: &mut TokenStream) {
211+
match self {
212+
LitStrOrIdent::LitStr(target) => target.to_tokens(tokens),
213+
LitStrOrIdent::Ident(ident) => ident.to_tokens(tokens),
214+
}
215+
}
216+
}
217+
218+
impl Parse for LitStrOrIdent {
219+
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
220+
input
221+
.parse::<LitStr>()
222+
.map(LitStrOrIdent::LitStr)
223+
.or_else(|_| input.parse::<Ident>().map(LitStrOrIdent::Ident))
224+
}
225+
}
226+
201227
struct StrArg<T> {
202-
value: LitStr,
228+
value: LitStrOrIdent,
203229
_p: std::marker::PhantomData<T>,
204230
}
205231

tracing-attributes/tests/instrument.rs

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

240240
handle.assert_finished();
241241
}
242+
243+
#[test]
244+
fn name_ident() {
245+
const MY_NAME: &str = "my_name";
246+
#[instrument(name = MY_NAME)]
247+
fn name() {}
248+
249+
let span_name = expect::span().named(MY_NAME);
250+
251+
let (collector, handle) = collector::mock()
252+
.new_span(span_name.clone())
253+
.enter(span_name.clone())
254+
.exit(span_name.clone())
255+
.drop_span(span_name)
256+
.only()
257+
.run_with_handle();
258+
259+
with_default(collector, || {
260+
name();
261+
});
262+
263+
handle.assert_finished();
264+
}
265+
266+
#[test]
267+
fn target_ident() {
268+
const MY_TARGET: &str = "my_target";
269+
270+
#[instrument(target = MY_TARGET)]
271+
fn target() {}
272+
273+
let span_target = expect::span().named("target").with_target(MY_TARGET);
274+
275+
let (collector, handle) = collector::mock()
276+
.new_span(span_target.clone())
277+
.enter(span_target.clone())
278+
.exit(span_target.clone())
279+
.drop_span(span_target)
280+
.only()
281+
.run_with_handle();
282+
283+
with_default(collector, || {
284+
target();
285+
});
286+
287+
handle.assert_finished();
288+
}
289+
290+
#[test]
291+
fn target_name_ident() {
292+
const MY_NAME: &str = "my_name";
293+
const MY_TARGET: &str = "my_target";
294+
295+
#[instrument(target = MY_TARGET, name = MY_NAME)]
296+
fn name_target() {}
297+
298+
let span_name_target = expect::span().named(MY_NAME).with_target(MY_TARGET);
299+
300+
let (collector, handle) = collector::mock()
301+
.new_span(span_name_target.clone())
302+
.enter(span_name_target.clone())
303+
.exit(span_name_target.clone())
304+
.drop_span(span_name_target)
305+
.only()
306+
.run_with_handle();
307+
308+
with_default(collector, || {
309+
name_target();
310+
});
311+
312+
handle.assert_finished();
313+
}

0 commit comments

Comments
 (0)