This repository has been archived by the owner on May 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert nil pointer log field value to string "nil" (#230)
* Set nil pointer to string nil when converting key values. This causes a panic that will be recovered by the fmt package. Altought on darwin this makes any project using open tracing impossible to debug. see cortexproject/cortex#1712 Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Adds some tests for InterleavedKVToFields. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
- Loading branch information
1 parent
cdf3382
commit 2876d20
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package log | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var nilInterface io.Reader | ||
|
||
func TestInterleavedKVToFields(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
keyValues []interface{} | ||
want []Field | ||
wantErr bool | ||
}{ | ||
{ | ||
"incorrect pair", | ||
[]interface{}{"test"}, | ||
nil, | ||
true, | ||
}, | ||
{ | ||
"non string key", | ||
[]interface{}{struct{}{}, "foo"}, | ||
nil, | ||
true, | ||
}, | ||
{ | ||
"happy path", | ||
[]interface{}{ | ||
"bool", true, | ||
"string", "string", | ||
"int", int(1), | ||
"int8", int8(2), | ||
"int16", int16(3), | ||
"int64", int64(4), | ||
"uint", uint(5), | ||
"uint64", uint64(6), | ||
"uint8", uint8(7), | ||
"uint16", uint16(8), | ||
"uint32", uint32(9), | ||
"float32", float32(10), | ||
"float64", float64(11), | ||
"int32", int32(12), | ||
"stringer", errors.New("err"), | ||
"nilInterface", nilInterface, | ||
"nil", nil, | ||
}, | ||
[]Field{ | ||
Bool("bool", true), | ||
String("string", "string"), | ||
Int("int", int(1)), | ||
Int32("int8", int32(2)), | ||
Int32("int16", int32(3)), | ||
Int64("int64", int64(4)), | ||
Uint64("uint", uint64(5)), | ||
Uint64("uint64", uint64(6)), | ||
Uint32("uint8", uint32(7)), | ||
Uint32("uint16", uint32(8)), | ||
Uint32("uint32", uint32(9)), | ||
Float32("float32", float32(10)), | ||
Float64("float64", float64(11)), | ||
Int32("int32", int32(12)), | ||
String("stringer", errors.New("err").Error()), | ||
String("nilInterface", "nil"), | ||
String("nil", "nil"), | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := InterleavedKVToFields(tt.keyValues...) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("InterleavedKVToFields() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |