Skip to content

Commit

Permalink
Replace 'zapcore.Level' with more general 'zapcore.LevelEnabler'. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshans authored Dec 11, 2024
1 parent 9cedd33 commit 63dee5a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion otelslog/otel_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
12 changes: 6 additions & 6 deletions otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -44,17 +44,17 @@ 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 {
fields := c.fields
fields = append(fields, f...)

return &otlpCore{
logger: c.logger,
fields: fields,
level: c.level,
logger: c.logger,
fields: fields,
levelEnabler: c.levelEnabler,
}
}

Expand Down
20 changes: 20 additions & 0 deletions otelzap/core_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
13 changes: 10 additions & 3 deletions otelzap/otelzap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
})
}

0 comments on commit 63dee5a

Please sign in to comment.