2018-02-05 21:05:59 +00:00
|
|
|
package jsonfilelog // import "github.com/docker/docker/daemon/logger/jsonfilelog"
|
2015-02-04 19:04:58 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-25 19:52:42 +00:00
|
|
|
"bytes"
|
2018-03-13 02:47:24 +00:00
|
|
|
"compress/gzip"
|
2015-10-04 21:07:09 +00:00
|
|
|
"encoding/json"
|
2018-04-05 16:39:28 +00:00
|
|
|
"fmt"
|
2015-02-04 19:04:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-10-04 21:07:09 +00:00
|
|
|
"reflect"
|
2015-07-01 00:40:13 +00:00
|
|
|
"strconv"
|
2015-02-04 19:04:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
2017-09-25 19:57:45 +00:00
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/fs"
|
2015-02-04 19:04:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJSONFileLogger(t *testing.T) {
|
2015-04-09 04:23:30 +00:00
|
|
|
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
2015-02-04 19:04:58 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "docker-logger-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
filename := filepath.Join(tmp, "container.log")
|
2016-11-26 05:08:34 +00:00
|
|
|
l, err := New(logger.Info{
|
2015-04-09 04:23:30 +00:00
|
|
|
ContainerID: cid,
|
|
|
|
LogPath: filename,
|
|
|
|
})
|
2015-02-04 19:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
2015-04-09 04:23:30 +00:00
|
|
|
|
2016-05-31 19:46:55 +00:00
|
|
|
if err := l.Log(&logger.Message{Line: []byte("line1"), Source: "src1"}); err != nil {
|
2015-02-04 19:04:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-31 19:46:55 +00:00
|
|
|
if err := l.Log(&logger.Message{Line: []byte("line2"), Source: "src2"}); err != nil {
|
2015-02-04 19:04:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-31 19:46:55 +00:00
|
|
|
if err := l.Log(&logger.Message{Line: []byte("line3"), Source: "src3"}); err != nil {
|
2015-02-04 19:04:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected := `{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line2\n","stream":"src2","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line3\n","stream":"src3","time":"0001-01-01T00:00:00Z"}
|
|
|
|
`
|
|
|
|
|
|
|
|
if string(res) != expected {
|
|
|
|
t.Fatalf("Wrong log content: %q, expected %q", res, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:22:10 +00:00
|
|
|
func TestJSONFileLoggerWithTags(t *testing.T) {
|
|
|
|
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
|
|
|
cname := "test-container"
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-logger-")
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
filename := filepath.Join(tmp, "container.log")
|
|
|
|
l, err := New(logger.Info{
|
|
|
|
Config: map[string]string{
|
|
|
|
"tag": "{{.ID}}/{{.Name}}", // first 12 characters of ContainerID and full ContainerName
|
|
|
|
},
|
|
|
|
ContainerID: cid,
|
|
|
|
ContainerName: cname,
|
|
|
|
LogPath: filename,
|
|
|
|
})
|
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
err = l.Log(&logger.Message{Line: []byte("line1"), Source: "src1"})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
|
|
|
|
err = l.Log(&logger.Message{Line: []byte("line2"), Source: "src2"})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
|
|
|
|
err = l.Log(&logger.Message{Line: []byte("line3"), Source: "src3"})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
|
|
|
|
res, err := ioutil.ReadFile(filename)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 14:22:10 +00:00
|
|
|
|
2018-01-08 00:54:58 +00:00
|
|
|
expected := `{"log":"line1\n","stream":"src1","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line2\n","stream":"src2","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line3\n","stream":"src3","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"}
|
2017-09-26 14:22:10 +00:00
|
|
|
`
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(expected, string(res)))
|
2017-09-26 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 19:52:42 +00:00
|
|
|
func BenchmarkJSONFileLoggerLog(b *testing.B) {
|
|
|
|
tmp := fs.NewDir(b, "bench-jsonfilelog")
|
|
|
|
defer tmp.Remove()
|
|
|
|
|
|
|
|
jsonlogger, err := New(logger.Info{
|
|
|
|
ContainerID: "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657",
|
|
|
|
LogPath: tmp.Join("container.log"),
|
|
|
|
Config: map[string]string{
|
2018-04-05 16:39:28 +00:00
|
|
|
"labels": "first,second",
|
|
|
|
"max-file": "10",
|
|
|
|
"compress": "true",
|
|
|
|
"max-size": "20m",
|
2017-09-25 19:52:42 +00:00
|
|
|
},
|
|
|
|
ContainerLabels: map[string]string{
|
|
|
|
"first": "label_value",
|
|
|
|
"second": "label_foo",
|
|
|
|
},
|
2015-04-09 04:23:30 +00:00
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(b, err)
|
2017-09-25 19:52:42 +00:00
|
|
|
defer jsonlogger.Close()
|
2015-04-09 04:23:30 +00:00
|
|
|
|
2018-04-05 16:39:28 +00:00
|
|
|
t := time.Now().UTC()
|
|
|
|
for _, data := range [][]byte{
|
|
|
|
[]byte(""),
|
|
|
|
[]byte("a short string"),
|
|
|
|
bytes.Repeat([]byte("a long string"), 100),
|
|
|
|
bytes.Repeat([]byte("a really long string"), 10000),
|
|
|
|
} {
|
|
|
|
b.Run(fmt.Sprintf("%d", len(data)), func(b *testing.B) {
|
|
|
|
testMsg := &logger.Message{
|
|
|
|
Line: data,
|
|
|
|
Source: "stderr",
|
|
|
|
Timestamp: t,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
assert.NilError(b, marshalMessage(testMsg, nil, buf))
|
|
|
|
b.SetBytes(int64(buf.Len()))
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
msg := logger.NewMessage()
|
|
|
|
msg.Line = testMsg.Line
|
|
|
|
msg.Timestamp = testMsg.Timestamp
|
|
|
|
msg.Source = testMsg.Source
|
|
|
|
if err := jsonlogger.Log(msg); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-02-04 19:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-01 00:40:13 +00:00
|
|
|
|
|
|
|
func TestJSONFileLoggerWithOpts(t *testing.T) {
|
|
|
|
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-logger-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
filename := filepath.Join(tmp, "container.log")
|
2018-03-13 02:47:24 +00:00
|
|
|
config := map[string]string{"max-file": "3", "max-size": "1k", "compress": "true"}
|
2016-11-26 05:08:34 +00:00
|
|
|
l, err := New(logger.Info{
|
2015-07-01 00:40:13 +00:00
|
|
|
ContainerID: cid,
|
|
|
|
LogPath: filename,
|
|
|
|
Config: config,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
2018-03-13 02:47:24 +00:00
|
|
|
for i := 0; i < 36; i++ {
|
2016-05-31 19:46:55 +00:00
|
|
|
if err := l.Log(&logger.Message{Line: []byte("line" + strconv.Itoa(i)), Source: "src1"}); err != nil {
|
2015-07-01 00:40:13 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 02:47:24 +00:00
|
|
|
|
2015-07-01 00:40:13 +00:00
|
|
|
res, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-13 02:47:24 +00:00
|
|
|
|
2015-07-01 00:40:13 +00:00
|
|
|
penUlt, err := ioutil.ReadFile(filename + ".1")
|
2018-03-13 02:47:24 +00:00
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(filename + ".1.gz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-06 03:05:40 +00:00
|
|
|
defer file.Close()
|
2018-03-13 02:47:24 +00:00
|
|
|
zipReader, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-06 03:05:40 +00:00
|
|
|
defer zipReader.Close()
|
2018-03-13 02:47:24 +00:00
|
|
|
penUlt, err = ioutil.ReadAll(zipReader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(filename + ".2.gz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-06 03:05:40 +00:00
|
|
|
defer file.Close()
|
2018-03-13 02:47:24 +00:00
|
|
|
zipReader, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-06 03:05:40 +00:00
|
|
|
defer zipReader.Close()
|
2018-03-13 02:47:24 +00:00
|
|
|
antepenult, err := ioutil.ReadAll(zipReader)
|
2015-07-01 00:40:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 02:47:24 +00:00
|
|
|
expectedAntepenultimate := `{"log":"line0\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
2015-07-01 00:40:13 +00:00
|
|
|
{"log":"line1\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line2\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line3\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line4\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line5\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line6\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line7\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line8\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line9\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line10\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line11\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line12\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line13\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line14\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line15\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
`
|
2018-03-13 02:47:24 +00:00
|
|
|
expectedPenultimate := `{"log":"line16\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
2015-07-01 00:40:13 +00:00
|
|
|
{"log":"line17\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line18\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line19\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
2018-03-13 02:47:24 +00:00
|
|
|
{"log":"line20\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line21\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line22\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line23\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line24\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line25\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line26\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line27\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line28\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line29\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line30\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line31\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
`
|
|
|
|
expected := `{"log":"line32\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line33\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line34\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
|
|
|
{"log":"line35\n","stream":"src1","time":"0001-01-01T00:00:00Z"}
|
2015-07-01 00:40:13 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
if string(res) != expected {
|
|
|
|
t.Fatalf("Wrong log content: %q, expected %q", res, expected)
|
|
|
|
}
|
|
|
|
if string(penUlt) != expectedPenultimate {
|
|
|
|
t.Fatalf("Wrong log content: %q, expected %q", penUlt, expectedPenultimate)
|
|
|
|
}
|
2018-03-13 02:47:24 +00:00
|
|
|
if string(antepenult) != expectedAntepenultimate {
|
|
|
|
t.Fatalf("Wrong log content: %q, expected %q", antepenult, expectedAntepenultimate)
|
|
|
|
}
|
2015-07-01 00:40:13 +00:00
|
|
|
}
|
2015-10-04 21:07:09 +00:00
|
|
|
|
|
|
|
func TestJSONFileLoggerWithLabelsEnv(t *testing.T) {
|
|
|
|
cid := "a7317399f3f857173c6179d44823594f8294678dea9999662e5c625b5a1c7657"
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-logger-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
filename := filepath.Join(tmp, "container.log")
|
2019-01-16 21:52:22 +00:00
|
|
|
config := map[string]string{"labels": "rack,dc", "labels-regex": "^loc", "env": "environ,debug,ssl", "env-regex": "^dc"}
|
2016-11-26 05:08:34 +00:00
|
|
|
l, err := New(logger.Info{
|
2015-10-04 21:07:09 +00:00
|
|
|
ContainerID: cid,
|
|
|
|
LogPath: filename,
|
|
|
|
Config: config,
|
2019-01-16 21:52:22 +00:00
|
|
|
ContainerLabels: map[string]string{"rack": "101", "dc": "lhr", "location": "here"},
|
2016-11-09 00:34:47 +00:00
|
|
|
ContainerEnv: []string{"environ=production", "debug=false", "port=10001", "ssl=true", "dc_region=west"},
|
2015-10-04 21:07:09 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
2016-05-31 19:46:55 +00:00
|
|
|
if err := l.Log(&logger.Message{Line: []byte("line"), Source: "src1"}); err != nil {
|
2015-10-04 21:07:09 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonLog jsonlog.JSONLogs
|
|
|
|
if err := json.Unmarshal(res, &jsonLog); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
extra := make(map[string]string)
|
|
|
|
if err := json.Unmarshal(jsonLog.RawAttrs, &extra); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected := map[string]string{
|
2016-11-09 00:34:47 +00:00
|
|
|
"rack": "101",
|
|
|
|
"dc": "lhr",
|
2019-01-16 21:52:22 +00:00
|
|
|
"location": "here",
|
2016-11-09 00:34:47 +00:00
|
|
|
"environ": "production",
|
|
|
|
"debug": "false",
|
|
|
|
"ssl": "true",
|
|
|
|
"dc_region": "west",
|
2015-10-04 21:07:09 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(extra, expected) {
|
|
|
|
t.Fatalf("Wrong log attrs: %q, expected %q", extra, expected)
|
|
|
|
}
|
|
|
|
}
|