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

trace: Add shorthand syntax for local fields #1103

Merged
merged 10 commits into from
Jun 9, 2019
Merged

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented May 22, 2019

Motivation

A common pattern in tokio-trace is to use the value of a local
variable as a field on a span or event. Currently, this requires code
like:

info!(foo = foo);

which is not particularly ergonomic given how commonly this occurs.
Struct initializers support a shorthand syntax for fields where the name
of the field is the same as a local variable, and tokio-trace should
as well.

Solution

This branch adds support for syntax like

let foo = ...;
info!(foo);

and

let foo = Foo {
    bar: ...,
    ...
};
info!(foo.bar)

to the tokio-trace span and event macros. This syntax also works with
the Debug and Display field shorthand.

The span macros previously used a field name with no value to indicate
an uninitialized field. A new issue, #1138, has been opened for finding a
replacement syntax for uninitialized fields. Until then, the tokio-trace
macros will no longer provide a way to create fields without values,
although the -core API will continue to support this.

Closes #1062

Signed-off-by: Eliza Weisman eliza@buoyant.io

@hawkw hawkw added C-enhancement Category: A PR with an enhancement or bugfix. tokio-trace labels May 22, 2019
@hawkw hawkw added this to the tokio-trace v0.2 (breaking changes) milestone May 22, 2019
@hawkw hawkw requested review from carllerche and jonhoo May 22, 2019 23:20
@hawkw hawkw self-assigned this May 22, 2019
@hawkw hawkw requested a review from jonhoo May 23, 2019 21:25
hawkw added 3 commits May 24, 2019 14:15
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw force-pushed the eliza/field-shorthand branch from c2ba3b5 to 9729fba Compare May 24, 2019 21:16
hawkw added 5 commits May 24, 2019 15:06
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
because apparently this is a thing that matters
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw
Copy link
Member Author

hawkw commented May 25, 2019

@carllerche, I'd like to go ahead and merge this, but I figured I'd ping you first in case you want to take a closer look.

@carllerche
Copy link
Member

i'm not in love w/ foo = _ as a syntax to define a field that is not being assigned to. This is using the assignment operator to say "not assigned".

@carllerche
Copy link
Member

Could something like: info!(foo, bar = 3, unassigned(baz, bur)) work?

@hawkw
Copy link
Member Author

hawkw commented May 28, 2019

@carllerche I agree that using the assignment operator to indicate unassigned fields is definitely a bit strange, and I'm open to a different syntax.

I have a couple of quibbles about unassigned(baz, bur) though:

  • unassigned is kind of wordy for what I still expect to be at least a relatively common macro usage.
  • I'd prefer to avoid a syntax that looks like a function call for something that isn't actually implemented as a function -- I'd rather have it look like a keyword, as in unassigned bar. On the other hand, in that case you'd have to repeat the unassigned for every unassigned field, which is even wordier.

So, I'm open to this syntax if a majority of folks agree it's better.

I'm still not really in love with any of the options we've got here, using the field name without an assignment was probably the best fit for this meaning. But the case of using local variables is even more common, and allowing shorthand for that definitely improves ergonomics. It's just a shame that there isn't really an obvious, natural syntax for unassigned fields.

I'm still open to suggestions.

@carllerche
Copy link
Member

info!(foo, bar, let baz, let buz);

?

@hawkw
Copy link
Member Author

hawkw commented May 28, 2019

@jonhoo, @davidbarsky, @LucioFranco, you've all weighed in on this in the past --- any thoughts on Carl's suggestions? What seems the most intuitive to you?

@jonhoo
Copy link
Contributor

jonhoo commented May 28, 2019

I still like field = _ better. let bar in a list strikes me as really odd.. We could use some kind of surrounder like (field) to indicate unnamed maybe, but that's also weird.

@hawkw
Copy link
Member Author

hawkw commented Jun 5, 2019

Unless anyone else using tokio-trace (@davidbarsky? @LucioFranco? @Ralith?) has opinions on the syntax, I'm inclined to just move forward with field = _ and merge this PR as-is, since it seems like it was what most folks were in favor of in the discussion on #1062.

I'm also open to Carl's suggestion of let field, but personally I'm slightly in favor of field = _.

@carllerche
Copy link
Member

I don't want to move forward with field = _. It seems very counter intuitive.

@carllerche
Copy link
Member

(unless others think I am wrong and the syntax makes sense).

@carllerche
Copy link
Member

why not foo? for an unspecified field?

@davidbarsky
Copy link
Member

I prefer the field = _ syntax significantly more than foo?, in that it is symmetric to the existing let _ = call(); syntax. foo? feels like the field is being assigned and an error might be returned from this context.

@carllerche
Copy link
Member

foo = _ is using the assignment operator to say "don't assign", this feels backwards to me and I don't know of any precedence where such syntax is used.

@seanmonstar
Copy link
Member

let field seems better. Maybe field?, but I haven't thought that one through at all.

@hawkw
Copy link
Member Author

hawkw commented Jun 5, 2019

why not foo? for an unspecified field?

There are a couple reasons I don't think this is a great option:

  1. trace: Add shorthand for field::display and field::debug #1088 added foo = ?expr as shorthand for foo = field::debug(expr), and this branch builds on that by allowing that shorthand to be used alongside the local variable shorthand (i.e., ?foo as shorthand for foo = field::debug(foo)). It seems potentially confusing to have both ?foo and foo? with different meanings.
  2. It also looks similar to the ? operator for propagating errors. An ? expression wouldn't currently be valid in this position in the macro, so it doesn't actually conflict, but it looks similar, which I think also has the potential to be confusing.

Another potential option I thought was worth bringing up again is @jonhoo's suggestion of field.. in #1062 (comment). We currently allow single . characters in field names, but only with an identifier on both sides (i.e., the field access syntax), so it would work with the existing macro matchers. I'm not sure if something like foo.bar.baz.. would be confusing to users or not, though.

@hawkw
Copy link
Member Author

hawkw commented Jun 7, 2019

In order to move forwards with the local field shorthand, I'm considering punting on the replacement syntax for uninitialized fields for now, and adding that in a future PR once we can agree on an appropriate syntax. This means that the tokio-trace span macros would (temporarily) not support uninitialized fields on master.

@jonhoo, @davidbarsky, @LucioFranco, do any of you have any objections to that? The alternative is trying to reach a consensus on a replacement syntax.

@jonhoo
Copy link
Contributor

jonhoo commented Jun 7, 2019

Seems fine to me

@LucioFranco
Copy link
Member

I personally do not need this right away, so im always +1 on punting (I like procrastinating anyways :D )

@carllerche
Copy link
Member

@hawkw would adding support later require a change to -core?

@hawkw
Copy link
Member Author

hawkw commented Jun 7, 2019

@carllerche no, tokio-trace-core will support uninitialized fields regardless of this change, this is strictly a change to the macros in tokio-trace. The support in the core APIs will still be there, it will just be inaccessible through the macros until they have a new syntax for using it.

@carllerche
Copy link
Member

@hawkw +1 on punting.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw
Copy link
Member Author

hawkw commented Jun 7, 2019

Okay, I've removed the field = _ syntax for now, and opened #1138 for agreeing on a new syntax for uninitialized fields.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw merged commit 41ca9a4 into master Jun 9, 2019
@seanmonstar seanmonstar deleted the eliza/field-shorthand branch June 11, 2019 19:56
@mbilker
Copy link
Contributor

mbilker commented Jun 22, 2019

Was changing the debug and debug_span macros to use the INFO level instead of the DEBUG level intentional with this PR? If not, I can open a PR changing those macros back to the DEBUG level.

@hawkw
Copy link
Member Author

hawkw commented Jun 22, 2019

Nope, that was definitely not intentional! A fix would be very appreciated.

@mbilker
Copy link
Contributor

mbilker commented Jun 22, 2019

I put up a fix in PR #1170

hawkw pushed a commit that referenced this pull request Jun 22, 2019
PR #1103 accidentally changed the log level for the debug and
debug_span macros to use the INFO level instead of the DEBUG
level. This PR corrects this regression back to the intended
behavior.
hawkw added a commit that referenced this pull request Jun 25, 2019
## Motivation

A common pattern in `tokio-trace` is to use the value of a local
variable as a field on a span or event. Currently, this requires code
like:
```rust
info!(foo = foo);
```
which is not particularly ergonomic given how commonly this occurs.
Struct initializers support a shorthand syntax for fields where the name
of the field is the same as a local variable, and `tokio-trace` should
as well.

## Solution

This branch adds support for syntax like
```rust
let foo = ...;
info!(foo);
```
and 
```rust
let foo = Foo {
    bar: ...,
    ...
};
info!(foo.bar)
```
to the `tokio-trace` span and event macros. This syntax also works with
the `Debug` and `Display` field shorthand.

The span macros previously used a field name with no value to indicate 
an uninitialized field. A new issue, #1138, has been opened for finding a
replacement syntax for uninitialized fields. Until then, the `tokio-trace` 
macros will no longer provide a way to create fields without values, 
although the `-core` API will continue to support this.

Closes #1062 

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw pushed a commit that referenced this pull request Jun 25, 2019
PR #1103 accidentally changed the log level for the debug and
debug_span macros to use the INFO level instead of the DEBUG
level. This PR corrects this regression back to the intended
behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: A PR with an enhancement or bugfix.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trace: Shorthand field syntax
7 participants