Skip to content

Commit

Permalink
Merge branch 'main' into dep-otelmacaron
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias authored May 23, 2024
2 parents ca3c3f4 + f18c842 commit 21daef0
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 21 deletions.
16 changes: 15 additions & 1 deletion bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,32 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetBody(log.StringValue(ent.Message))
r.SetSeverity(convertLevel(ent.Level))

// TODO: Handle attributes passed via fields (exceptions: context.Context and zap.Namespace).
// TODO: Handle attributes passed via With (exceptions: context.Context and zap.Namespace).
// TODO: Handle context.Context containing trace context.
// TODO: Handle zap.Namespace.
// TODO: Handle zap.Object.
// TODO: Handle zap.Array.
// TODO: Handle ent.LoggerName.

if len(fields) > 0 {
attrbuf := convertField(fields)
r.AddAttributes(attrbuf...)
}

o.logger.Emit(context.Background(), r)
return nil
}

func convertField(fields []zapcore.Field) []log.KeyValue {
// TODO: Use objectEncoder from a pool instead of newObjectEncoder.
enc := newObjectEncoder(len(fields))
for _, field := range fields {
field.AddTo(enc)
}

return enc.kv
}

func convertLevel(level zapcore.Level) log.Severity {
switch level {
case zapcore.DebugLevel:
Expand Down
11 changes: 9 additions & 2 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@ import (
var (
testMessage = "log message"
loggerName = "name"
testKey = "key"
testValue = "value"
)

func TestCore(t *testing.T) {
rec := logtest.NewRecorder()
zc := NewCore(loggerName, WithLoggerProvider(rec))
logger := zap.New(zc)

logger.Info(testMessage)
logger.Info(testMessage, zap.String(testKey, testValue))

// TODO (#5580): Not sure why index 1 is populated with results and not 0.
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, 1, got.AttributesLen())
got.WalkAttributes(func(kv log.KeyValue) bool {
assert.Equal(t, testKey, string(kv.Key))
assert.Equal(t, testValue, value2Result(kv.Value))
return true
})
}

func TestCoreEnabled(t *testing.T) {
Expand Down
42 changes: 33 additions & 9 deletions bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ func newObjectEncoder(len int) *objectEncoder {
}

func (m *objectEncoder) AddArray(key string, v zapcore.ArrayMarshaler) error {
// TODO
return nil
// TODO: Use arrayEncoder from a pool.
arr := &arrayEncoder{}
err := v.MarshalLogArray(arr)
m.kv = append(m.kv, log.Slice(key, arr.elems...))
return err
}

func (m *objectEncoder) AddObject(k string, v zapcore.ObjectMarshaler) error {
Expand Down Expand Up @@ -170,16 +173,37 @@ func (a *arrayEncoder) AppendReflected(v interface{}) error {
return nil
}

func (a *arrayEncoder) AppendByteString(v []byte) {
a.elems = append(a.elems, log.StringValue(string(v)))
}

func (a *arrayEncoder) AppendBool(v bool) {
a.elems = append(a.elems, log.BoolValue(v))
}

func (a *arrayEncoder) AppendFloat64(v float64) {
a.elems = append(a.elems, log.Float64Value(v))
}

func (a *arrayEncoder) AppendFloat32(v float32) {
a.AppendFloat64(float64(v))
}

func (a *arrayEncoder) AppendInt(v int) {
a.elems = append(a.elems, log.IntValue(v))
}

func (a *arrayEncoder) AppendInt64(v int64) {
a.elems = append(a.elems, log.Int64Value(v))
}

func (a *arrayEncoder) AppendString(v string) {
a.elems = append(a.elems, log.StringValue(v))
}

// TODO.
func (a *arrayEncoder) AppendComplex128(v complex128) {}
func (a *arrayEncoder) AppendFloat32(v float32) {}
func (a *arrayEncoder) AppendByteString(v []byte) {}
func (a *arrayEncoder) AppendBool(v bool) {}
func (a *arrayEncoder) AppendUint64(v uint64) {}
func (a *arrayEncoder) AppendFloat64(v float64) {}
func (a *arrayEncoder) AppendInt(v int) {}
func (a *arrayEncoder) AppendInt64(v int64) {}
func (a *arrayEncoder) AppendString(v string) {}
func (a *arrayEncoder) AppendComplex64(v complex64) {}
func (a *arrayEncoder) AppendDuration(v time.Duration) {}
func (a *arrayEncoder) AppendInt32(v int32) {}
Expand Down
42 changes: 42 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ func TestObjectEncoder(t *testing.T) {
f func(zapcore.ObjectEncoder)
expected interface{}
}{
{
desc: "AddArray",
f: func(e zapcore.ObjectEncoder) {
assert.NoError(t, e.AddArray("k", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error {
arr.AppendBool(true)
arr.AppendBool(false)
arr.AppendBool(true)
return nil
})), "Expected AddArray to succeed.")
},
expected: []interface{}{true, false, true},
},
{
desc: "AddBinary",
f: func(e zapcore.ObjectEncoder) { e.AddBinary("k", []byte("foo")) },
Expand Down Expand Up @@ -145,6 +157,36 @@ func TestObjectEncoder(t *testing.T) {
}
}

// Copied from https:/uber-go/zap/blob/b39f8b6b6a44d8371a87610be50cce58eeeaabcb/zapcore/memory_encoder_test.go.
func TestArrayEncoder(t *testing.T) {
tests := []struct {
desc string
f func(zapcore.ArrayEncoder)
expected interface{}
}{
{"AppendBool", func(e zapcore.ArrayEncoder) { e.AppendBool(true) }, true},
{"AppendByteString", func(e zapcore.ArrayEncoder) { e.AppendByteString([]byte("foo")) }, "foo"},
{"AppendFloat64", func(e zapcore.ArrayEncoder) { e.AppendFloat64(3.14) }, 3.14},
{"AppendFloat32", func(e zapcore.ArrayEncoder) { e.AppendFloat32(3.14) }, float64(float32(3.14))},
{"AppendInt64", func(e zapcore.ArrayEncoder) { e.AppendInt64(42) }, int64(42)},
{"AppendInt", func(e zapcore.ArrayEncoder) { e.AppendInt(42) }, int64(42)},
{"AppendString", func(e zapcore.ArrayEncoder) { e.AppendString("foo") }, "foo"},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
enc := newObjectEncoder(1)
assert.NoError(t, enc.AddArray("k", zapcore.ArrayMarshalerFunc(func(arr zapcore.ArrayEncoder) error {
tt.f(arr)
tt.f(arr)
return nil
})), "Expected AddArray to succeed.")

assert.Equal(t, []interface{}{tt.expected, tt.expected}, value2Result(enc.kv[0].Value), "Unexpected encoder output.")
})
}
}

func value2Result(v log.Value) any {
switch v.Kind() {
case log.KindBool:
Expand Down
2 changes: 1 addition & 1 deletion detectors/aws/ec2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module go.opentelemetry.io/contrib/detectors/aws/ec2
go 1.21

require (
github.com/aws/aws-sdk-go v1.53.7
github.com/aws/aws-sdk-go v1.53.8
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
Expand Down
4 changes: 2 additions & 2 deletions detectors/aws/ec2/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/aws-sdk-go v1.53.7 h1:ZSsRYHLRxsbO2rJR2oPMz0SUkJLnBkN+1meT95B6Ixs=
github.com/aws/aws-sdk-go v1.53.7/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.53.8 h1:eoqGb1WOHIrCFKo1d51cMcnt1ralfLFaEqRkC5Zzv8k=
github.com/aws/aws-sdk-go v1.53.8/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 2 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ require (
go.opentelemetry.io/build-tools/gotmpl v0.13.0
go.opentelemetry.io/build-tools/multimod v0.13.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/tools v0.21.0
golang.org/x/vuln v1.1.0
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7
golang.org/x/vuln v1.1.1
)

require (
Expand Down
8 changes: 4 additions & 4 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,10 @@ golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.21.0 h1:qc0xYgIbsSDt9EyWz05J5wfa7LOVW0YTLOXrqdLAWIw=
golang.org/x/tools v0.21.0/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/vuln v1.1.0 h1:ECEdI+aEtjpF90eqEcDL5Q11DWSZAw5PJQWlp0+gWqc=
golang.org/x/vuln v1.1.0/go.mod h1:HT/Ar8fE34tbxWG2s7PYjVl+iIE4Er36/940Z+K540Y=
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7 h1:DnP3aRQn/r68glNGB8/7+3iE77jA+YZZCxpfIXx2MdA=
golang.org/x/tools v0.21.1-0.20240514024235-59d9797072e7/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/vuln v1.1.1 h1:4nYQg4OSr7uYQMtjuuYqLAEVuTjY4k/CPMYqvv5OPcI=
golang.org/x/vuln v1.1.1/go.mod h1:hNgE+SKMSp2wHVUpW0Ow2ejgKpNJePdML+4YjxrVxik=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down

0 comments on commit 21daef0

Please sign in to comment.