From f0b4f4df3de071a453ee3aed0d07a488cac14767 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Mon, 15 Jul 2024 22:05:42 +0100 Subject: [PATCH 01/10] worked on the implementation of run method --- bridges/otelzerolog/hook.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index 2928396240b..366ae3816e6 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -7,6 +7,8 @@ package otelzerolog // import "go.opentelemetry.io/contrib/bridges/otelzerolog" import ( + "time" + "github.com/rs/zerolog" "go.opentelemetry.io/otel/log" @@ -100,5 +102,29 @@ func NewHook(name string, options ...Option) *Hook { // Run handles the passed record, and sends it to OpenTelemetry. func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { - // TODO + r := log.Record{} + r.SetSeverity(convertLevel(level)) + r.SetBody(log.StringValue(msg)) + r.SetTimestamp(time.Now()) + r.SetSeverityText(level.String()) + h.logger.Emit(e.GetCtx(), r) +} + +func convertLevel(level zerolog.Level) log.Severity { + switch level { + case zerolog.DebugLevel: + return log.SeverityDebug + case zerolog.InfoLevel: + return log.SeverityInfo + case zerolog.WarnLevel: + return log.SeverityWarn + case zerolog.ErrorLevel: + return log.SeverityError + case zerolog.PanicLevel: + return log.SeverityFatal1 + case zerolog.FatalLevel: + return log.SeverityFatal2 + default: + return log.SeverityUndefined + } } From f8b80cc1adb914da2543afce8fa0c7cc92534e88 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Tue, 16 Jul 2024 11:51:15 +0100 Subject: [PATCH 02/10] added tests --- bridges/otelzerolog/hook_test.go | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/bridges/otelzerolog/hook_test.go b/bridges/otelzerolog/hook_test.go index 273e9c73c59..baad25cdf09 100644 --- a/bridges/otelzerolog/hook_test.go +++ b/bridges/otelzerolog/hook_test.go @@ -3,19 +3,23 @@ package otelzerolog import ( + "os" + // "reflect". "testing" + // "time". + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/embedded" "go.opentelemetry.io/otel/log/global" + "go.opentelemetry.io/otel/log/logtest" ) type mockLoggerProvider struct { embedded.LoggerProvider } - func (mockLoggerProvider) Logger(name string, options ...log.LoggerOption) log.Logger { return nil } @@ -103,3 +107,30 @@ func TestNewHook(t *testing.T) { }) } } + +var ( + testMessage = "log message" + loggerName = "name" + testKey = "key" + testValue = "value" + testEntry = zerolog.InfoLevel +) + +func TestHookRun(t *testing.T) { + rec := logtest.NewRecorder() + hook := NewHook(loggerName, WithLoggerProvider(rec)) + + logger := zerolog.New(os.Stderr).Hook(hook) + + t.Run("Run", func(t *testing.T) { + // Create an event and run the hook + event := logger.Info().Str(testKey, testValue) + hook.Run(event, testEntry, testMessage) + + // Check the results + got := rec.Result()[0].Records[0] + assert.Equal(t, testMessage, got.Body().AsString()) + assert.Equal(t, log.SeverityInfo, got.Severity()) + assert.Equal(t, zerolog.InfoLevel.String(), got.SeverityText()) + }) +} From 506d648564a53987f5e9c9a837b63313bbaa7253 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Tue, 16 Jul 2024 12:15:09 +0100 Subject: [PATCH 03/10] added test for convertLevel --- bridges/otelzerolog/hook_test.go | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/bridges/otelzerolog/hook_test.go b/bridges/otelzerolog/hook_test.go index baad25cdf09..8d16f2fd173 100644 --- a/bridges/otelzerolog/hook_test.go +++ b/bridges/otelzerolog/hook_test.go @@ -20,6 +20,7 @@ import ( type mockLoggerProvider struct { embedded.LoggerProvider } + func (mockLoggerProvider) Logger(name string, options ...log.LoggerOption) log.Logger { return nil } @@ -134,3 +135,54 @@ func TestHookRun(t *testing.T) { assert.Equal(t, zerolog.InfoLevel.String(), got.SeverityText()) }) } + +func TestConvertLevel(t *testing.T) { + tests := []struct { + name string + zerologLevel zerolog.Level + expected log.Severity + }{ + { + name: "DebugLevel", + zerologLevel: zerolog.DebugLevel, + expected: log.SeverityDebug, + }, + { + name: "InfoLevel", + zerologLevel: zerolog.InfoLevel, + expected: log.SeverityInfo, + }, + { + name: "WarnLevel", + zerologLevel: zerolog.WarnLevel, + expected: log.SeverityWarn, + }, + { + name: "ErrorLevel", + zerologLevel: zerolog.ErrorLevel, + expected: log.SeverityError, + }, + { + name: "PanicLevel", + zerologLevel: zerolog.PanicLevel, + expected: log.SeverityFatal1, + }, + { + name: "FatalLevel", + zerologLevel: zerolog.FatalLevel, + expected: log.SeverityFatal2, + }, + { + name: "UnknownLevel", + zerologLevel: zerolog.NoLevel, // An unknown level + expected: log.SeverityUndefined, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := convertLevel(tt.zerologLevel) + assert.Equal(t, tt.expected, actual, "severity mismatch") + }) + } +} From be12da6e06e7773a9108ba6a6296dd450f0a2b54 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Tue, 16 Jul 2024 12:32:01 +0100 Subject: [PATCH 04/10] removed comment --- bridges/otelzerolog/hook_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/bridges/otelzerolog/hook_test.go b/bridges/otelzerolog/hook_test.go index 8d16f2fd173..d0fd35b64dc 100644 --- a/bridges/otelzerolog/hook_test.go +++ b/bridges/otelzerolog/hook_test.go @@ -4,10 +4,7 @@ package otelzerolog import ( "os" - // "reflect". "testing" - // "time". - "github.com/rs/zerolog" "github.com/stretchr/testify/assert" From ca43efc5f14c028f98690fae400edb766238544b Mon Sep 17 00:00:00 2001 From: Akhigbe Eromosele David Date: Tue, 16 Jul 2024 12:32:42 +0100 Subject: [PATCH 05/10] Update bridges/otelzerolog/hook_test.go Co-authored-by: Damien Mathieu <42@dmathieu.com> --- bridges/otelzerolog/hook_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bridges/otelzerolog/hook_test.go b/bridges/otelzerolog/hook_test.go index d0fd35b64dc..a1ddc95aa90 100644 --- a/bridges/otelzerolog/hook_test.go +++ b/bridges/otelzerolog/hook_test.go @@ -126,6 +126,8 @@ func TestHookRun(t *testing.T) { hook.Run(event, testEntry, testMessage) // Check the results + require.Len(t, rec.Result(), 1) + require.Len(t, rec.Result()[0].Records, 1) got := rec.Result()[0].Records[0] assert.Equal(t, testMessage, got.Body().AsString()) assert.Equal(t, log.SeverityInfo, got.Severity()) From 5c25409e5e23c81f0ed198e7fb9e07cb9ed7564e Mon Sep 17 00:00:00 2001 From: Akhigbe Eromosele David Date: Tue, 16 Jul 2024 12:32:58 +0100 Subject: [PATCH 06/10] Update bridges/otelzerolog/hook.go Co-authored-by: Damien Mathieu <42@dmathieu.com> --- bridges/otelzerolog/hook.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index 366ae3816e6..782f84b53f5 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -107,6 +107,11 @@ func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { r.SetBody(log.StringValue(msg)) r.SetTimestamp(time.Now()) r.SetSeverityText(level.String()) + + // TODO: add support for attributes + // This is limited by zerolog's unability to retrieve fields. + // https://github.com/rs/zerolog/issues/493 + h.logger.Emit(e.GetCtx(), r) } From 145763144cb2e6d62f982d9b31a5586b8a644907 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Tue, 16 Jul 2024 12:45:24 +0100 Subject: [PATCH 07/10] added require --- bridges/otelzerolog/hook.go | 2 +- bridges/otelzerolog/hook_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index 782f84b53f5..718bfaad1f9 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -107,7 +107,7 @@ func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { r.SetBody(log.StringValue(msg)) r.SetTimestamp(time.Now()) r.SetSeverityText(level.String()) - + // TODO: add support for attributes // This is limited by zerolog's unability to retrieve fields. // https://github.com/rs/zerolog/issues/493 diff --git a/bridges/otelzerolog/hook_test.go b/bridges/otelzerolog/hook_test.go index a1ddc95aa90..27f23a4a192 100644 --- a/bridges/otelzerolog/hook_test.go +++ b/bridges/otelzerolog/hook_test.go @@ -5,8 +5,10 @@ package otelzerolog import ( "os" "testing" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/embedded" From a375571fdb5a818e61638ab361e9ee3bb83bbff4 Mon Sep 17 00:00:00 2001 From: Akhigbe Eromosele David Date: Wed, 17 Jul 2024 13:02:16 +0100 Subject: [PATCH 08/10] Update bridges/otelzerolog/hook.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert PajÄ…k --- bridges/otelzerolog/hook.go | 1 - 1 file changed, 1 deletion(-) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index 718bfaad1f9..cf307e2fabe 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -105,7 +105,6 @@ func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { r := log.Record{} r.SetSeverity(convertLevel(level)) r.SetBody(log.StringValue(msg)) - r.SetTimestamp(time.Now()) r.SetSeverityText(level.String()) // TODO: add support for attributes From 568f9dd5f0c81cd78ff7e438cf260b7d3bfbaae8 Mon Sep 17 00:00:00 2001 From: EROMOSELE AKHIGBE Date: Wed, 17 Jul 2024 13:10:10 +0100 Subject: [PATCH 09/10] Updated the hook --- bridges/otelzerolog/hook.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index cf307e2fabe..3794119c141 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -7,8 +7,6 @@ package otelzerolog // import "go.opentelemetry.io/contrib/bridges/otelzerolog" import ( - "time" - "github.com/rs/zerolog" "go.opentelemetry.io/otel/log" From 86c709e2121335f98376f5c640e3baf4fbf8a911 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 17 Jul 2024 08:10:57 -0700 Subject: [PATCH 10/10] Update bridges/otelzerolog/hook.go --- bridges/otelzerolog/hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridges/otelzerolog/hook.go b/bridges/otelzerolog/hook.go index 3794119c141..5f8327f1f01 100644 --- a/bridges/otelzerolog/hook.go +++ b/bridges/otelzerolog/hook.go @@ -106,7 +106,7 @@ func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) { r.SetSeverityText(level.String()) // TODO: add support for attributes - // This is limited by zerolog's unability to retrieve fields. + // This is limited by zerolog's inability to retrieve fields. // https://github.com/rs/zerolog/issues/493 h.logger.Emit(e.GetCtx(), r)