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

tracing-subscriber: LocalTime doesn't work by default on unix systems, no warning of --cfg unsound_local_offset #1688

Closed
lilyball opened this issue Oct 26, 2021 · 1 comment · Fixed by #1699
Assignees
Labels
crate/subscriber Related to the `tracing-subscriber` crate kind/bug Something isn't working kind/docs

Comments

@lilyball
Copy link
Contributor

Bug Report

Version

tracing-slog v0.1.0 (/Users/lily/Dev/Work/rust-crates/crates/tracing-slog)
└── tracing-core v0.1.21
├── tracing v0.1.29
│   ├── tracing-attributes v0.1.18 (proc-macro)
│   └── tracing-core v0.1.21 (*)
└── tracing-subscriber v0.3.1
    ├── tracing-core v0.1.21 (*)
    └── tracing-log v0.1.2
        └── tracing-core v0.1.21 (*)

Platform

Darwin DeerBook 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64

Crates

tracing-subscriber

Description

Attempting to use .with_timer with a LocalTime argument cause all tracing events to vanish. The problem seems to be that on unix systems the time crate refuses to fetch the local offset without --cfg unsound_local_offset, and this gets turned into an error which bubbles up through the formatting until it gets to tracing_subscriber::fmt::FmtLayer::on_event() which just throws away the error and logs nothing.

This is rather surprising behavior, all the more-so that it isn't documented. Even the time crate itself doesn't give a good explanation of this feature, and doesn't warn that OffsetDateTime::now_local() doesn't work on unix systems without it. I only figured it out by looking through the source.

It also seems to be rather poor behavior to throw away the whole tracing event because the local time offset couldn't be fetched. Surely there's some fallback behavior that could be used here. I'd suggest just falling back to UTC except if the time formatter doesn't show offset then this could be confusing. It could of course just fall back to the default timestamp formatter, as if I never called .with_timer().

In any case, I feel like at a minimum this should be documented properly, so it doesn't surprise everybody. And then I would suggest that FormatTime be extended with methods that allow for implementing easy fallback, so I can say e.g. "if LocalTime doesn't work, try this other formatter instead". And any error bubbled up from FormatTime should just result in using a default fallback instead of abandoning the whole event.

@hawkw
Copy link
Member

hawkw commented Oct 26, 2021

. Even the time crate itself doesn't give a good explanation of this feature, and doesn't warn that OffsetDateTime::now_local() doesn't work on unix systems without it. I only figured it out by looking through the source.

Whew, okay, I had no idea about this. Hmm. We should probably surface the feature flag in the docs, as well as adding a note. This was definitely not obvious from the time crate's docs.

It also seems to be rather poor behavior to throw away the whole tracing event because the local time offset couldn't be fetched. Surely there's some fallback behavior that could be used here. I'd suggest just falling back to UTC except if the time formatter doesn't show offset then this could be confusing. It could of course just fall back to the default timestamp formatter, as if I never called .with_timer().

Yeah, this should probably be fixed. Previously, getting the timestamp itself was always infallible, and the error would only be returned if formatting it failed...which, because it's being written to a String (the local buffer), would never happen. So, if writing to the buffer fails for some reason, it makes sense to just bail...but if getting the timestamp fails, it makes less sense. We should fix that as well.

@hawkw hawkw added crate/subscriber Related to the `tracing-subscriber` crate kind/bug Something isn't working labels Oct 26, 2021
@hawkw hawkw self-assigned this Oct 26, 2021
hawkw added a commit that referenced this issue Oct 26, 2021
## Motivation

Currently, `tracing_subscriber::fmt` will bail out of formatting the log
line when formatting the timestamp fails. Previously, when timestamps
were formatted using the `chrono` crate, this was the correct behavior,
as getting the timestamp was infallible, and an error would only be
returned if *formatting* the timestamp to the buffer fails. This should
never actually happen, because we are writing the timestamp to a string,
which should never fail; using `?` is just a bit more efficient than
`.expect` because it doesn't require generating unwinding code.

However, this is no longer the case when using the `time` crate. In the
`time` API, actually getting a timestamp is fallible, and in some cases,
will fail. The current code will bail out of formatting the entire log
line if getting the timestamp fails, which is not great --- using the
wrong timestamp formatter will result in silently dropping all log
lines.

## Solution

This branch changes the `format_timestamp` method to print `<unknown
time>` when the timestamp formatting fails, rather than bailing. This
way, we at least print the rest of the log line for the event.

This fixes half of the issue described in #1688 (the other half is
improving documentation).
hawkw added a commit that referenced this issue Oct 26, 2021
## Motivation

Currently, `tracing_subscriber::fmt` will bail out of formatting the log
line when formatting the timestamp fails. Previously, when timestamps
were formatted using the `chrono` crate, this was the correct behavior,
as getting the timestamp was infallible, and an error would only be
returned if *formatting* the timestamp to the buffer fails. This should
never actually happen, because we are writing the timestamp to a string,
which should never fail; using `?` is just a bit more efficient than
`.expect` because it doesn't require generating unwinding code.

However, this is no longer the case when using the `time` crate. In the
`time` API, actually getting a timestamp is fallible, and in some cases,
will fail. The current code will bail out of formatting the entire log
line if getting the timestamp fails, which is not great --- using the
wrong timestamp formatter will result in silently dropping all log
lines.

## Solution

This branch changes the `format_timestamp` method to print `<unknown
time>` when the timestamp formatting fails, rather than bailing. This
way, we at least print the rest of the log line for the event.

This fixes half of the issue described in #1688 (the other half is
improving documentation).
hawkw added a commit that referenced this issue Nov 3, 2021
## Motivation

The `time` crate must be compiled with `--cfg unsound_local_offset` in
order for local timestamps to be enabled. For users whose first exposure
to the `time` crate's API is via `tracing-subscriber`'s `time` timestamp
formatters, this is potentially _very_ surprising! Therefore, although
this cfg is not part of `tracing`'s API surface, we should probably
document this aspect of `time`'s API in the
`tracing-subscriber::fmt::time` documentation.

## Solution

This branch adds warnings in the `time::LocalTime` type's API docs, in
the struct-level documentation and on the `new` constructor, describing
that `--cfg unsound_local_offset` is necessary to record local
timestamps and referring users to the `time` documentation. I also added
`unsound_local_offset` to the `doc(cfg(...))` attributes for the
`LocalTime` type.

While I was changing `tracing-subscriber`'s docs, I also fixed a couple
formatting issues I noticed.

Fixes #1688
hawkw added a commit that referenced this issue Nov 3, 2021
## Motivation

The `time` crate must be compiled with `--cfg unsound_local_offset` in
order for local timestamps to be enabled. For users whose first exposure
to the `time` crate's API is via `tracing-subscriber`'s `time` timestamp
formatters, this is potentially _very_ surprising! Therefore, although
this cfg is not part of `tracing`'s API surface, we should probably
document this aspect of `time`'s API in the
`tracing-subscriber::fmt::time` documentation.

## Solution

This branch adds warnings in the `time::LocalTime` type's API docs, in
the struct-level documentation and on the `new` constructor, describing
that `--cfg unsound_local_offset` is necessary to record local
timestamps and referring users to the `time` documentation. I also added
`unsound_local_offset` to the `doc(cfg(...))` attributes for the
`LocalTime` type.

While I was changing `tracing-subscriber`'s docs, I also fixed a couple
formatting issues I noticed.

Fixes #1688
hawkw added a commit that referenced this issue Nov 30, 2021
## Motivation

Currently, `tracing_subscriber::fmt` will bail out of formatting the log
line when formatting the timestamp fails. Previously, when timestamps
were formatted using the `chrono` crate, this was the correct behavior,
as getting the timestamp was infallible, and an error would only be
returned if *formatting* the timestamp to the buffer fails. This should
never actually happen, because we are writing the timestamp to a string,
which should never fail; using `?` is just a bit more efficient than
`.expect` because it doesn't require generating unwinding code.

However, this is no longer the case when using the `time` crate. In the
`time` API, actually getting a timestamp is fallible, and in some cases,
will fail. The current code will bail out of formatting the entire log
line if getting the timestamp fails, which is not great --- using the
wrong timestamp formatter will result in silently dropping all log
lines.

## Solution

This branch changes the `format_timestamp` method to print `<unknown
time>` when the timestamp formatting fails, rather than bailing. This
way, we at least print the rest of the log line for the event.

This fixes half of the issue described in #1688 (the other half is
improving documentation).
hawkw added a commit that referenced this issue Dec 1, 2021
## Motivation

The `time` crate must be compiled with `--cfg unsound_local_offset` in
order for local timestamps to be enabled. For users whose first exposure
to the `time` crate's API is via `tracing-subscriber`'s `time` timestamp
formatters, this is potentially _very_ surprising! Therefore, although
this cfg is not part of `tracing`'s API surface, we should probably
document this aspect of `time`'s API in the
`tracing-subscriber::fmt::time` documentation.

## Solution

This branch adds warnings in the `time::LocalTime` type's API docs, in
the struct-level documentation and on the `new` constructor, describing
that `--cfg unsound_local_offset` is necessary to record local
timestamps and referring users to the `time` documentation. I also added
`unsound_local_offset` to the `doc(cfg(...))` attributes for the
`LocalTime` type.

While I was changing `tracing-subscriber`'s docs, I also fixed a couple
formatting issues I noticed.

Fixes #1688
hawkw added a commit that referenced this issue Dec 1, 2021
## Motivation

Currently, `tracing_subscriber::fmt` will bail out of formatting the log
line when formatting the timestamp fails. Previously, when timestamps
were formatted using the `chrono` crate, this was the correct behavior,
as getting the timestamp was infallible, and an error would only be
returned if *formatting* the timestamp to the buffer fails. This should
never actually happen, because we are writing the timestamp to a string,
which should never fail; using `?` is just a bit more efficient than
`.expect` because it doesn't require generating unwinding code.

However, this is no longer the case when using the `time` crate. In the
`time` API, actually getting a timestamp is fallible, and in some cases,
will fail. The current code will bail out of formatting the entire log
line if getting the timestamp fails, which is not great --- using the
wrong timestamp formatter will result in silently dropping all log
lines.

## Solution

This branch changes the `format_timestamp` method to print `<unknown
time>` when the timestamp formatting fails, rather than bailing. This
way, we at least print the rest of the log line for the event.

This fixes half of the issue described in #1688 (the other half is
improving documentation).
hawkw added a commit that referenced this issue Dec 1, 2021
## Motivation

The `time` crate must be compiled with `--cfg unsound_local_offset` in
order for local timestamps to be enabled. For users whose first exposure
to the `time` crate's API is via `tracing-subscriber`'s `time` timestamp
formatters, this is potentially _very_ surprising! Therefore, although
this cfg is not part of `tracing`'s API surface, we should probably
document this aspect of `time`'s API in the
`tracing-subscriber::fmt::time` documentation.

## Solution

This branch adds warnings in the `time::LocalTime` type's API docs, in
the struct-level documentation and on the `new` constructor, describing
that `--cfg unsound_local_offset` is necessary to record local
timestamps and referring users to the `time` documentation. I also added
`unsound_local_offset` to the `doc(cfg(...))` attributes for the
`LocalTime` type.

While I was changing `tracing-subscriber`'s docs, I also fixed a couple
formatting issues I noticed.

Fixes #1688
d-e-s-o added a commit to d-e-s-o/apcacli that referenced this issue Dec 16, 2021
This change updates the tracing-subscriber dependency we consume to
version 0.3.
Note that we stop emitting local time stamps in traces with this change
(i.e., they will now be for the UTC time zone). The new version comes
with a 'local-time' feature state switch but unless we jump through huge
hoops (and have to accept unsound code), trace messages are just
silently swallowed. Please refer to
tokio-rs/tracing#1688 for the glory details.
d-e-s-o added a commit to d-e-s-o/apcacli that referenced this issue Dec 19, 2021
This change updates the tracing-subscriber dependency we consume to
version 0.3.
Note that we stop emitting local time stamps in traces with this change
(i.e., they will now be for the UTC time zone). The new version comes
with a 'local-time' feature state switch but unless we jump through huge
hoops (and have to accept unsound code), trace messages are just
silently swallowed. Please refer to
tokio-rs/tracing#1688 for the glory details.
d-e-s-o added a commit to d-e-s-o/apcaledge that referenced this issue Jan 10, 2022
This change updates the tracing-subscriber dependency we consume to
version 0.3.
Note that we stop emitting local time stamps in traces with this change
(i.e., they will now be for the UTC time zone). The new version comes
with a 'local-time' feature state switch but unless we jump through huge
hoops (and have to accept unsound code), trace messages are just
silently swallowed. Please refer to
tokio-rs/tracing#1688 for the glory details.
d-e-s-o added a commit to d-e-s-o/apcaledge that referenced this issue Jan 30, 2022
This change updates the tracing-subscriber dependency we consume to
version 0.3.
Note that we stop emitting local time stamps in traces with this change
(i.e., they will now be for the UTC time zone). The new version comes
with a 'local-time' feature state switch but unless we jump through huge
hoops (and have to accept unsound code), trace messages are just
silently swallowed. Please refer to
tokio-rs/tracing#1688 for the glory details.
d-e-s-o added a commit to d-e-s-o/cargo-http-registry that referenced this issue Mar 2, 2022
This change updates the tracing-subscriber dependency we consume to
version 0.3.
Note that we stop emitting local time stamps in traces with this change
(i.e., they will now be for the UTC time zone). The new version comes
with a 'local-time' feature state switch but unless we jump through huge
hoops (and have to accept unsound code), trace messages are just
silently swallowed. Please refer to
tokio-rs/tracing#1688 for the glory details.
We could use the recently added OffsetTime, but it has the downside of
not reacting to time zone changes, which may cause confusion for a long
running service. There is nothing wrong with emitting UTC time stamps,
though, so that is what we do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/subscriber Related to the `tracing-subscriber` crate kind/bug Something isn't working kind/docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants