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>
... | ... |
@@ -62,17 +62,21 @@ func New(info logger.Info) (logger.Logger, error) { |
62 | 62 |
} |
63 | 63 |
} |
64 | 64 |
|
65 |
- // no default template. only use a tag if the user asked for it |
|
66 |
- tag, err := loggerutils.ParseLogTag(info, "") |
|
65 |
+ attrs, err := info.ExtraAttributes(nil) |
|
67 | 66 |
if err != nil { |
68 | 67 |
return nil, err |
69 | 68 |
} |
70 | 69 |
|
71 |
- var extra []byte |
|
72 |
- attrs, err := info.ExtraAttributes(nil) |
|
70 |
+ // no default template. only use a tag if the user asked for it |
|
71 |
+ tag, err := loggerutils.ParseLogTag(info, "") |
|
73 | 72 |
if err != nil { |
74 | 73 |
return nil, err |
75 | 74 |
} |
75 |
+ if tag != "" { |
|
76 |
+ attrs["tag"] = tag |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ var extra []byte |
|
76 | 80 |
if len(attrs) > 0 { |
77 | 81 |
var err error |
78 | 82 |
extra, err = json.Marshal(attrs) |
... | ... |
@@ -83,7 +87,7 @@ func New(info logger.Info) (logger.Logger, error) { |
83 | 83 |
|
84 | 84 |
buf := bytes.NewBuffer(nil) |
85 | 85 |
marshalFunc := func(msg *logger.Message) ([]byte, error) { |
86 |
- if err := marshalMessage(msg, extra, buf, tag); err != nil { |
|
86 |
+ if err := marshalMessage(msg, extra, buf); err != nil { |
|
87 | 87 |
return nil, err |
88 | 88 |
} |
89 | 89 |
b := buf.Bytes() |
... | ... |
@@ -111,7 +115,7 @@ func (l *JSONFileLogger) Log(msg *logger.Message) error { |
111 | 111 |
return err |
112 | 112 |
} |
113 | 113 |
|
114 |
-func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer, tag string) error { |
|
114 |
+func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffer) error { |
|
115 | 115 |
logLine := msg.Line |
116 | 116 |
if !msg.Partial { |
117 | 117 |
logLine = append(msg.Line, '\n') |
... | ... |
@@ -121,7 +125,6 @@ func marshalMessage(msg *logger.Message, extra json.RawMessage, buf *bytes.Buffe |
121 | 121 |
Stream: msg.Source, |
122 | 122 |
Created: msg.Timestamp, |
123 | 123 |
RawAttrs: extra, |
124 |
- Tag: tag, |
|
125 | 124 |
}).MarshalJSONBuf(buf) |
126 | 125 |
if err != nil { |
127 | 126 |
return errors.Wrap(err, "error writing log message to buffer") |
... | ... |
@@ -14,6 +14,7 @@ import ( |
14 | 14 |
"github.com/docker/docker/daemon/logger" |
15 | 15 |
"github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog" |
16 | 16 |
"github.com/gotestyourself/gotestyourself/fs" |
17 |
+ "github.com/stretchr/testify/assert" |
|
17 | 18 |
"github.com/stretchr/testify/require" |
18 | 19 |
) |
19 | 20 |
|
... | ... |
@@ -90,14 +91,11 @@ func TestJSONFileLoggerWithTags(t *testing.T) { |
90 | 90 |
res, err := ioutil.ReadFile(filename) |
91 | 91 |
require.NoError(t, err) |
92 | 92 |
|
93 |
- expected := `{"log":"line1\n","stream":"src1","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"} |
|
94 |
-{"log":"line2\n","stream":"src2","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"} |
|
95 |
-{"log":"line3\n","stream":"src3","tag":"a7317399f3f8/test-container","time":"0001-01-01T00:00:00Z"} |
|
93 |
+ expected := `{"log":"line1\n","stream":"src1","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"} |
|
94 |
+{"log":"line2\n","stream":"src2","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"} |
|
95 |
+{"log":"line3\n","stream":"src3","attrs":{"tag":"a7317399f3f8/test-container"},"time":"0001-01-01T00:00:00Z"} |
|
96 | 96 |
` |
97 |
- |
|
98 |
- if string(res) != expected { |
|
99 |
- t.Fatalf("Wrong log content: %q, expected %q", res, expected) |
|
100 |
- } |
|
97 |
+ assert.Equal(t, expected, string(res)) |
|
101 | 98 |
} |
102 | 99 |
|
103 | 100 |
func BenchmarkJSONFileLoggerLog(b *testing.B) { |
... | ... |
@@ -125,7 +123,7 @@ func BenchmarkJSONFileLoggerLog(b *testing.B) { |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
buf := bytes.NewBuffer(nil) |
128 |
- require.NoError(b, marshalMessage(msg, nil, buf, "")) |
|
128 |
+ require.NoError(b, marshalMessage(msg, nil, buf)) |
|
129 | 129 |
b.SetBytes(int64(buf.Len())) |
130 | 130 |
|
131 | 131 |
b.ResetTimer() |
... | ... |
@@ -14,8 +14,6 @@ type JSONLog struct { |
14 | 14 |
Created time.Time `json:"time"` |
15 | 15 |
// Attrs is the list of extra attributes provided by the user |
16 | 16 |
Attrs map[string]string `json:"attrs,omitempty"` |
17 |
- // Tags requested the operator |
|
18 |
- Tag string `json:"tag,omitempty"` |
|
19 | 17 |
} |
20 | 18 |
|
21 | 19 |
// Reset all fields to their zero value. |
... | ... |
@@ -24,5 +22,4 @@ func (jl *JSONLog) Reset() { |
24 | 24 |
jl.Stream = "" |
25 | 25 |
jl.Created = time.Time{} |
26 | 26 |
jl.Attrs = make(map[string]string) |
27 |
- jl.Tag = "" |
|
28 | 27 |
} |
... | ... |
@@ -12,7 +12,6 @@ type JSONLogs struct { |
12 | 12 |
Log []byte `json:"log,omitempty"` |
13 | 13 |
Stream string `json:"stream,omitempty"` |
14 | 14 |
Created time.Time `json:"time"` |
15 |
- Tag string `json:"tag,omitempty"` |
|
16 | 15 |
|
17 | 16 |
// json-encoded bytes |
18 | 17 |
RawAttrs json.RawMessage `json:"attrs,omitempty"` |
... | ... |
@@ -38,15 +37,6 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error { |
38 | 38 |
buf.WriteString(`"stream":`) |
39 | 39 |
ffjsonWriteJSONBytesAsString(buf, []byte(mj.Stream)) |
40 | 40 |
} |
41 |
- if len(mj.Tag) > 0 { |
|
42 |
- if first { |
|
43 |
- first = false |
|
44 |
- } else { |
|
45 |
- buf.WriteString(`,`) |
|
46 |
- } |
|
47 |
- buf.WriteString(`"tag":`) |
|
48 |
- ffjsonWriteJSONBytesAsString(buf, []byte(mj.Tag)) |
|
49 |
- } |
|
50 | 41 |
if len(mj.RawAttrs) > 0 { |
51 | 42 |
if first { |
52 | 43 |
first = false |
... | ... |
@@ -30,7 +30,7 @@ func TestJSONLogsMarshalJSONBuf(t *testing.T) { |
30 | 30 |
// with raw attributes |
31 | 31 |
{Log: []byte("A log line"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":`, |
32 | 32 |
// with Tag set |
33 |
- {Log: []byte("A log line with tag"), Tag: "test-tag"}: `^{\"log\":\"A log line with tag\",\"tag\":\"test-tag\",\"time\":`, |
|
33 |
+ {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\":`, |
|
34 | 34 |
} |
35 | 35 |
for jsonLog, expression := range logs { |
36 | 36 |
var buf bytes.Buffer |
... | ... |
@@ -35,7 +35,7 @@ func BenchmarkJSONFileLoggerReadLogs(b *testing.B) { |
35 | 35 |
} |
36 | 36 |
|
37 | 37 |
buf := bytes.NewBuffer(nil) |
38 |
- require.NoError(b, marshalMessage(msg, nil, buf, "")) |
|
38 |
+ require.NoError(b, marshalMessage(msg, nil, buf)) |
|
39 | 39 |
b.SetBytes(int64(buf.Len())) |
40 | 40 |
|
41 | 41 |
b.ResetTimer() |