-
Notifications
You must be signed in to change notification settings - Fork 727
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
subscriber: if error occurs when formatting event, write error to Writer #2102
subscriber: if error occurs when formatting event, write error to Writer #2102
Conversation
I looked again at my code (albeit a quick look on mobile), and there's only |
Oh, yeah you're right. Will retest.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a fine solution!
One thought, though, is that it would be really nice to be able to expose any actual error message as well. However, doing that would be a bit more complex. For all the text-based formatters, the error generated would just be a fmt::Error
, which doesn't have any interesting information attached to it. On the other hand, when using the JSON formatter, we get a potentially more useful serde
serialization error...which, unfortunately, is currently always just discarded and replaced with fmt::Error
, discarding any information about the error:
visit().map_err(|_| fmt::Error)?; |
We could potentially print that error information if we changed the approach so that it's the FormatEvent
implementations that are responsible for printing the error instead. However, this would mean that any arbitrary user-provided FormatEvent
implementations would have to remember to add that themselves, while in your approach, tracing-subscriber
would always ensure that the error is printed. So, there's a tradeoff there that's worth thinking about.
A third solution is to change the FormatEvent
trait to allow it to return any arbitrary error type, rather than hard-coding fmt::Error
. This would permit the JSON formatter to return serialization errors directly. However, this would be a breaking API change, so we'd have to save that for tracing-subscriber
0.4...
Some other things:
|
Yeah @jaskij so if I test with a span, and diff --git a/not_working/src/bin/tracing.rs b/not_working/src/bin/tracing.rs
index 76a1799..7861533 100644
--- a/not_working/src/bin/tracing.rs
+++ b/not_working/src/bin/tracing.rs
@@ -2,7 +2,7 @@
mod valuable_anyhow;
use anyhow::anyhow;
-use tracing::info;
+use tracing::{info, info_span};
use valuable::Valuable;
use valuable_anyhow::ValuableAnyhow;
@@ -14,5 +14,6 @@ fn main() {
let err = anyhow!("asdf FOO");
let va = ValuableAnyhow::from(err);
- info!(test=va.as_value());
+ // info!(test=va.as_value());
+ info_span!("foo", test=va.as_value());
}
Not sure if that is good enough for the Span case, since everyone is probably building with |
@hawkw Regarding your comment about being able to actually see the original error (since we are just losing it due to Since that's a breaking change, one thing we could do in the meantime (maybe) would be to print the error directly for now: visit().map_err(|e| {
// we can also add event.name here etc I think
eprintln("Unable to serialize to JSON due to: {:?}", e);
fmt::Error
})?; |
When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. This patch adds a single `eprintln` line to let the user know that we were unable to format a specific event. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves tokio-rs#1965.
22b5873
to
c725c8a
Compare
I amended the original commit (wanted to correct the commit message) |
533e1f1
to
9be1962
Compare
…lder Update the error-handling logic when calling `format_event` to write the error message to the Writer. Only if that fails do we write to stderr. Define new method `SubscriberBuilder::with_silence_errors(bool)` so that a user may disable this behavior. Enabled by default. Add unit tests for both `silence_errors=true` and `false`.
9be1962
to
aa64320
Compare
Ready for a second review :) |
…om:bryangarza/tracing into feat-1965-stderr-for-subscriber-fmt-errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we should make this a toggle; this should probably be on by-default. It's not a breaking change, and if people are encountered this issue in the wild, I'd rather have subscriber break their log ingestion system loudly.
I think it should definitely be possible to turn it off, but I'm fine with making it default to on. |
Fair enough! In that case, the builder option should probably be renamed to something more descriptive. maybe |
This patch updates `with_silence_errors` to be named `with_silence_internal_errors`, and also updates the rustdocs so that it's more clear what its behavior is.
I renamed to |
This patch renames the `with_silence_internal_errors` flag to simply `log_internal_errors`, and switches the default value to `true` since the flag now represents the opposite idea.
Renamed to |
All feedback up to this point has been addressed :)
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
…ter (#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves #1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
# 0.3.16 (October 6, 2022) This release of `tracing-subscriber` fixes a regression introduced in [v0.3.15][subscriber-0.3.15] where `Option::None`'s `Layer` implementation would set the max level hint to `OFF`. In addition, it adds several new APIs, including the `Filter::event_enabled` method for filtering events based on fields values, and the ability to log internal errors that occur when writing a log line. This release also replaces the dependency on the unmaintained [`ansi-term`] crate with the [`nu-ansi-term`] crate, resolving an *informational* security advisory ([RUSTSEC-2021-0139] for [`ansi-term`]'s maintainance status. This increases the minimum supported Rust version (MSRV) to Rust 1.50+, although the crate should still compile for the previous MSRV of Rust 1.49+ when the `ansi` feature is not enabled. ### Fixed - **layer**: `Option::None`'s `Layer` impl always setting the `max_level_hint` to `LevelFilter::OFF` (#2321) - Compilation with `-Z minimal versions` (#2246) - **env-filter**: Clarify that disabled level warnings are emitted by `tracing-subscriber` (#2285) ### Added - **fmt**: Log internal errors to `stderr` if writing a log line fails (#2102) - **fmt**: `FmtLayer::log_internal_errors` and `FmtSubscriber::log_internal_errors` methods for configuring whether internal writer errors are printed to `stderr` (#2102) - **fmt**: `#[must_use]` attributes on builders to warn if a `Subscriber` is configured but not set as the default subscriber (#2239) - **filter**: `Filter::event_enabled` method for filtering an event based on its fields (#2245, #2251) - **filter**: `Targets::default_level` accessor (#2242) ### Changed - **ansi**: Replaced dependency on unmaintained `ansi-term` crate with `nu-ansi-term` ((#2287, fixes informational advisory [RUSTSEC-2021-0139]) - `tracing-core`: updated to [0.1.30][core-0.1.30] - Minimum Supported Rust Version (MSRV) increased to Rust 1.50+ (when the `ansi`) feature flag is enabled (#2287) ### Documented - **fmt**: Correct inaccuracies in `fmt::init` documentation (#2224) - **filter**: Fix incorrect doc link in `filter::Not` combinator (#2249) Thanks to new contributors @cgbur, @DesmondWillowbrook, @RalfJung, and @poliorcetics, as well as returning contributors @CAD97, @connec, @jswrenn, @guswynn, and @bryangarza, for contributing to this release! [nu-ansi-term]: https://github.com/nushell/nu-ansi-term [ansi_term]: https://github.com/ogham/rust-ansi-term [RUSTSEC-2021-0139]: https://rustsec.org/advisories/RUSTSEC-2021-0139.html [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [subscriber-0.3.15]: https://github.com/tokio-rs/tracing/releases/tag/tracing-subscriber-0.3.15
# 0.3.16 (October 6, 2022) This release of `tracing-subscriber` fixes a regression introduced in [v0.3.15][subscriber-0.3.15] where `Option::None`'s `Layer` implementation would set the max level hint to `OFF`. In addition, it adds several new APIs, including the `Filter::event_enabled` method for filtering events based on fields values, and the ability to log internal errors that occur when writing a log line. This release also replaces the dependency on the unmaintained [`ansi-term`] crate with the [`nu-ansi-term`] crate, resolving an *informational* security advisory ([RUSTSEC-2021-0139] for [`ansi-term`]'s maintainance status. This increases the minimum supported Rust version (MSRV) to Rust 1.50+, although the crate should still compile for the previous MSRV of Rust 1.49+ when the `ansi` feature is not enabled. ### Fixed - **layer**: `Option::None`'s `Layer` impl always setting the `max_level_hint` to `LevelFilter::OFF` (#2321) - Compilation with `-Z minimal versions` (#2246) - **env-filter**: Clarify that disabled level warnings are emitted by `tracing-subscriber` (#2285) ### Added - **fmt**: Log internal errors to `stderr` if writing a log line fails (#2102) - **fmt**: `FmtLayer::log_internal_errors` and `FmtSubscriber::log_internal_errors` methods for configuring whether internal writer errors are printed to `stderr` (#2102) - **fmt**: `#[must_use]` attributes on builders to warn if a `Subscriber` is configured but not set as the default subscriber (#2239) - **filter**: `Filter::event_enabled` method for filtering an event based on its fields (#2245, #2251) - **filter**: `Targets::default_level` accessor (#2242) ### Changed - **ansi**: Replaced dependency on unmaintained `ansi-term` crate with `nu-ansi-term` ((#2287, fixes informational advisory [RUSTSEC-2021-0139]) - `tracing-core`: updated to [0.1.30][core-0.1.30] - Minimum Supported Rust Version (MSRV) increased to Rust 1.50+ (when the `ansi`) feature flag is enabled (#2287) ### Documented - **fmt**: Correct inaccuracies in `fmt::init` documentation (#2224) - **filter**: Fix incorrect doc link in `filter::Not` combinator (#2249) Thanks to new contributors @cgbur, @DesmondWillowbrook, @RalfJung, and @poliorcetics, as well as returning contributors @CAD97, @connec, @jswrenn, @guswynn, and @bryangarza, for contributing to this release! [nu-ansi-term]: https://github.com/nushell/nu-ansi-term [ansi_term]: https://github.com/ogham/rust-ansi-term [RUSTSEC-2021-0139]: https://rustsec.org/advisories/RUSTSEC-2021-0139.html [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [subscriber-0.3.15]: https://github.com/tokio-rs/tracing/releases/tag/tracing-subscriber-0.3.15
# 0.3.16 (October 6, 2022) This release of `tracing-subscriber` fixes a regression introduced in [v0.3.15][subscriber-0.3.15] where `Option::None`'s `Layer` implementation would set the max level hint to `OFF`. In addition, it adds several new APIs, including the `Filter::event_enabled` method for filtering events based on fields values, and the ability to log internal errors that occur when writing a log line. This release also replaces the dependency on the unmaintained [`ansi-term`] crate with the [`nu-ansi-term`] crate, resolving an *informational* security advisory ([RUSTSEC-2021-0139] for [`ansi-term`]'s maintainance status. This increases the minimum supported Rust version (MSRV) to Rust 1.50+, although the crate should still compile for the previous MSRV of Rust 1.49+ when the `ansi` feature is not enabled. ### Fixed - **layer**: `Option::None`'s `Layer` impl always setting the `max_level_hint` to `LevelFilter::OFF` (#2321) - Compilation with `-Z minimal versions` (#2246) - **env-filter**: Clarify that disabled level warnings are emitted by `tracing-subscriber` (#2285) ### Added - **fmt**: Log internal errors to `stderr` if writing a log line fails (#2102) - **fmt**: `FmtLayer::log_internal_errors` and `FmtSubscriber::log_internal_errors` methods for configuring whether internal writer errors are printed to `stderr` (#2102) - **fmt**: `#[must_use]` attributes on builders to warn if a `Subscriber` is configured but not set as the default subscriber (#2239) - **filter**: `Filter::event_enabled` method for filtering an event based on its fields (#2245, #2251) - **filter**: `Targets::default_level` accessor (#2242) ### Changed - **ansi**: Replaced dependency on unmaintained `ansi-term` crate with `nu-ansi-term` ((#2287, fixes informational advisory [RUSTSEC-2021-0139]) - `tracing-core`: updated to [0.1.30][core-0.1.30] - Minimum Supported Rust Version (MSRV) increased to Rust 1.50+ (when the `ansi`) feature flag is enabled (#2287) ### Documented - **fmt**: Correct inaccuracies in `fmt::init` documentation (#2224) - **filter**: Fix incorrect doc link in `filter::Not` combinator (#2249) Thanks to new contributors @cgbur, @DesmondWillowbrook, @RalfJung, and @poliorcetics, as well as returning contributors @CAD97, @connec, @jswrenn, @guswynn, and @bryangarza, for contributing to this release! [nu-ansi-term]: https://github.com/nushell/nu-ansi-term [ansi_term]: https://github.com/ogham/rust-ansi-term [RUSTSEC-2021-0139]: https://rustsec.org/advisories/RUSTSEC-2021-0139.html [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [subscriber-0.3.15]: https://github.com/tokio-rs/tracing/releases/tag/tracing-subscriber-0.3.15
…ter (tokio-rs#2102) Motivation: When `Format_event::format_event(...)` returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped. Solution: Write to the Writer with an error message to let the user know that we were unable to format a specific event. If writing to the Writer fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again). Resolves tokio-rs#1965. Co-authored-by: Eliza Weisman <eliza@buoyant.io> Co-authored-by: David Barsky <me@davidbarsky.com>
# 0.3.16 (October 6, 2022) This release of `tracing-subscriber` fixes a regression introduced in [v0.3.15][subscriber-0.3.15] where `Option::None`'s `Layer` implementation would set the max level hint to `OFF`. In addition, it adds several new APIs, including the `Filter::event_enabled` method for filtering events based on fields values, and the ability to log internal errors that occur when writing a log line. This release also replaces the dependency on the unmaintained [`ansi-term`] crate with the [`nu-ansi-term`] crate, resolving an *informational* security advisory ([RUSTSEC-2021-0139] for [`ansi-term`]'s maintainance status. This increases the minimum supported Rust version (MSRV) to Rust 1.50+, although the crate should still compile for the previous MSRV of Rust 1.49+ when the `ansi` feature is not enabled. ### Fixed - **layer**: `Option::None`'s `Layer` impl always setting the `max_level_hint` to `LevelFilter::OFF` (tokio-rs#2321) - Compilation with `-Z minimal versions` (tokio-rs#2246) - **env-filter**: Clarify that disabled level warnings are emitted by `tracing-subscriber` (tokio-rs#2285) ### Added - **fmt**: Log internal errors to `stderr` if writing a log line fails (tokio-rs#2102) - **fmt**: `FmtLayer::log_internal_errors` and `FmtSubscriber::log_internal_errors` methods for configuring whether internal writer errors are printed to `stderr` (tokio-rs#2102) - **fmt**: `#[must_use]` attributes on builders to warn if a `Subscriber` is configured but not set as the default subscriber (tokio-rs#2239) - **filter**: `Filter::event_enabled` method for filtering an event based on its fields (tokio-rs#2245, tokio-rs#2251) - **filter**: `Targets::default_level` accessor (tokio-rs#2242) ### Changed - **ansi**: Replaced dependency on unmaintained `ansi-term` crate with `nu-ansi-term` ((tokio-rs#2287, fixes informational advisory [RUSTSEC-2021-0139]) - `tracing-core`: updated to [0.1.30][core-0.1.30] - Minimum Supported Rust Version (MSRV) increased to Rust 1.50+ (when the `ansi`) feature flag is enabled (tokio-rs#2287) ### Documented - **fmt**: Correct inaccuracies in `fmt::init` documentation (tokio-rs#2224) - **filter**: Fix incorrect doc link in `filter::Not` combinator (tokio-rs#2249) Thanks to new contributors @cgbur, @DesmondWillowbrook, @RalfJung, and @poliorcetics, as well as returning contributors @CAD97, @connec, @jswrenn, @guswynn, and @bryangarza, for contributing to this release! [nu-ansi-term]: https://github.com/nushell/nu-ansi-term [ansi_term]: https://github.com/ogham/rust-ansi-term [RUSTSEC-2021-0139]: https://rustsec.org/advisories/RUSTSEC-2021-0139.html [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [subscriber-0.3.15]: https://github.com/tokio-rs/tracing/releases/tag/tracing-subscriber-0.3.15
Motivation
When
Format_event::format_event(...)
returns an error, we are currently silently dropping that Event. tokio-rs/valuable#88 explains one such case in which this was encountered (due to a bug in valuable-serde). We want to be made aware whenever an Event is dropped.Solution
Write to the
Writer
with an error message to let the user know that we were unable to format a specific event. If writing to theWriter
fails, we fall back to writing to stderr. We are not emitting an actual tracing Event, to avoid the risk of a cycle (the new Event could trigger the same formatting error again).Resolves #1965.
Testing
Added unit tests in
fmt_layer.rs
.I also tested using @jaskij's repo: https://github.com/jaskij/valuable-serde-bug-reproduction.
I tried adding a Span, which then panics (when
debug_assertions=true
):I think the Span case should be good enough (without addressing it in this PR)? I think anyone testing their code will hit the panic, because non-release builds do have
debug_assertions=true
by default.Open Questions
Should I add a method toSubscriberBuilder
to be able to enable/disable this behavior? If so, what should be the default, on or off?Something like:Edit: I did update the builder, in my second commit.