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

rotation - possibility of log rotations of 2,3,4,5,x minutes or hours #11602

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions libbeat/common/file/interval_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type intervalRotator struct {
fileFormat string
clock clock
weekly bool
yearly bool
arbitrary bool
newInterval func(lastTime time.Time, currentTime time.Time) bool
}
Expand Down Expand Up @@ -61,38 +62,31 @@ func newIntervalRotator(interval time.Duration) (*intervalRotator, error) {
func (r *intervalRotator) initialize() error {
r.clock = realClock{}

switch r.interval {
case time.Second:
if r.interval < time.Minute {
Copy link

Choose a reason for hiding this comment

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

nit: better use switch statements instead of if-elseif-else...

switch {
case r.interval < time.Minute:
   ...
case r.interval < time.Hour:
   ...
...
default:
    ...
}

How about defining some constants like const day = 24 * time.Hour?

Moving this selection into an extra function makes it testable (often it's the most simplest code that contains the bug :) ).

r.fileFormat = "2006-01-02-15-04-05"
r.newInterval = newSecond
case time.Minute:
} else if r.interval < time.Hour {
r.fileFormat = "2006-01-02-15-04"
r.newInterval = newMinute
case time.Hour:
} else if r.interval < 24*time.Hour {
r.fileFormat = "2006-01-02-15"
r.newInterval = newHour
case 24 * time.Hour: // calendar day
} else if r.interval < 7*24*time.Hour {
r.fileFormat = "2006-01-02"
r.newInterval = newDay
case 7 * 24 * time.Hour: // calendar week
} else if r.interval < 30*24*time.Hour {
r.fileFormat = ""
r.newInterval = newWeek
r.weekly = true
case 30 * 24 * time.Hour: // calendar month
r.newInterval = newWeek
} else if r.interval < 365*24*time.Hour {
r.fileFormat = "2006-01"
r.newInterval = newMonth
case 365 * 24 * time.Hour: // calendar year
} else if r.interval >= 365*24*time.Hour {
r.fileFormat = "2006"
r.yearly = true
r.newInterval = newYear
default:
r.arbitrary = true
r.fileFormat = "2006-01-02-15-04-05"
r.newInterval = func(lastTime time.Time, currentTime time.Time) bool {
lastInterval := lastTime.Unix() / (int64(r.interval) / int64(time.Second))
currentInterval := currentTime.Unix() / (int64(r.interval) / int64(time.Second))
return lastInterval != currentInterval
}
}

return nil
}

Expand All @@ -108,11 +102,6 @@ func (r *intervalRotator) LogPrefix(filename string, modTime time.Time) string {
y, w := t.ISOWeek()
return fmt.Sprintf("%s-%04d-%02d-", filename, y, w)
}
if r.arbitrary {
intervalNumber := t.Unix() / (int64(r.interval) / int64(time.Second))
intervalStart := time.Unix(0, intervalNumber*int64(r.interval))
return fmt.Sprintf("%s-%s-", filename, intervalStart.Format(r.fileFormat))
}
return fmt.Sprintf("%s-%s-", filename, t.Format(r.fileFormat))
}

Expand Down
43 changes: 0 additions & 43 deletions libbeat/common/file/interval_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,49 +209,6 @@ func TestYearlyRotator(t *testing.T) {
assert.Equal(t, "foo-2019-", a.LogPrefix("foo", time.Now()))
}

func TestArbitraryIntervalRotator(t *testing.T) {
a, err := newIntervalRotator(3 * time.Second)
if err != nil {
t.Fatal(err)
}

// Monday, 2018-Dec-31
clock := &testClock{time.Date(2018, 12, 31, 0, 0, 1, 0, time.Local)}
a.clock = clock
assert.Equal(t, "foo-2018-12-30-00-00-00-", a.LogPrefix("foo", time.Date(2018, 12, 30, 0, 0, 0, 0, time.Local)))
a.Rotate()
n := a.NewInterval()
assert.False(t, n)
assert.Equal(t, "foo-2018-12-31-00-00-00-", a.LogPrefix("foo", time.Now()))

clock.time = clock.time.Add(time.Second)
n = a.NewInterval()
assert.False(t, n)
assert.Equal(t, "foo-2018-12-31-00-00-00-", a.LogPrefix("foo", time.Now()))

clock.time = clock.time.Add(time.Second)
n = a.NewInterval()
assert.True(t, n)
a.Rotate()
assert.Equal(t, "foo-2018-12-31-00-00-03-", a.LogPrefix("foo", time.Now()))

clock.time = clock.time.Add(time.Second)
n = a.NewInterval()
assert.False(t, n)
assert.Equal(t, "foo-2018-12-31-00-00-03-", a.LogPrefix("foo", time.Now()))

clock.time = clock.time.Add(time.Second)
n = a.NewInterval()
assert.False(t, n)
assert.Equal(t, "foo-2018-12-31-00-00-03-", a.LogPrefix("foo", time.Now()))

clock.time = clock.time.Add(time.Second)
n = a.NewInterval()
assert.True(t, n)
a.Rotate()
assert.Equal(t, "foo-2018-12-31-00-00-06-", a.LogPrefix("foo", time.Now()))
}

func TestIntervalIsTruncatedToSeconds(t *testing.T) {
a, err := newIntervalRotator(2345 * time.Millisecond)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions libbeat/common/file/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ func (r *Rotator) rotate(reason rotateReason) error {

func (r *Rotator) rotateByInterval(reason rotateReason) error {
fi, err := os.Stat(r.filename)

if os.IsNotExist(err) {
return nil
} else if err != nil {
Expand Down