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

Allow string literal field identifiers #2606

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

Conversation

valkum
Copy link

@valkum valkum commented Jun 1, 2023

Fixes #2438

Motivation

Currently, you can only use Idents delimited by . for fields.
This prevents us creating an empty field containing rust keywords such
as type. (e.g. graphql.operation.type)
The span! and event! macros allow the usage of string literals to circumvent
this limitation.

tracing::event!(Level::TRACE, "field.type" = "test", "event name");

Solution

To allow for a similar API as with the macros, this PR replaces the Field.name with an enum FieldName.
The enum can be the current Punctuated type or a LitStr.

Possible other solution

Another option would be to unraw the identifiers when emitting the field name. But this might cause confusion when an emitted field name does not match a struct field name in code.

I am open to providing a PR targeting the master branch as well.

@valkum valkum requested review from hawkw and a team as code owners June 1, 2023 19:09
@gruebel
Copy link

gruebel commented Jan 30, 2024

hey @valkum thanks for the code 🍻 I'm really interested in getting this into the crate, because it solves the issue of key names with type. Do you still want to work on it, otherwise I would create a new PR with your changes.

@hawkw as one of the tokio members, how do you feel about the code changes in the general?

@valkum
Copy link
Author

valkum commented Jan 31, 2024

I can see if I can rebase the current PR in the coming days. Do you want two PRs one for 0.1.x and one for master?

@nimrodkor
Copy link

@valkum @hawkw Any progress on this one?

@valkum
Copy link
Author

valkum commented Mar 15, 2024

Hey sorry. Nothing new. I hope I can make some progress tomorrow or next Thursday. I set myself a deadline for next weekend.

@gruebel
Copy link

gruebel commented Mar 15, 2024

@valkum all good 🙂 let me know, otherwise I will create a new PR branching off yours 💪

@valkum valkum force-pushed the allow-string-literal-fields branch from fa1c44d to a6841ce Compare March 16, 2024 19:31
@valkum valkum requested a review from yaahc as a code owner March 16, 2024 19:31
@valkum valkum changed the base branch from v0.1.x to master March 16, 2024 19:31
Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

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

Thank you very much for the PR and sorry for the long lead time on the review.

We're happy with the change, but ideally we'd like a test for the negative case (compilation error I'm guessing) mentioned in the review comments.

Given how long it's been, if you don't have the time and/or interest to come back to this PR, please let us know and we'll hand it off to someone else.

Only a PR against master is necessary, we'll handle backporting ourselves.

Once again, thank you very much!

tracing-attributes/tests/instrument.rs Show resolved Hide resolved
@mladedav
Copy link
Contributor

I'm not entirely sure, but is this the same #2924 and #2925? This seems to be similar especially to the latter but the former seems to fix the same thing in a simpler way (unless there is a hidden issue with that approach).

Arguably this has been the first PR, but I'd just like to bring it to attention here.

@valkum
Copy link
Author

valkum commented Apr 22, 2024

I'm not entirely sure, but is this the same #2924 and #2925? This seems to be similar especially to the latter but the former seems to fix the same thing in a simpler way (unless there is a hidden issue with that approach).

Arguably this has been the first PR, but I'd just like to bring it to attention here.

It fixes the same problem but uses peek instead of ahead which makes the implementation slightly less complex. I think my PR provides a bit more DX with the custom error message. Not sure if there is a drawback of using ahead instead of peek.

Replaces the Field.name type with an enum that can be the current
Punctuated type or a LitStr
@valkum valkum force-pushed the allow-string-literal-fields branch from a6841ce to ca0c0b3 Compare April 22, 2024 17:03
@nimrodkor
Copy link

@hds Mind taking another look?

Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

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

This looks good to me now, thank you!

@davidbarsky what do you think?

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.

tracing::instrument doesn't support fields as strings or raw identifiers when keywords are involved
6 participants