diff --git a/otelslog/otel_handler_test.go b/otelslog/otel_handler_test.go index 6d57915..2573c27 100644 --- a/otelslog/otel_handler_test.go +++ b/otelslog/otel_handler_test.go @@ -66,6 +66,6 @@ func TestNewOtelHandler(t *testing.T) { actual := buf.String() - assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.0.1] {host.name=") + assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.2.0] {host.name=") assert.Contains(t, actual, "service.name=otelslog-example, service.version=1.0.0, baggage.key=true, first=value1, group1.second=value2, group1.group2.myKey=myValue, group1.group2.myGroup.groupKey=groupValue}") } diff --git a/otelzap/core.go b/otelzap/core.go index c4887b2..852c312 100644 --- a/otelzap/core.go +++ b/otelzap/core.go @@ -33,8 +33,8 @@ const ( type otlpCore struct { logger otel.Logger - fields []zapcore.Field - level zapcore.Level + fields []zapcore.Field + levelEnabler zapcore.LevelEnabler } var instrumentationScope = instrumentation.Scope{ @@ -44,7 +44,7 @@ var instrumentationScope = instrumentation.Scope{ } func (c *otlpCore) Enabled(level zapcore.Level) bool { - return c.level.Enabled(level) + return c.levelEnabler.Enabled(level) } func (c *otlpCore) With(f []zapcore.Field) zapcore.Core { @@ -52,9 +52,9 @@ func (c *otlpCore) With(f []zapcore.Field) zapcore.Core { fields = append(fields, f...) return &otlpCore{ - logger: c.logger, - fields: fields, - level: c.level, + logger: c.logger, + fields: fields, + levelEnabler: c.levelEnabler, } } diff --git a/otelzap/core_test.go b/otelzap/core_test.go new file mode 100644 index 0000000..d3a65c7 --- /dev/null +++ b/otelzap/core_test.go @@ -0,0 +1,20 @@ +package otelzap + +import ( + sdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs" + "github.com/stretchr/testify/assert" + "go.uber.org/zap" + "testing" +) + +func TestLevelEnabler_ChangeLevelAfterCreation(t *testing.T) { + atomicLevel := zap.NewAtomicLevelAt(zap.WarnLevel) + loggerProvider := sdk.NewLoggerProvider() + core := NewOtelCore(loggerProvider, WithLevelEnabler(atomicLevel)) + + assert.False(t, core.Enabled(zap.InfoLevel)) + + atomicLevel.SetLevel(zap.InfoLevel) + + assert.True(t, core.Enabled(zap.InfoLevel)) +} diff --git a/otelzap/otelzap.go b/otelzap/otelzap.go index 16c986e..ec1188b 100644 --- a/otelzap/otelzap.go +++ b/otelzap/otelzap.go @@ -29,8 +29,8 @@ func NewOtelCore(loggerProvider otel.LoggerProvider, opts ...Option) zapcore.Cor ) c := &otlpCore{ - logger: logger, - level: zapcore.InfoLevel, + logger: logger, + levelEnabler: zapcore.InfoLevel, } for _, apply := range opts { apply(c) @@ -45,6 +45,13 @@ type Option func(c *otlpCore) // WithLevel sets the minimum level for the OpenTelemetry Core log to be exported func WithLevel(level zapcore.Level) Option { return Option(func(c *otlpCore) { - c.level = level + c.levelEnabler = level + }) +} + +// WithLevelEnabler sets the zapcore.LevelEnabler for determining which log levels to export +func WithLevelEnabler(levelEnabler zapcore.LevelEnabler) Option { + return Option(func(c *otlpCore) { + c.levelEnabler = levelEnabler }) }