From 3d531909bd95c9c19220591f336ec31f1cc4c912 Mon Sep 17 00:00:00 2001 From: Khushi Jain Date: Sat, 18 May 2024 20:58:47 +0530 Subject: [PATCH] otelzap: remove dependency on logs sdk (#5604) Part of https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5191 and #5586 Pre-work https://github.com/open-telemetry/opentelemetry-go-contrib/pull/5279 --------- Co-authored-by: Tyler Yahn --- bridges/otelzap/core.go | 58 ++++++++++++++++-------------------- bridges/otelzap/core_test.go | 21 +++++++------ bridges/otelzap/go.mod | 1 - bridges/otelzap/go.sum | 2 -- bridges/otelzap/version.go | 7 ----- 5 files changed, 38 insertions(+), 51 deletions(-) delete mode 100644 bridges/otelzap/version.go diff --git a/bridges/otelzap/core.go b/bridges/otelzap/core.go index 956f5d2657a..8a1b80ade7e 100644 --- a/bridges/otelzap/core.go +++ b/bridges/otelzap/core.go @@ -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 { @@ -30,14 +26,6 @@ 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() } @@ -45,15 +33,15 @@ func newConfig(options []Option) config { 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]. @@ -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 }) } @@ -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), } } diff --git a/bridges/otelzap/core_test.go b/bridges/otelzap/core_test.go index dc9cb18624c..78621e491fb 100644 --- a/bridges/otelzap/core_test.go +++ b/bridges/otelzap/core_test.go @@ -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) @@ -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) @@ -66,25 +68,26 @@ 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) @@ -92,7 +95,7 @@ func TestNewCoreConfiguration(t *testing.T) { 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) }) diff --git a/bridges/otelzap/go.mod b/bridges/otelzap/go.mod index 5028fef49d1..d3349245721 100644 --- a/bridges/otelzap/go.mod +++ b/bridges/otelzap/go.mod @@ -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 ) diff --git a/bridges/otelzap/go.sum b/bridges/otelzap/go.sum index d2ab0f5b3a1..f5531fd670c 100644 --- a/bridges/otelzap/go.sum +++ b/bridges/otelzap/go.sum @@ -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= diff --git a/bridges/otelzap/version.go b/bridges/otelzap/version.go deleted file mode 100644 index 2c3a0431ac4..00000000000 --- a/bridges/otelzap/version.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap" - -// Version is the current release version of otelzap in use. -const version = "0.1.0"