Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support constant expressions as instrument field names #3158

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nico-incubiq
Copy link

@nico-incubiq nico-incubiq commented Nov 27, 2024

Motivation

The #[instrument] proc macro only allows setting field names as identifiers, which is not on par with other macros such as span! or event! that allow constant expressions. Using constants for example can ensure consistency across a project for the field names used.

This addresses #2969, which was already mentioned in this comment here on #2617 (note that "classic macros" mentioned there already work, there are tests attesting of it).
I also updated the doc block to mention field values can be any arbitrary expressions, which was implemented by #672 but never updated.

Solution

I updated the instrument proc macro to allow constant expressions (const, const fn, literal str, ...) as field names, using the same syntax as #2617, enclosed in curly braces.

const CONST_FIELD_NAME: &str = "foo.bar";

#[instrument(fields({CONST_FIELD_NAME} = "quux"))]
fn fn_const_field_name() {}

Notes

  • The identifier (Punctuated) field names have a convenient feature allowing to supersede non-skipped function arguments (a field named s would be used instead of a function argument s in the span). As far as I'm aware this is not possible to implement for constant expressions, as the compiler doesn't allow to evaluate constants at build time (MIRI might be a solution but that seems complicated, and https://rustc-dev-guide.rust-lang.org/const-eval seems experimental). It's a minor inconsistency; in this scenario, the code would panic immediately at startup, and the workaround is as simple as skipping the function argument.

@nico-incubiq nico-incubiq requested review from hawkw, davidbarsky and a team as code owners November 27, 2024 01:40
@nico-incubiq nico-incubiq changed the title Support constant expressions as tracing field names Support constant expressions as instrument field names Nov 27, 2024
@@ -730,11 +730,46 @@ impl Clone for Field {
}
}

// Compares two strings for equality in a constant context.
const fn str_eq(left: &str, right: &str) -> bool {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of this is because the code is executed from a const fn context. i couldn't find off-the-shelf functions that do the same, and I also couldn't find a better place to add this check than the constructor of the FieldSet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant