Skip to content

tracing-appender: Introduce weekly rotation #3218

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

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

Conversation

NickCaplinger
Copy link

Motivation

While configuring tracing-appender, I wanted to specify a weekly log rotation interval. I was unable to do so, as the largest rotation interval was daily.

Solution

Before my introduction of weekly log rotation, rounding the current OffsetDateTime was straightforward: we could simply keep the current date and truncate part or all of the time component. However, we cannot simply truncate the time with weekly rotation; the date must now be modified.

To round the date, I take its ordinal (how many days have passed since the start of the year) and round it backwards to the nearest 7 day period. This gives us consistent date-times that only change weekly.

@NickCaplinger NickCaplinger requested a review from a team as a code owner February 17, 2025 19:34
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 for your PR!

In general this looks good. However, I think that we should reconsider the way we pick the beginning of the week (see comment).

/// }
/// ```
///
/// This will result in a log file located at `/some/path/rolling.log.yyyy-MM-dd-HH`.
Copy link
Contributor

Choose a reason for hiding this comment

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

There shouldn't be an hour should there?

Suggested change
/// This will result in a log file located at `/some/path/rolling.log.yyyy-MM-dd-HH`.
/// This will result in a log file located at `/some/path/rolling.log.yyyy-MM-dd`.

I'm guessing this was copied from daily above (which is also wrong), so if you'd like to fix that too...

Comment on lines +539 to +543
date.replace_date(
Date::from_ordinal_date(date.year(), rounded_ordinal).expect(
"ordinal should never be out of range; this is a bug in tracing-appender",
),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel that this approach may be surprising to some users. It means that this year, the files will rotate on a Wednesday morning (UTC), but last year they would have rotated on a Monday.

Instead, I would suggest that we rotate on a fixed day of the week always. My preference would be on Sunday, because that's the day that cron uses for 0. This may not be what everyone wants either, but without providing a configurable option, we just have to pick a day.

We should also call out explicitly in the docs for weekly() when the rotation will occur, since I don't think that any option we choose will be obvious to everyone.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review, Hayden!

I've given this some thought and I agree with you. For my particular use case, I didn't care as much about the consistency, but I agree that this is probably going to be unexpected for users.

I really like the idea of matching cron behavior here. I'll work on those changes soon and get back to you.

Thanks again!

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.

2 participants