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

Use circular buffer for zap logs #585

Merged
merged 80 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
9d3ebe4
init branch
fenogentov May 9, 2022
1c78d75
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 9, 2022
c96465e
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 9, 2022
0849a43
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 9, 2022
b0b67ff
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 10, 2022
f2a731d
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 10, 2022
8a29207
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 10, 2022
b0e5a57
fix
fenogentov May 10, 2022
634c492
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 10, 2022
81c13a4
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 11, 2022
2f887b7
fix
fenogentov May 11, 2022
599c327
small rollback
fenogentov May 11, 2022
e5d6e43
fix
fenogentov May 11, 2022
8acd12e
fix
fenogentov May 11, 2022
082dcf8
fix
fenogentov May 11, 2022
9740907
fix
fenogentov May 11, 2022
000f92f
fix
fenogentov May 11, 2022
01c2c58
fix
fenogentov May 11, 2022
727ec82
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 12, 2022
c6c0046
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 12, 2022
3250303
added buffer size
fenogentov May 12, 2022
1ea7532
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 12, 2022
db087bc
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
8eb7130
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
8caedb9
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
6899784
fix `getLogRAM`
fenogentov May 12, 2022
7de6cfb
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 12, 2022
12c42ca
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 13, 2022
7b2aca7
fix
fenogentov May 13, 2022
07af1b1
fix
fenogentov May 13, 2022
e97e793
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 16, 2022
e362928
fix
fenogentov May 16, 2022
cdae5c9
Merge branch 'main' into issue-564-BufferZap
AlekSi May 16, 2022
a478c65
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 18, 2022
8cad812
Merge branch 'main' into issue-564-BufferZap
May 18, 2022
ddfe1ce
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 19, 2022
2290a67
refactoring logram
fenogentov May 19, 2022
c4ea95f
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 19, 2022
63e7844
redone logram_test
fenogentov May 19, 2022
62ad583
fix
fenogentov May 19, 2022
6a670f8
fix
fenogentov May 23, 2022
2bc98a9
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 23, 2022
0e3f9e9
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 26, 2022
34e3fc3
fix
fenogentov May 26, 2022
3eb53d1
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
db998bd
Apply suggestions from code review
fenogentov May 26, 2022
c27deff
fix
fenogentov May 26, 2022
4254acc
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
39653cf
fix
fenogentov May 26, 2022
3140c32
fix test
fenogentov May 26, 2022
2e8ad6c
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 26, 2022
6458ed1
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 26, 2022
69e0935
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 26, 2022
8c5c33b
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
15d2c60
Merge branch 'main' into issue-564-BufferZap
AlekSi May 30, 2022
26718ec
Apply suggestions from code review
fenogentov May 30, 2022
dc477ac
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 30, 2022
f5bcc10
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 30, 2022
4ce0505
new names
fenogentov May 30, 2022
7da50d0
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 30, 2022
7b89880
fix commentary
fenogentov May 30, 2022
861055a
fix
fenogentov May 30, 2022
34a7b2e
fix test
fenogentov May 30, 2022
7d3d331
fix test
fenogentov May 30, 2022
e33a1b2
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 30, 2022
af30675
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 30, 2022
7d5578d
Merge branch 'main' into issue-564-BufferZap
AlekSi May 30, 2022
bf4535a
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 31, 2022
b54cec8
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 31, 2022
55c998b
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 31, 2022
032c1b2
Apply suggestions from code review
fenogentov May 31, 2022
3be5927
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov Jun 2, 2022
80244ea
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 2, 2022
61694b3
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov Jun 2, 2022
8937f69
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 3, 2022
9ba4109
Merge branch 'main' into issue-564-BufferZap
AlekSi Jun 6, 2022
8adf2f2
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 6, 2022
6275b60
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov Jun 6, 2022
8e3e578
Rename files
AlekSi Jun 6, 2022
9486c10
Add package description
AlekSi Jun 6, 2022
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
13 changes: 10 additions & 3 deletions internal/util/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ func Setup(level zapcore.Level) {
log.Fatal(err)
}

SetupWithLogger(logger)
RecentEntries = NewCircularBuffer(1024)

logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
RecentEntries.append(&entry)
return nil
}))

setupWithLogger(logger)
}

// SetupWithLogger initializes logging with a given logger and its level.
func SetupWithLogger(logger *zap.Logger) {
// setupWithLogger initializes logging with a given logger and its level.
func setupWithLogger(logger *zap.Logger) {
zap.ReplaceGlobals(logger)

if _, err := zap.RedirectStdLogAt(logger, zap.InfoLevel); err != nil {
Expand Down
71 changes: 71 additions & 0 deletions internal/util/logging/logram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging
seeforschauer marked this conversation as resolved.
Show resolved Hide resolved
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"sync"

"go.uber.org/zap/zapcore"
)

// RecentEntries implements zap logging entries interception
// and stores the last 1024 entries in circular buffer in memory.
var RecentEntries *circularBuffer

// circularBuffer is a storage of log records in memory.
type circularBuffer struct {
mu sync.RWMutex
log []*zapcore.Entry
index int64
}

// NewCircularBuffer creates a circular buffer for log entries in memory.
func NewCircularBuffer(size int64) *circularBuffer {
if size < 1 {
panic(fmt.Sprintf("buffer size must be at least 1, but %d provided", size))
}

return &circularBuffer{
log: make([]*zapcore.Entry, size),
}
}

// append adds an entry in circularBuffer.
func (l *circularBuffer) append(entry *zapcore.Entry) {
l.mu.Lock()
defer l.mu.Unlock()

l.log[l.index] = entry
l.index = (l.index + 1) % int64(len(l.log))
}

// Get returns entries from circularBuffer.
func (l *circularBuffer) Get() []*zapcore.Entry {
l.mu.RLock()
defer l.mu.RUnlock()

n := len(l.log)
entries := make([]*zapcore.Entry, 0, n)
for i := int64(0); i < int64(len(l.log)); i++ {
k := (i + l.index) % int64(len(l.log))
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

if l.log[k] != nil {
entries = append(entries, l.log[k])
}
}

return entries
}
140 changes: 140 additions & 0 deletions internal/util/logging/logram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestCircularBuffer(t *testing.T) {
for name, tc := range map[string]struct {
size int64
msgPanic string
}{
"PanicNegativSize": {
size: -2,
msgPanic: "buffer size must be at least 1, but -2 provided",
},
"PanicZeroSize": {
size: 0,
msgPanic: "buffer size must be at least 1, but 0 provided",
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
assert.PanicsWithValue(t, tc.msgPanic, func() { NewCircularBuffer(tc.size) })
})
}

logram := NewCircularBuffer(2)
for n, tc := range []struct {
inLog zapcore.Entry
expected []zapcore.Entry
}{{
inLog: zapcore.Entry{
Level: 1,
Time: time.Date(2022, 12, 31, 11, 59, 1, 0, time.UTC),
LoggerName: "logger_1",
Message: "message 1",
},
expected: []zapcore.Entry{{
Level: 1,
Time: time.Date(2022, 12, 31, 11, 59, 1, 0, time.UTC),
LoggerName: "logger_1",
Message: "message 1",
}},
}, {
inLog: zapcore.Entry{
Level: 2,
Time: time.Date(2022, 12, 31, 11, 59, 2, 0, time.UTC),
LoggerName: "logger_2",
Message: "message 2",
},
expected: []zapcore.Entry{{
Level: 1,
Time: time.Date(2022, 12, 31, 11, 59, 1, 0, time.UTC),
LoggerName: "logger_1",
Message: "message 1",
}, {
Level: 2,
Time: time.Date(2022, 12, 31, 11, 59, 2, 0, time.UTC),
LoggerName: "logger_2",
Message: "message 2",
}},
}, {
inLog: zapcore.Entry{
Level: 3,
Time: time.Date(2022, 12, 31, 11, 59, 3, 0, time.UTC),
LoggerName: "logger_3",
Message: "message 3",
},
expected: []zapcore.Entry{{
Level: 2,
Time: time.Date(2022, 12, 31, 11, 59, 2, 0, time.UTC),
LoggerName: "logger_2",
Message: "message 2",
}, {
Level: 3,
Time: time.Date(2022, 12, 31, 11, 59, 3, 0, time.UTC),
LoggerName: "logger_3",
Message: "message 3",
}},
}} {
name := fmt.Sprintf("AppendGet_%d", n)
tc := tc
t.Run(name, func(t *testing.T) {
logram.append(&tc.inLog)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
actual := logram.Get()
for i, exp := range tc.expected {
assert.Equal(t, exp, *actual[i])
}
})
}

Setup(zap.DebugLevel)
logger := zap.L()
for n, tc := range []struct {
addMsg string
expected []string
}{{
addMsg: "Test message 1",
expected: []string{"Test message 1"},
}, {
addMsg: "Test message 2",
expected: []string{"Test message 1", "Test message 2"},
}, {
addMsg: "Test message 3",
expected: []string{"Test message 1", "Test message 2", "Test message 3"},
}, {
addMsg: "Test message 4",
expected: []string{"Test message 1", "Test message 2", "Test message 3", "Test message 4"},
}} {
name := fmt.Sprintf("ZapHooks_%d", n)
tc := tc
t.Run(name, func(t *testing.T) {
logger.Info(tc.addMsg)
actual := RecentEntries.Get()
for i, exp := range tc.expected {
assert.Equal(t, exp, actual[i].Message)
}
})
}
}