Skip to content

Commit

Permalink
fix fluent-bit output plugin generating invalid JSON (#3240)
Browse files Browse the repository at this point in the history
* fix fluent-bit output plugin generating invalid JSON

* fix golint error
  • Loading branch information
sbaier1 authored Feb 11, 2021
1 parent f4dfce4 commit 9a58f83
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/fluent-bit/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func createLine(records map[string]interface{}, f format) (string, error) {
if err != nil {
return "", err
}
return lineReplacer.Replace(string(js)), nil
return string(js), nil
case kvPairFormat:
buf := &bytes.Buffer{}
enc := logfmt.NewEncoder(buf)
Expand Down
10 changes: 10 additions & 0 deletions cmd/fluent-bit/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func Test_loki_sendRecord(t *testing.T) {
},
},
}
var nestedJSONFixture = map[interface{}]interface{}{
"kubernetes": map[interface{}]interface{}{
"annotations": map[interface{}]interface{}{
"kubernetes.io/psp": "test",
"prometheus.io/port": "8085",
},
},
"log": "\tstatus code: 403, request id: b41c1ffa-c586-4359-a7da-457dd8da4bad\n",
}

tests := []struct {
name string
Expand All @@ -71,6 +80,7 @@ func Test_loki_sendRecord(t *testing.T) {
{"labelmap", &config{labelMap: map[string]interface{}{"bar": "other"}, lineFormat: jsonFormat, removeKeys: []string{"bar", "error"}}, simpleRecordFixture, []api.Entry{{Labels: model.LabelSet{"other": "500"}, Entry: logproto.Entry{Line: `{"foo":"bar"}`, Timestamp: now}}}, false},
{"byte array", &config{labelKeys: []string{"label"}, lineFormat: jsonFormat}, byteArrayRecordFixture, []api.Entry{{Labels: model.LabelSet{"label": "label"}, Entry: logproto.Entry{Line: `{"map":{"inner":"bar"},"outer":"foo"}`, Timestamp: now}}}, false},
{"mixed types", &config{labelKeys: []string{"label"}, lineFormat: jsonFormat}, mixedTypesRecordFixture, []api.Entry{{Labels: model.LabelSet{"label": "label"}, Entry: logproto.Entry{Line: `{"array":[42,42.42,"foo"],"float":42.42,"int":42,"map":{"nested":{"foo":"bar","invalid":"a\ufffdz"}}}`, Timestamp: now}}}, false},
{"JSON inner string escaping", &config{removeKeys: []string{"kubernetes"}, labelMap: map[string]interface{}{"kubernetes": map[string]interface{}{"annotations": map[string]interface{}{"kubernetes.io/psp": "label"}}}, lineFormat: jsonFormat}, nestedJSONFixture, []api.Entry{{Labels: model.LabelSet{"label": "test"}, Entry: logproto.Entry{Line: `{"log":"\tstatus code: 403, request id: b41c1ffa-c586-4359-a7da-457dd8da4bad\n"}`, Timestamp: now}}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 9a58f83

Please sign in to comment.