1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Carry 34248 Added tag log option to json-logger and use RawAttrs

This fix carries PR 34248: Added tag log option to json-logger

This fix changes to use RawAttrs based on review feedback.

This fix fixes 19803, this fix closes 34248.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2018-01-08 00:54:58 +00:00
parent 5f50f4f511
commit e77267c5a6
6 changed files with 18 additions and 30 deletions

View file

@ -62,17 +62,21 @@ func New(info logger.Info) (logger.Logger, error) {
}
}
attrs, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}
// no default template. only use a tag if the user asked for it
tag, err := loggerutils.ParseLogTag(info, "")
if err != nil {
return nil, err
}
if tag != "" {
attrs["tag"] = tag
}
var extra []byte
attrs, err := info.ExtraAttributes(nil)
if err != nil {
return nil, err
}
if len(attrs) > 0 {
var err error
extra, err = json.Marshal(attrs)
@ -83,7 +87,7 @@ func New(info logger.Info) (logger.Logger, error) {
buf := bytes.NewBuffer(nil)
marshalFunc := func(msg *logger.Message) ([]byte, error) {
if err := marshalMessage(msg, extra, buf, tag); err != nil {
if err := marshalMessage(msg, extra, buf); err != nil {
return nil, err
}
b := buf.Bytes()
@ -111,7 +115,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error {
return err
}
func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer, tag string) error {
func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer) error {
logLine := msg.Line
if !msg.Partial {
logLine = append(msg.Line, '\n')
@ -121,7 +125,6 @@ func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffe
Stream: msg.Source,
Created: msg.Timestamp,
RawAttrs: extra,
Tag: tag,
}).MarshalJSONBuf(buf)
if err != nil {
return errors.Wrap(err, "error writing log message to buffer")

View file

@ -14,6 +14,7 @@ import (
"github.com/docker/docker/daemon/logger"
"github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
"github.com/gotestyourself/gotestyourself/fs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -90,14 +91,11 @@ func TestJSONFileLoggerWithTags(t *testing.T) {
res, err := ioutil.ReadFile(filename)
require.NoError(t, err)
expected := `{"log":"line1\n","stream":"src1","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"}
{"log":"line2\n","stream":"src2","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"}
{"log":"line3\n","stream":"src3","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"}
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"}
`
if string(res) != expected {
t.Fatalf("Wrong log content: %q, expected %q", res, expected)
}
assert.Equal(t, expected, string(res))
}
func BenchmarkJSONFileLoggerLog(b *testing.B) {
@ -125,7 +123,7 @@ func BenchmarkJSONFileLoggerLog(b *testing.B) {
}
buf := bytes.NewBuffer(nil)
require.NoError(b, marshalMessage(msg, nil, buf, ""))
require.NoError(b, marshalMessage(msg, nil, buf))
b.SetBytes(int64(buf.Len()))
b.ResetTimer()

View file

@ -14,8 +14,6 @@ type JSONLog struct {
Created time.Time `json:"time"`
// Attrs is the list of extra attributes provided by the user
Attrs map[string]string `json:"attrs,omitempty"`
// Tags requested the operator
Tag string `json:"tag,omitempty"`
}
// Reset all fields to their zero value.
@ -24,5 +22,4 @@ func (jl *JSONLog) Reset() {
jl.Stream = ""
jl.Created = time.Time{}
jl.Attrs = make(map[string]string)
jl.Tag = ""
}

View file

@ -12,7 +12,6 @@ type JSONLogs struct {
Log []byte `json:"log,omitempty"`
Stream string `json:"stream,omitempty"`
Created time.Time `json:"time"`
Tag string `json:"tag,omitempty"`
// json-encoded bytes
RawAttrs json.RawMessage `json:"attrs,omitempty"`
@ -38,15 +37,6 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
buf.WriteString(`"stream":`)
ffjsonWriteJSONBytesAsString(buf, []byte(mj.Stream))
}
if len(mj.Tag) > 0 {
if first {
first = false
} else {
buf.WriteString(`,`)
}
buf.WriteString(`"tag":`)
ffjsonWriteJSONBytesAsString(buf, []byte(mj.Tag))
}
if len(mj.RawAttrs) > 0 {
if first {
first = false

View file

@ -30,7 +30,7 @@ func TestJSONLogsMarshalJSONBuf(t *testing.T) {
// with raw attributes
{Log: []byte("A log line"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`,
// with Tag set
{Log: []byte("A log line with tag"), Tag: "test-tag"}: `^{\"log\":\"A log line with tag\",\"tag\":\"test-tag\",\"time\":`,
{Log: []byte("A log line with tag"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line with tag\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`,
}
for jsonLog, expression := range logs {
var buf bytes.Buffer

View file

@ -35,7 +35,7 @@ func BenchmarkJSONFileLoggerReadLogs(b *testing.B) {
}
buf := bytes.NewBuffer(nil)
require.NoError(b, marshalMessage(msg, nil, buf, ""))
require.NoError(b, marshalMessage(msg, nil, buf))
b.SetBytes(int64(buf.Len()))
b.ResetTimer()