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

Add sample implementation of FormatEvent #1189

Merged
merged 5 commits into from
Jan 28, 2021

Conversation

davidpdrsn
Copy link
Member

Motivation

I recently had to write a custom FormatEvent implementation. Most things were straight forward but I struggled with how to print the values of fields on the span. Through the context you can get SpanRefs but those don't include field values, only field names. I had to dig around in the code to discover FormattedFields which made things easy. I think an example implementation in the docs would be helpful to others.

Solution

A sample implementation of FormatEvent in the docs that prints level, span name and fields, and event.

@davidpdrsn davidpdrsn requested review from hawkw and a team as code owners January 13, 2021 21:32
@hawkw
Copy link
Member

hawkw commented Jan 27, 2021

Hi @davidpdrsn, would you mind rebasing this branch onto the latest master? Thanks!

@davidpdrsn
Copy link
Member Author

Done 😊

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

I had a few suggestions, but this looks good to me overall!

tracing-subscriber/src/fmt/format/mod.rs Outdated Show resolved Hide resolved
Comment on lines 84 to 93
/// let ext = span.extensions();
/// let fields = &ext
/// .get::<FormattedFields<N>>()
/// .expect("will never be `None`");
Copy link
Member

Choose a reason for hiding this comment

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

it would probably be good to have an additional comment explaining this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I stole a line from the docs in 602d7e7

tracing-subscriber/src/fmt/format/mod.rs Outdated Show resolved Hide resolved
@hawkw
Copy link
Member

hawkw commented Jan 27, 2021

looks like this somehow made rustfmt unhappy?

@davidpdrsn
Copy link
Member Author

Hm strange. Works for me.

~/de/e/tracing-fork [format-event-docs @ 602d7e7fea96d]
❯ cargo fmt --all -- --check

~/de/e/tracing-fork [format-event-docs @ 602d7e7fea96d]
❯ cargo +nightly fmt --all -- --check

davidpdrsn and others added 5 commits January 28, 2021 11:01
The docs previously didn't provide much help implementing `FormatEvent`.
Especially getting access to fields on spans could be tricky unless the
user was aware of `FormattedFields`.

This updates the docs with a basic, but working, example.
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
@davidpdrsn
Copy link
Member Author

Omg its green 🤯

Copy link
Member

@davidbarsky davidbarsky left a comment

Choose a reason for hiding this comment

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

This LGTM!

@hawkw hawkw merged commit 5649d3b into tokio-rs:master Jan 28, 2021
hawkw added a commit that referenced this pull request Jan 28, 2021
* Add sample implementation of `FormatEvent`

The docs previously didn't provide much help implementing `FormatEvent`.
Especially getting access to fields on spans could be tricky unless the
user was aware of `FormattedFields`.

This updates the docs with a basic, but working, example.

* Apply suggestions from code review

Co-authored-by: Eliza Weisman <eliza@buoyant.io>

* Add comment explaining what `FormattedFields` is

* Expand docs a bit

* Fix formatting

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
@davidpdrsn davidpdrsn deleted the format-event-docs branch January 28, 2021 17:37
hawkw added a commit that referenced this pull request Jan 28, 2021
* Add sample implementation of `FormatEvent`

The docs previously didn't provide much help implementing `FormatEvent`.
Especially getting access to fields on spans could be tricky unless the
user was aware of `FormattedFields`.

This updates the docs with a basic, but working, example.

* Apply suggestions from code review

Co-authored-by: Eliza Weisman <eliza@buoyant.io>

* Add comment explaining what `FormattedFields` is

* Expand docs a bit

* Fix formatting

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Jan 28, 2021
* Add sample implementation of `FormatEvent`

The docs previously didn't provide much help implementing `FormatEvent`.
Especially getting access to fields on spans could be tricky unless the
user was aware of `FormattedFields`.

This updates the docs with a basic, but working, example.

* Apply suggestions from code review

Co-authored-by: Eliza Weisman <eliza@buoyant.io>

* Add comment explaining what `FormattedFields` is

* Expand docs a bit

* Fix formatting

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Feb 20, 2021
Fixed

- **env-filter**: Fixed directives where the level is in mixed case
  (such as `Info`) failing to parse ([#1126])
- **fmt**: Fixed `fmt::Subscriber` not providing a max-level hint
  ([#1251])
- `tracing-subscriber` no longer enables `tracing` and `tracing-core`'s
  default features ([#1144])

Changed

- **chrono**: Updated `chrono` dependency to 0.4.16 ([#1189])
- **log**: Updated `tracing-log` dependency to 0.1.2

Thanks to @salewski, @taiki-e, @davidpdrsn and @markdingram for
contributing to this release!

[#1126]: #1126
[#1251]: #1251
[#1144]: #1144
[#1189]: #1189

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Feb 20, 2021
### Fixed

- **env-filter**: Fixed directives where the level is in mixed case
  (such as `Info`) failing to parse ([#1126])
- **fmt**: Fixed `fmt::Subscriber` not providing a max-level hint
  ([#1251])
- `tracing-subscriber` no longer enables `tracing` and `tracing-core`'s
  default features ([#1144])

### Changed

- **chrono**: Updated `chrono` dependency to 0.4.16 ([#1189])
- **log**: Updated `tracing-log` dependency to 0.1.2

Thanks to @salewski, @taiki-e, @davidpdrsn and @markdingram for
contributing to this release!

[#1126]: #1126
[#1251]: #1251
[#1144]: #1144
[#1189]: #1189

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
### Fixed

- **env-filter**: Fixed directives where the level is in mixed case
  (such as `Info`) failing to parse ([tokio-rs#1126])
- **fmt**: Fixed `fmt::Subscriber` not providing a max-level hint
  ([tokio-rs#1251])
- `tracing-subscriber` no longer enables `tracing` and `tracing-core`'s
  default features ([tokio-rs#1144])

### Changed

- **chrono**: Updated `chrono` dependency to 0.4.16 ([tokio-rs#1189])
- **log**: Updated `tracing-log` dependency to 0.1.2

Thanks to @salewski, @taiki-e, @davidpdrsn and @markdingram for
contributing to this release!

[tokio-rs#1126]: tokio-rs#1126
[tokio-rs#1251]: tokio-rs#1251
[tokio-rs#1144]: tokio-rs#1144
[tokio-rs#1189]: tokio-rs#1189

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
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.

None yet

3 participants