Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otelzap: remove dependency on logs sdk #5604

Merged
merged 6 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 26 additions & 32 deletions bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ import (

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/sdk/instrumentation"
)

const (
bridgeName = "go.opentelemetry.io/contrib/bridges/otelzap"
)

type config struct {
provider log.LoggerProvider
scope instrumentation.Scope
provider log.LoggerProvider
version string
schemaURL string
}

func newConfig(options []Option) config {
Expand All @@ -30,30 +26,22 @@ func newConfig(options []Option) config {
c = opt.apply(c)
}

var emptyScope instrumentation.Scope
if c.scope == emptyScope {
c.scope = instrumentation.Scope{
Name: bridgeName,
Version: version,
}
}

if c.provider == nil {
c.provider = global.GetLoggerProvider()
}

return c
}

func (c config) logger() log.Logger {
func (c config) logger(name string) log.Logger {
var opts []log.LoggerOption
if c.scope.Version != "" {
opts = append(opts, log.WithInstrumentationVersion(c.scope.Version))
if c.version != "" {
opts = append(opts, log.WithInstrumentationVersion(c.version))
}
if c.scope.SchemaURL != "" {
opts = append(opts, log.WithSchemaURL(c.scope.SchemaURL))
if c.schemaURL != "" {
opts = append(opts, log.WithSchemaURL(c.schemaURL))
}
return c.provider.Logger(c.scope.Name, opts...)
return c.provider.Logger(name, opts...)
}

// Option configures a [Core].
Expand All @@ -65,16 +53,22 @@ type optFunc func(config) config

func (f optFunc) apply(c config) config { return f(c) }

// WithInstrumentationScope returns an [Option] that configures the scope of
// the [log.Logger] used by a [Core].
//
// By default if this Option is not provided, [Core] will use a default
// instrumentation scope describing this bridge package. It is recommended to
// provide this so log data can be associated with its source package or
// module.
func WithInstrumentationScope(scope instrumentation.Scope) Option {
// WithVersion returns an [Option] that configures the version of the
// [log.Logger] used by a [Core]. The version should be the version of the
// package that is being logged.
func WithVersion(version string) Option {
return optFunc(func(c config) config {
c.version = version
return c
})
}

// WithSchemaURL returns an [Option] that configures the semantic convention
// schema URL of the [log.Logger] used by a [Core]. The schemaURL should be
// the schema URL for the semantic conventions used in log records.
func WithSchemaURL(schemaURL string) Option {
return optFunc(func(c config) config {
c.scope = scope
c.schemaURL = schemaURL
return c
})
}
Expand All @@ -100,10 +94,10 @@ type Core struct {
var _ zapcore.Core = (*Core)(nil)

// NewCore creates a new [zapcore.Core] that can be used with [go.uber.org/zap.New].
func NewCore(opts ...Option) *Core {
func NewCore(name string, opts ...Option) *Core {
cfg := newConfig(opts)
return &Core{
logger: cfg.logger(),
logger: cfg.logger(name),
}
}

Expand Down
21 changes: 12 additions & 9 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import (
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/log/logtest"
"go.opentelemetry.io/otel/sdk/instrumentation"
)

var testMessage = "log message"
var (
testMessage = "log message"
loggerName = "name"
)

func TestCore(t *testing.T) {
rec := logtest.NewRecorder()
zc := NewCore(WithLoggerProvider(rec))
zc := NewCore(loggerName, WithLoggerProvider(rec))
logger := zap.New(zc)

logger.Info(testMessage)
Expand All @@ -39,7 +41,7 @@ func TestCoreEnabled(t *testing.T) {
}

r := logtest.NewRecorder(logtest.WithEnabledFunc(enabledFunc))
logger := zap.New(NewCore(WithLoggerProvider(r)))
logger := zap.New(NewCore(loggerName, WithLoggerProvider(r)))

logger.Debug(testMessage)
assert.Empty(t, r.Result()[1].Records)
Expand All @@ -66,33 +68,34 @@ func TestNewCoreConfiguration(t *testing.T) {
global.SetLoggerProvider(r)

var h *Core
require.NotPanics(t, func() { h = NewCore() })
require.NotPanics(t, func() { h = NewCore(loggerName) })
require.NotNil(t, h.logger)
require.IsType(t, &logtest.Recorder{}, h.logger)
l := h.logger.(*logtest.Recorder)
require.Len(t, l.Result(), 1)

want := &logtest.ScopeRecords{Name: bridgeName, Version: version}
want := &logtest.ScopeRecords{Name: loggerName}
got := l.Result()[0]
assert.Equal(t, want, got)
})

t.Run("Options", func(t *testing.T) {
r := logtest.NewRecorder()
scope := instrumentation.Scope{Name: "name", Version: "ver", SchemaURL: "url"}
var h *Core
require.NotPanics(t, func() {
h = NewCore(
loggerName,
WithLoggerProvider(r),
WithInstrumentationScope(scope),
WithVersion("1.0.0"),
WithSchemaURL("url"),
)
})
require.NotNil(t, h.logger)
require.IsType(t, &logtest.Recorder{}, h.logger)
l := h.logger.(*logtest.Recorder)
require.Len(t, l.Result(), 1)

want := &logtest.ScopeRecords{Name: scope.Name, Version: scope.Version, SchemaURL: scope.SchemaURL}
want := &logtest.ScopeRecords{Name: loggerName, Version: "1.0.0", SchemaURL: "url"}
got := l.Result()[0]
assert.Equal(t, want, got)
})
Expand Down
1 change: 0 additions & 1 deletion bridges/otelzap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel/log v0.2.0-alpha
go.opentelemetry.io/otel/sdk v1.26.0
go.uber.org/zap v1.27.0
)

Expand Down
2 changes: 0 additions & 2 deletions bridges/otelzap/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ go.opentelemetry.io/otel/log v0.2.0-alpha h1:ixOPvMzserpqA07SENHvRzkZOsnG0XbPr74
go.opentelemetry.io/otel/log v0.2.0-alpha/go.mod h1:vbFZc65yq4c4ssvXY43y/nIqkNJLxORrqw0L85P59LA=
go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30=
go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4=
go.opentelemetry.io/otel/sdk v1.26.0 h1:Y7bumHf5tAiDlRYFmGqetNcLaVUZmh4iYfmGxtmz7F8=
go.opentelemetry.io/otel/sdk v1.26.0/go.mod h1:0p8MXpqLeJ0pzcszQQN4F0S5FVjBLgypeGSngLsmirs=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down
7 changes: 0 additions & 7 deletions bridges/otelzap/version.go

This file was deleted.