2019-04-08 18:47:15 +00:00
|
|
|
package logging
|
2017-04-03 21:48:50 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-16 22:41:50 +00:00
|
|
|
"context"
|
2017-04-03 21:48:50 +00:00
|
|
|
"crypto/rand"
|
2019-04-08 18:47:15 +00:00
|
|
|
"encoding/json"
|
2021-09-20 17:46:51 +00:00
|
|
|
stderrors "errors"
|
|
|
|
"io/fs"
|
2017-04-03 21:48:50 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2017-04-03 21:48:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-04-17 22:55:31 +00:00
|
|
|
func TestFilesystemLogger(t *testing.T) {
|
2019-07-16 22:41:50 +00:00
|
|
|
ctx := context.Background()
|
2021-08-11 14:02:22 +00:00
|
|
|
tempPath := t.TempDir()
|
|
|
|
require.NoError(t, os.Chmod(tempPath, 0755))
|
2019-04-08 18:47:15 +00:00
|
|
|
fileName := path.Join(tempPath, "filesystemLogWriter")
|
2020-09-09 20:33:32 +00:00
|
|
|
lgr, err := NewFilesystemLogWriter(fileName, log.NewNopLogger(), false, false)
|
2017-04-03 21:48:50 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
defer os.Remove(fileName)
|
|
|
|
|
2019-04-17 22:55:31 +00:00
|
|
|
var (
|
|
|
|
batches = 50
|
|
|
|
logCount = 100
|
|
|
|
logSize = 512
|
|
|
|
)
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
var logs []json.RawMessage
|
2019-04-17 22:55:31 +00:00
|
|
|
for i := 0; i < logCount; i++ {
|
|
|
|
randInput := make([]byte, logSize)
|
2019-04-08 18:47:15 +00:00
|
|
|
rand.Read(randInput)
|
|
|
|
logs = append(logs, randInput)
|
|
|
|
}
|
2017-04-03 21:48:50 +00:00
|
|
|
|
2019-04-17 22:55:31 +00:00
|
|
|
for i := 0; i < batches; i++ {
|
2019-07-16 22:41:50 +00:00
|
|
|
err := lgr.Write(ctx, logs)
|
2017-04-03 21:48:50 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
err = lgr.writer.Close()
|
2017-04-03 21:48:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// can't write to a closed logger
|
2019-07-16 22:41:50 +00:00
|
|
|
err = lgr.Write(ctx, logs)
|
2017-04-03 21:48:50 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
// call close twice noop
|
2019-04-08 18:47:15 +00:00
|
|
|
err = lgr.writer.Close()
|
2017-04-03 21:48:50 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
info, err := os.Stat(fileName)
|
|
|
|
require.Nil(t, err)
|
2019-04-17 22:55:31 +00:00
|
|
|
// + 1 below is for newlines that should be appended to each log
|
|
|
|
assert.Equal(t, int64(batches*logCount*(logSize+1)), info.Size())
|
2017-04-03 21:48:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:46:51 +00:00
|
|
|
// TestFilesystemLoggerPermission tests that NewFilesystemLogWriter fails
|
|
|
|
// if the process does not have permissions to write to the provided path.
|
|
|
|
func TestFilesystemLoggerPermission(t *testing.T) {
|
|
|
|
tempPath := t.TempDir()
|
|
|
|
require.NoError(t, os.Chmod(tempPath, 0000))
|
|
|
|
fileName := path.Join(tempPath, "filesystemLogWriter")
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
rotation bool
|
|
|
|
}{
|
|
|
|
{name: "with-rotation", rotation: true},
|
|
|
|
{name: "without-rotation", rotation: false},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := NewFilesystemLogWriter(fileName, log.NewNopLogger(), tc.rotation, false)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, stderrors.Is(err, fs.ErrPermission), err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 22:55:31 +00:00
|
|
|
func BenchmarkFilesystemLogger(b *testing.B) {
|
2019-07-16 22:41:50 +00:00
|
|
|
ctx := context.Background()
|
2017-04-03 21:48:50 +00:00
|
|
|
tempPath, err := ioutil.TempDir("", "test")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("temp dir failed", err)
|
|
|
|
}
|
2019-04-08 18:47:15 +00:00
|
|
|
fileName := path.Join(tempPath, "filesystemLogWriter")
|
2020-09-09 20:33:32 +00:00
|
|
|
lgr, err := NewFilesystemLogWriter(fileName, log.NewNopLogger(), false, false)
|
2017-04-03 21:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("new failed ", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(fileName)
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
var logs []json.RawMessage
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
randInput := make([]byte, 512)
|
|
|
|
rand.Read(randInput)
|
|
|
|
logs = append(logs, randInput)
|
|
|
|
}
|
2017-04-03 21:48:50 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-07-16 22:41:50 +00:00
|
|
|
err := lgr.Write(ctx, logs)
|
2017-04-03 21:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("write failed ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
lgr.writer.Close()
|
2017-04-03 21:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLumberjack(b *testing.B) {
|
2020-09-09 20:33:32 +00:00
|
|
|
benchLumberjack(b, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLumberjackWithCompression(b *testing.B) {
|
|
|
|
benchLumberjack(b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchLumberjack(b *testing.B, compression bool) {
|
2019-07-16 22:41:50 +00:00
|
|
|
ctx := context.Background()
|
2017-04-03 21:48:50 +00:00
|
|
|
tempPath, err := ioutil.TempDir("", "test")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("temp dir failed", err)
|
|
|
|
}
|
|
|
|
fileName := path.Join(tempPath, "lumberjack")
|
2020-09-09 20:33:32 +00:00
|
|
|
lgr, err := NewFilesystemLogWriter(fileName, log.NewNopLogger(), true, compression)
|
2019-04-08 18:47:15 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("new failed ", err)
|
2017-04-03 21:48:50 +00:00
|
|
|
}
|
|
|
|
defer os.Remove(fileName)
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
var logs []json.RawMessage
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
randInput := make([]byte, 512)
|
|
|
|
rand.Read(randInput)
|
|
|
|
logs = append(logs, randInput)
|
|
|
|
}
|
2017-04-03 21:48:50 +00:00
|
|
|
// first lumberjack write opens file so we count that as part of initialization
|
|
|
|
// just to make sure we're comparing apples to apples with our logger
|
2019-07-16 22:41:50 +00:00
|
|
|
err = lgr.Write(ctx, logs)
|
2017-04-03 21:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("first write failed ", err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-07-16 22:41:50 +00:00
|
|
|
err := lgr.Write(ctx, logs)
|
2017-04-03 21:48:50 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal("write failed ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
2019-04-08 18:47:15 +00:00
|
|
|
lgr.writer.Close()
|
2017-04-03 21:48:50 +00:00
|
|
|
}
|