Skip to content

Commit

Permalink
fix(otelzap): do not panic on invalid input (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
igrayson committed Aug 9, 2022
1 parent 7fcbf03 commit a41206e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion otelzap/otelzap.go
Expand Up @@ -528,7 +528,7 @@ func (s *SugaredLogger) logKVs(

attrs := make([]attribute.KeyValue, 0, numAttr+len(kvs))

for i := 0; i < len(kvs); i += 2 {
for i := 0; i < len(kvs)-1; i += 2 {
if key, ok := kvs[i].(string); ok {
attrs = append(attrs, otelutil.Attribute(key, kvs[i+1]))
}
Expand Down
30 changes: 30 additions & 0 deletions otelzap/otelzap_test.go
Expand Up @@ -265,6 +265,36 @@ func TestOtelZap(t *testing.T) {
requireCodeAttrs(t, m)
},
},
{
log: func(ctx context.Context, log *Logger) {
log.Sugar().InfowContext(ctx, "hello", "foo", "bar")
},
require: func(t *testing.T, event sdktrace.Event) {
m := attrMap(event.Attributes)

sev, ok := m[logSeverityKey]
require.True(t, ok)
require.Equal(t, "INFO", sev.AsString())

msg, ok := m[logMessageKey]
require.True(t, ok)
require.Equal(t, "hello", msg.AsString())

foo, ok := m["foo"]
require.True(t, ok)
require.NotZero(t, foo.AsString())

requireCodeAttrs(t, m)
},
},
{
log: func(ctx context.Context, log *Logger) {
log.Sugar().InfowContext(ctx, "sugary logs require keyAndValues to come in pairs", "so this is invalid, but it shouldn't panic")
},
require: func(t *testing.T, event sdktrace.Event) {
// no panic? success!
},
},
{
log: func(ctx context.Context, log *Logger) {
log.Sugar().Ctx(ctx).Errorf("hello %s", "world")
Expand Down

0 comments on commit a41206e

Please sign in to comment.