Skip to content

Commit

Permalink
addressed requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed May 13, 2024
1 parent 444be44 commit e3900ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 57 deletions.
33 changes: 8 additions & 25 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"
import (
"context"
"fmt"
"slices"

"go.uber.org/zap/zapcore"

Expand Down Expand Up @@ -96,7 +95,6 @@ func WithLoggerProvider(provider log.LoggerProvider) Option {
// Core is a [zapcore.Core] that sends logging records to OpenTelemetry.
type Core struct {
logger log.Logger
attr []log.KeyValue
}

// Compile-time check *Core implements zapcore.Core.
Expand All @@ -110,22 +108,16 @@ func NewCore(opts ...Option) *Core {
}
}

// TODO
// LevelEnabler decides whether a given logging level is enabled when logging a message.
func (o *Core) Enabled(level zapcore.Level) bool {
r := log.Record{}
r.SetSeverity(getOTelLevel(level))
return o.logger.Enabled(context.Background(), r)
return true
}

// TODO
// With adds structured context to the Core.
func (o *Core) With(fields []zapcore.Field) zapcore.Core {
clone := o.clone()
if len(fields) > 0 {
// TODO convert zap fields to otel attributes
fmt.Println("TODO")
}
return clone
return o
}

// TODO
Expand All @@ -134,12 +126,10 @@ func (o *Core) Sync() error {
return nil
}

// TODO
// Check determines whether the supplied Entry should be logged using core.Enabled method.
func (o *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if o.Enabled(ent.Level) {
return ce.AddCore(ent, o)
}
return ce
return ce.AddCore(ent, o)
}

// TODO
Expand All @@ -149,24 +139,17 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetTimestamp(ent.Time)
r.SetBody(log.StringValue(ent.Message))
r.SetSeverity(getOTelLevel(ent.Level))

fmt.Println(ent, fields)
// 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 containg trace context.
// TODO: Handle context.Context containing trace context.
// TODO: Handle zap.Namespace.
// TODO: Handle logger name.

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

func (o *Core) clone() *Core {
return &Core{
logger: o.logger,
attr: slices.Clone(o.attr),
}
}

// converts zap level to OTel log level.
func getOTelLevel(level zapcore.Level) log.Severity {
switch level {
Expand Down
35 changes: 3 additions & 32 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package otelzap

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,11 +20,7 @@ import (
var (
testBodyString = "log message"
testSeverity = log.SeverityInfo
entry = zapcore.Entry{
Level: zapcore.InfoLevel,
Message: testBodyString,
}
field = zap.String("key", "testValue")
testField = zap.String("key", "testValue")
)

func TestNewCoreConfiguration(t *testing.T) {
Expand Down Expand Up @@ -68,18 +63,6 @@ func TestNewCoreConfiguration(t *testing.T) {
})
}

func TestCoreEnabled(t *testing.T) {
enabledFunc := func(c context.Context, r log.Record) bool {
return r.Severity() >= log.SeverityInfo
}

r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
zc := NewCore(WithLoggerProvider(r))

assert.False(t, zc.Enabled(zap.DebugLevel), "level conversion: permissive")
assert.True(t, zc.Enabled(zap.InfoLevel), "level conversion: restrictive")
}

// Test conversion of Zap Level to OTel level.
func TestGetOTelLevel(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -110,10 +93,8 @@ func TestCore(t *testing.T) {
zc := NewCore(WithLoggerProvider(rec))

t.Run("test Write method of Core", func(t *testing.T) {
err := zc.Write(entry, []zap.Field{field})
if err != nil {
t.Errorf("Error occurred: %v", err)
}
logger := zap.New(zc)
logger.Info(testBodyString, testField)

// why is index 1 populated with results and not 0?
got := rec.Result()[1].Records[0]
Expand All @@ -124,14 +105,4 @@ func TestCore(t *testing.T) {

rec.Reset()
})

t.Run("test With method of Core", func(t *testing.T) {
childCore := zc.With([]zap.Field{field})
assert.Equal(t, childCore, zc)

// TODO test record attributes

rec.Reset()
})

}

0 comments on commit e3900ce

Please sign in to comment.