Skip to content

Commit

Permalink
JSON encoding: avoid base64 encoding of []byte inside other slices (#…
Browse files Browse the repository at this point in the history
…1890)

Closes #1889

Signed-off-by: Mikhail Gusarov <[email protected]>
(cherry picked from commit 3786d74)
  • Loading branch information
dottedmag authored and slim-bean committed Apr 6, 2020
1 parent 8e80160 commit 77da15c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/fluent-bit/loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ func (l *loki) sendRecord(r map[interface{}]interface{}, ts time.Time) error {
return l.client.Handle(lbs, ts, line)
}

// prevent base64-encoding []byte values (default json.Encoder rule) by
// converting them to strings

func toStringSlice(slice []interface{}) []interface{} {
var s []interface{}
for _, v := range slice {
switch t := v.(type) {
case []byte:
s = append(s, string(t))
case map[interface{}]interface{}:
s = append(s, toStringMap(t))
case []interface{}:
s = append(s, toStringSlice(t))
default:
s = append(s, t)
}
}
return s
}

func toStringMap(record map[interface{}]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for k, v := range record {
Expand All @@ -76,10 +96,11 @@ func toStringMap(record map[interface{}]interface{}) map[string]interface{} {
}
switch t := v.(type) {
case []byte:
// prevent encoding to base64
m[key] = string(t)
case map[interface{}]interface{}:
m[key] = toStringMap(t)
case []interface{}:
m[key] = toStringSlice(t)
default:
m[key] = v
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/fluent-bit/loki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func Test_toStringMap(t *testing.T) {
}{
{"already string", map[interface{}]interface{}{"string": "foo", "bar": []byte("buzz")}, map[string]interface{}{"string": "foo", "bar": "buzz"}},
{"skip non string", map[interface{}]interface{}{"string": "foo", 1.0: []byte("buzz")}, map[string]interface{}{"string": "foo"}},
{"byteslice in array", map[interface{}]interface{}{"string": "foo", "bar": []interface{}{map[interface{}]interface{}{"baz": []byte("quux")}}},
map[string]interface{}{"string": "foo", "bar": []interface{}{map[string]interface{}{"baz": "quux"}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 77da15c

Please sign in to comment.