From 6e38ddd40880ddef0689dc739ea523aa29ea5a72 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Thu, 9 May 2024 09:49:26 +0530 Subject: [PATCH 01/19] Implement functionality of Core --- bridges/otelzap/core.go | 66 +++++++++++++++++++++++++++++++--- bridges/otelzap/core_test.go | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 5 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 1e934666c58..c48a547e546 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -6,6 +6,10 @@ package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap" import ( + "context" + "fmt" + "slices" + "go.uber.org/zap/zapcore" "go.opentelemetry.io/otel/log" @@ -92,6 +96,8 @@ func WithLoggerProvider(provider log.LoggerProvider) Option { // Core is a [zapcore.Core] that sends logging records to OpenTelemetry. type Core struct { logger log.Logger + ctx context.Context + attr []log.KeyValue } // Compile-time check *Core implements zapcore.Core. @@ -105,16 +111,23 @@ 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 { - return true + r := log.Record{} + r.SetSeverity(getOTelLevel(level)) + // should Enabled take the context set on Core? + return o.logger.Enabled(context.Background(), r) } // TODO // With adds structured context to the Core. func (o *Core) With(fields []zapcore.Field) zapcore.Core { - return o + clone := o.clone() + if len(fields) > 0 { + // TODO convert zap fields to otel attributes + fmt.Println("TODO") + } + return clone } // TODO @@ -123,14 +136,57 @@ 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 } -// TODO // Write method encodes zap fields to OTel logs and emits them. func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { + r := log.Record{} + r.SetTimestamp(ent.Time) + r.SetBody(log.StringValue(ent.Message)) + r.SetSeverity(getOTelLevel(ent.Level)) + + if len(fields) > 0 { + // TODO map zap field to otel attributes + fmt.Println("TODO") + } else { + r.AddAttributes(o.attr...) + } + + 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 { + case zapcore.DebugLevel: + return log.SeverityDebug + case zapcore.InfoLevel: + return log.SeverityInfo + case zapcore.WarnLevel: + return log.SeverityWarn + case zapcore.ErrorLevel: + return log.SeverityError + case zapcore.DPanicLevel: + return log.SeverityFatal1 + case zapcore.PanicLevel: + return log.SeverityFatal2 + case zapcore.FatalLevel: + return log.SeverityFatal3 + default: + return log.SeverityUndefined + } +} diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 9d41da03d20..94a71d062c9 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -4,16 +4,30 @@ package otelzap import ( + "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/global" "go.opentelemetry.io/otel/log/logtest" "go.opentelemetry.io/otel/sdk/instrumentation" ) +var ( + testBodyString = "log message" + testSeverity = log.SeverityInfo + entry = zapcore.Entry{ + Level: zapcore.InfoLevel, + Message: testBodyString, + } + field = zap.String("key", "testValue") +) + func TestNewCoreConfiguration(t *testing.T) { t.Run("Default", func(t *testing.T) { r := logtest.NewRecorder() @@ -53,3 +67,58 @@ func TestNewCoreConfiguration(t *testing.T) { assert.Equal(t, want, got) }) } + +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)) + r.Reset() + 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 { + level zapcore.Level + expectedSev log.Severity + }{ + {zapcore.DebugLevel, log.SeverityDebug}, + {zapcore.InfoLevel, log.SeverityInfo}, + {zapcore.WarnLevel, log.SeverityWarn}, + {zapcore.ErrorLevel, log.SeverityError}, + {zapcore.DPanicLevel, log.SeverityFatal1}, + {zapcore.PanicLevel, log.SeverityFatal2}, + {zapcore.FatalLevel, log.SeverityFatal3}, + {zapcore.InvalidLevel, log.SeverityUndefined}, + } + + for _, test := range tests { + result := getOTelLevel(test.level) + if result != test.expectedSev { + t.Errorf("For level %v, expected %v but got %v", test.level, test.expectedSev, result) + } + } +} + +// Tests [Core] write method. +func TestCoreWrite(t *testing.T) { + rec := logtest.NewRecorder() + zc := NewCore(WithLoggerProvider(rec)) + + err := zc.Write(entry, []zap.Field{field}) + if err != nil { + t.Errorf("Error occurred: %v", err) + } + + // why is index 1 populated with results and not 0? + got := rec.Result()[1].Records[0] + assert.Equal(t, testBodyString, got.Body().AsString()) + assert.Equal(t, testSeverity, got.Severity()) + + // TODO test record attributes +} From ac3cd9dbb975d7ebb894b65870982d93e041ff17 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Thu, 9 May 2024 10:12:30 +0530 Subject: [PATCH 02/19] write tests for with and write --- bridges/otelzap/core.go | 1 - bridges/otelzap/core_test.go | 35 ++++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index c48a547e546..b88ebbda37d 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -96,7 +96,6 @@ func WithLoggerProvider(provider log.LoggerProvider) Option { // Core is a [zapcore.Core] that sends logging records to OpenTelemetry. type Core struct { logger log.Logger - ctx context.Context attr []log.KeyValue } diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 94a71d062c9..3fd07f332f6 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -74,7 +74,6 @@ func TestCoreEnabled(t *testing.T) { } r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc)) - r.Reset() zc := NewCore(WithLoggerProvider(r)) assert.False(t, zc.Enabled(zap.DebugLevel), "level conversion: permissive") @@ -106,19 +105,33 @@ func TestGetOTelLevel(t *testing.T) { } // Tests [Core] write method. -func TestCoreWrite(t *testing.T) { +func TestCore(t *testing.T) { rec := logtest.NewRecorder() zc := NewCore(WithLoggerProvider(rec)) - err := zc.Write(entry, []zap.Field{field}) - if err != nil { - t.Errorf("Error occurred: %v", err) - } + 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) + } + + // why is index 1 populated with results and not 0? + got := rec.Result()[1].Records[0] + assert.Equal(t, testBodyString, got.Body().AsString()) + assert.Equal(t, testSeverity, got.Severity()) - // why is index 1 populated with results and not 0? - got := rec.Result()[1].Records[0] - assert.Equal(t, testBodyString, got.Body().AsString()) - assert.Equal(t, testSeverity, got.Severity()) + // TODO test record attributes + + 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() + }) - // TODO test record attributes } From 72110a2b90844d446f24949b0fe475eaf3bf1297 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Thu, 9 May 2024 10:13:22 +0530 Subject: [PATCH 03/19] remove comments --- bridges/otelzap/core.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index b88ebbda37d..a9e8d76b7b2 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -114,7 +114,6 @@ func NewCore(opts ...Option) *Core { func (o *Core) Enabled(level zapcore.Level) bool { r := log.Record{} r.SetSeverity(getOTelLevel(level)) - // should Enabled take the context set on Core? return o.logger.Enabled(context.Background(), r) } @@ -143,6 +142,7 @@ func (o *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.Check return ce } +// TODO // Write method encodes zap fields to OTel logs and emits them. func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { r := log.Record{} From 444be440d340f39d3da0d5b6b066b0792eda2acf Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Mon, 13 May 2024 09:17:40 +0530 Subject: [PATCH 04/19] Update bridges/otelzap/core.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert Pająk --- bridges/otelzap/core.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index a9e8d76b7b2..2d7d44ed11e 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -150,12 +150,11 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { r.SetBody(log.StringValue(ent.Message)) r.SetSeverity(getOTelLevel(ent.Level)) - if len(fields) > 0 { - // TODO map zap field to otel attributes - fmt.Println("TODO") - } else { - r.AddAttributes(o.attr...) - } + // 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 zap.Namespace. + // TODO: Handle logger name. o.logger.Emit(context.Background(), r) return nil From e3900ce26d0ac26483dc84d67a079555b93c25ec Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 13 May 2024 09:38:49 +0530 Subject: [PATCH 05/19] addressed requested changes --- bridges/otelzap/core.go | 33 ++++++++------------------------- bridges/otelzap/core_test.go | 35 +++-------------------------------- 2 files changed, 11 insertions(+), 57 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 2d7d44ed11e..26f63f357fc 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -8,7 +8,6 @@ package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap" import ( "context" "fmt" - "slices" "go.uber.org/zap/zapcore" @@ -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. @@ -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 @@ -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 @@ -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 { diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 3fd07f332f6..3d862055063 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -4,7 +4,6 @@ package otelzap import ( - "context" "testing" "github.com/stretchr/testify/assert" @@ -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) { @@ -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 { @@ -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] @@ -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() - }) - } From 16e183eef4157b04aee3c3ef0a3b0970ff896ce7 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 13 May 2024 09:42:54 +0530 Subject: [PATCH 06/19] addressed requested changes --- bridges/otelzap/core_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 3d862055063..2f99bf8cfa5 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -101,8 +101,6 @@ func TestCore(t *testing.T) { assert.Equal(t, testBodyString, got.Body().AsString()) assert.Equal(t, testSeverity, got.Severity()) - // TODO test record attributes - rec.Reset() }) } From e51ae343274131261679870f9c0329dce24bed9f Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 13 May 2024 10:14:24 +0530 Subject: [PATCH 07/19] changes --- bridges/otelzap/core_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 2f99bf8cfa5..a8860679329 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -96,7 +96,7 @@ func TestCore(t *testing.T) { logger := zap.New(zc) logger.Info(testBodyString, testField) - // why is index 1 populated with results and not 0? + // not sure index 1 populated with results and not 0 got := rec.Result()[1].Records[0] assert.Equal(t, testBodyString, got.Body().AsString()) assert.Equal(t, testSeverity, got.Severity()) From 262196dbc1311aae1fde62533ecf043b92fce399 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 13 May 2024 13:04:22 +0530 Subject: [PATCH 08/19] add check method --- bridges/otelzap/core.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 26f63f357fc..a90ceca7345 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -126,10 +126,12 @@ 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 { - return ce.AddCore(ent, o) + if o.Enabled(ent.Level) { + return ce.AddCore(ent, o) + } + return ce } // TODO From 80a541b6ca5f1950d00701a3c19d3c8a0d128f91 Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 13 May 2024 13:09:25 +0530 Subject: [PATCH 09/19] minor changes --- bridges/otelzap/core.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index a90ceca7345..3fd01692ea6 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -7,7 +7,6 @@ package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap" import ( "context" - "fmt" "go.uber.org/zap/zapcore" @@ -141,13 +140,11 @@ 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 containing trace context. // TODO: Handle zap.Namespace. // TODO: Handle logger name. - fmt.Println(r) o.logger.Emit(context.Background(), r) return nil } From f78d7022c258643f4c9291c445e72decadf4e27a Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Wed, 15 May 2024 19:00:02 +0530 Subject: [PATCH 10/19] Update bridges/otelzap/core.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert Pająk --- bridges/otelzap/core.go | 1 - 1 file changed, 1 deletion(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 3fd01692ea6..30ee4465df5 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -133,7 +133,6 @@ func (o *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.Check return ce } -// TODO // Write method encodes zap fields to OTel logs and emits them. func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { r := log.Record{} From 5059a868f72114b00f05e8d5f919d95c30078a4d Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Wed, 15 May 2024 19:00:19 +0530 Subject: [PATCH 11/19] Update bridges/otelzap/core.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert Pająk --- bridges/otelzap/core.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 30ee4465df5..d2639b4808b 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -148,8 +148,7 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { return nil } -// converts zap level to OTel log level. -func getOTelLevel(level zapcore.Level) log.Severity { +func convertLevel(level zapcore.Level) log.Severity { switch level { case zapcore.DebugLevel: return log.SeverityDebug From a3c6c83b39315b58cd9c610a062ad542b45fd5bf Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Wed, 15 May 2024 19:13:10 +0530 Subject: [PATCH 12/19] accomdodate feedback --- bridges/otelzap/core.go | 4 +++- bridges/otelzap/core_test.go | 7 +++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index d2639b4808b..4f2242e8529 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -138,12 +138,14 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { r := log.Record{} r.SetTimestamp(ent.Time) r.SetBody(log.StringValue(ent.Message)) - r.SetSeverity(getOTelLevel(ent.Level)) + 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 logger name. + o.logger.Emit(context.Background(), r) return nil } diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index a8860679329..ee08256ec46 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -80,7 +80,7 @@ func TestGetOTelLevel(t *testing.T) { } for _, test := range tests { - result := getOTelLevel(test.level) + result := convertLevel(test.level) if result != test.expectedSev { t.Errorf("For level %v, expected %v but got %v", test.level, test.expectedSev, result) } @@ -93,14 +93,13 @@ func TestCore(t *testing.T) { zc := NewCore(WithLoggerProvider(rec)) t.Run("test Write method of Core", func(t *testing.T) { + defer rec.Reset() logger := zap.New(zc) logger.Info(testBodyString, testField) - // not sure index 1 populated with results and not 0 + // not sure why index 1 is populated with results and not 0 got := rec.Result()[1].Records[0] assert.Equal(t, testBodyString, got.Body().AsString()) assert.Equal(t, testSeverity, got.Severity()) - - rec.Reset() }) } From 02489df97f0e46c24ebead26aa1a84616ddc5c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 15 May 2024 15:57:36 +0200 Subject: [PATCH 13/19] Update core_test.go --- bridges/otelzap/core_test.go | 37 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index ee08256ec46..4c0c38cf589 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -18,11 +18,22 @@ import ( ) var ( - testBodyString = "log message" - testSeverity = log.SeverityInfo - testField = zap.String("key", "testValue") + testMessage = "log message" ) +func TestCore(t *testing.T) { + rec := logtest.NewRecorder() + zc := NewCore(WithLoggerProvider(rec)) + logger := zap.New(zc) + + logger.Info(testMessage) + + // TODO: not sure why index 1 is populated with results and not 0 + got := rec.Result()[1].Records[0] + assert.Equal(t, testMessage, got.Body().AsString()) + assert.Equal(t, log.SeverityInfo, got.Severity()) +} + func TestNewCoreConfiguration(t *testing.T) { t.Run("Default", func(t *testing.T) { r := logtest.NewRecorder() @@ -63,8 +74,7 @@ func TestNewCoreConfiguration(t *testing.T) { }) } -// Test conversion of Zap Level to OTel level. -func TestGetOTelLevel(t *testing.T) { +func TestConvertLevel(t *testing.T) { tests := []struct { level zapcore.Level expectedSev log.Severity @@ -86,20 +96,3 @@ func TestGetOTelLevel(t *testing.T) { } } } - -// Tests [Core] write method. -func TestCore(t *testing.T) { - rec := logtest.NewRecorder() - zc := NewCore(WithLoggerProvider(rec)) - - t.Run("test Write method of Core", func(t *testing.T) { - defer rec.Reset() - logger := zap.New(zc) - logger.Info(testBodyString, testField) - - // not sure why index 1 is populated with results and not 0 - got := rec.Result()[1].Records[0] - assert.Equal(t, testBodyString, got.Body().AsString()) - assert.Equal(t, testSeverity, got.Severity()) - }) -} From b10604ad2d15b80d445a3b665e364a546adacb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 15 May 2024 16:04:42 +0200 Subject: [PATCH 14/19] format --- bridges/otelzap/core_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 4c0c38cf589..1a7a4e5528c 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -17,9 +17,7 @@ import ( "go.opentelemetry.io/otel/sdk/instrumentation" ) -var ( - testMessage = "log message" -) +var testMessage = "log message" func TestCore(t *testing.T) { rec := logtest.NewRecorder() From d3e6ee40112d69af2938bcb91a75b0e53efa6831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 15 May 2024 16:11:18 +0200 Subject: [PATCH 15/19] Update core_test.go --- bridges/otelzap/core_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index 1a7a4e5528c..d51a267092e 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -26,7 +26,7 @@ func TestCore(t *testing.T) { logger.Info(testMessage) - // TODO: not sure why index 1 is populated with results and not 0 + // TODO (#5580): Not sure why index 1 is populated with results and not 0. got := rec.Result()[1].Records[0] assert.Equal(t, testMessage, got.Body().AsString()) assert.Equal(t, log.SeverityInfo, got.Severity()) From 1d91526488b03264730c0b802a0d36a36eb1100f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 15 May 2024 17:09:10 +0200 Subject: [PATCH 16/19] Update core.go --- bridges/otelzap/core.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 4f2242e8529..c59232caef7 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -144,6 +144,8 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { // 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 logger name. o.logger.Emit(context.Background(), r) From fbf0ddd65b65c39a4aa18240395a47f1198a6ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 15 May 2024 17:21:50 +0200 Subject: [PATCH 17/19] Update core.go --- bridges/otelzap/core.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index c59232caef7..cab858bd201 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -146,7 +146,7 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error { // TODO: Handle zap.Namespace. // TODO: Handle zap.Object. // TODO: Handle zap.Array. - // TODO: Handle logger name. + // TODO: Handle ent.LoggerName. o.logger.Emit(context.Background(), r) return nil From baa33cb061a1d5856cdccabd602b0e958b1fd7dd Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Wed, 15 May 2024 20:56:31 +0530 Subject: [PATCH 18/19] Update bridges/otelzap/core.go Co-authored-by: Damien Mathieu <42@dmathieu.com> --- bridges/otelzap/core.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index cab858bd201..6ea0bf0ad56 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -126,6 +126,7 @@ func (o *Core) Sync() error { } // Check determines whether the supplied Entry should be logged using core.Enabled method. +// If the entry should be logged, the Core adds itself to the CheckedEntry and returns the result. func (o *Core) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { if o.Enabled(ent.Level) { return ce.AddCore(ent, o) From accb80cd7bf9c814c1425da3f2b0c91435888a41 Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Wed, 15 May 2024 21:01:50 +0530 Subject: [PATCH 19/19] Update bridges/otelzap/core_test.go Co-authored-by: Damien Mathieu <42@dmathieu.com> --- bridges/otelzap/core_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index d51a267092e..e4babb78a15 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -17,9 +17,8 @@ import ( "go.opentelemetry.io/otel/sdk/instrumentation" ) -var testMessage = "log message" - func TestCore(t *testing.T) { + testMessage := "log message" rec := logtest.NewRecorder() zc := NewCore(WithLoggerProvider(rec)) logger := zap.New(zc)