From 5538d847ce8afed1243d067b6a41910bc6e0dd11 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 4 Jul 2024 13:46:29 -0700 Subject: [PATCH 01/13] [service] add logger provider configuration support This allows us to use the otel-go/config package to support configuring external destinations for logs. I'm putting this in draft to gather community feedback on whether this is a desirable feature for the collector. Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- cmd/otelcorecol/go.sum | 2 ++ internal/e2e/go.sum | 2 ++ otelcol/go.sum | 2 ++ otelcol/otelcoltest/go.sum | 2 ++ service/go.sum | 2 ++ service/telemetry/config.go | 4 ++++ service/telemetry/logger.go | 26 +++++++++++++++++++++++++- 7 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/otelcorecol/go.sum b/cmd/otelcorecol/go.sum index 9779d4dece0..b26b61d2042 100644 --- a/cmd/otelcorecol/go.sum +++ b/cmd/otelcorecol/go.sum @@ -109,6 +109,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 h1:yMkBS9yViCc7U7yeLzJPM2XizlfdVvBRSmsQDWu6qc0= diff --git a/internal/e2e/go.sum b/internal/e2e/go.sum index bf723e761bd..a363cb82140 100644 --- a/internal/e2e/go.sum +++ b/internal/e2e/go.sum @@ -101,6 +101,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 h1:yMkBS9yViCc7U7yeLzJPM2XizlfdVvBRSmsQDWu6qc0= diff --git a/otelcol/go.sum b/otelcol/go.sum index e6581163b83..5607df4a7f0 100644 --- a/otelcol/go.sum +++ b/otelcol/go.sum @@ -107,6 +107,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= diff --git a/otelcol/otelcoltest/go.sum b/otelcol/otelcoltest/go.sum index e6581163b83..5607df4a7f0 100644 --- a/otelcol/otelcoltest/go.sum +++ b/otelcol/otelcoltest/go.sum @@ -107,6 +107,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= diff --git a/service/go.sum b/service/go.sum index d1ebfb66b56..49e945e2da8 100644 --- a/service/go.sum +++ b/service/go.sum @@ -99,6 +99,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= +go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= diff --git a/service/telemetry/config.go b/service/telemetry/config.go index 6cdb12bf6db..15f647386c9 100644 --- a/service/telemetry/config.go +++ b/service/telemetry/config.go @@ -105,6 +105,10 @@ type LogsConfig struct { // // By default, there is no initial field. InitialFields map[string]any `mapstructure:"initial_fields"` + + // Processors allow configuration of log record processors to emit logs to + // any number of suported backends. + Processors []config.LogRecordProcessor `mapstructure:"processors"` } // LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index eb675bc459f..2f607e360dc 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -4,11 +4,30 @@ package telemetry // import "go.opentelemetry.io/collector/service/telemetry" import ( + "context" + + "go.opentelemetry.io/contrib/bridges/otelzap" + "go.opentelemetry.io/contrib/config" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) -func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) { +func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap.Logger, error) { + sdk, err := config.NewSDK( + config.WithContext(ctx), + config.WithOpenTelemetryConfiguration( + config.OpenTelemetryConfiguration{ + LoggerProvider: &config.LoggerProvider{ + Processors: cfg.Processors, + }, + }, + ), + ) + + if err != nil { + return nil, err + } + // Copied from NewProductionConfig. zapCfg := &zap.Config{ Level: zap.NewAtomicLevelAt(cfg.Level), @@ -28,6 +47,11 @@ func newLogger(cfg LogsConfig, options []zap.Option) (*zap.Logger, error) { } logger, err := zapCfg.Build(options...) + + logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + return otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(sdk.LoggerProvider())) + })) + if err != nil { return nil, err } From 24d147952e61fa077bff2b935b4518792a7b5144 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:45:23 -0700 Subject: [PATCH 02/13] check if there's processors Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index 2f607e360dc..e18e60617b8 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -48,9 +48,11 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. logger, err := zapCfg.Build(options...) - logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(sdk.LoggerProvider())) - })) + if len(cfg.Processors) > 0 { + logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + return otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(sdk.LoggerProvider())) + })) + } if err != nil { return nil, err From 57d7f9d12a099132ac4a8ce27dbd0bc4a4645a34 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:56:05 -0700 Subject: [PATCH 03/13] update name Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index e18e60617b8..3fb9ac42d15 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -50,7 +50,7 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. if len(cfg.Processors) > 0 { logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return otelzap.NewCore("my/pkg/name", otelzap.WithLoggerProvider(sdk.LoggerProvider())) + return otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", otelzap.WithLoggerProvider(sdk.LoggerProvider())) })) } From 67f0d1e5a9c158281493b9ead883854a47a76df6 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:56:56 -0700 Subject: [PATCH 04/13] add changelog Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- ...boten_add-log-record-processor-config.yaml | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/codeboten_add-log-record-processor-config.yaml diff --git a/.chloggen/codeboten_add-log-record-processor-config.yaml b/.chloggen/codeboten_add-log-record-processor-config.yaml new file mode 100644 index 00000000000..e194ce8e911 --- /dev/null +++ b/.chloggen/codeboten_add-log-record-processor-config.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: service + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: add support for using the otelzap bridge and emit logs using the OTel Go SDK + +# One or more tracking issues or pull requests related to the change +issues: [10544] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] From e973561d191940288eb8008cd68923f0496bcfad Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:40:08 -0700 Subject: [PATCH 05/13] fix lint Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index 3fb9ac42d15..2213f1c204e 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -49,7 +49,7 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. logger, err := zapCfg.Build(options...) if len(cfg.Processors) > 0 { - logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + logger = logger.WithOptions(zap.WrapCore(func(_ zapcore.Core) zapcore.Core { return otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", otelzap.WithLoggerProvider(sdk.LoggerProvider())) })) } From bd41890cdfcb146ce0d103a648868bce494df53e Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:26:40 -0700 Subject: [PATCH 06/13] fix Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/factory.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service/telemetry/factory.go b/service/telemetry/factory.go index 9f25402adbe..83129efbbb4 100644 --- a/service/telemetry/factory.go +++ b/service/telemetry/factory.go @@ -65,9 +65,9 @@ type Factory interface { // NewFactory creates a new Factory. func NewFactory() Factory { return newFactory(createDefaultConfig, - withLogger(func(_ context.Context, set Settings, cfg component.Config) (*zap.Logger, error) { + withLogger(func(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error) { c := *cfg.(*Config) - return newLogger(c.Logs, set.ZapOptions) + return newLogger(ctx, c.Logs, set.ZapOptions) }), withTracerProvider(func(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) { c := *cfg.(*Config) From 1f08e895c4a084d2fb3bfb73b7cc6a80afaef9ce Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:28:49 -0700 Subject: [PATCH 07/13] move logic to config SDK into block checking for processors Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index 2213f1c204e..de4ce14fbdf 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -13,21 +13,6 @@ import ( ) func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap.Logger, error) { - sdk, err := config.NewSDK( - config.WithContext(ctx), - config.WithOpenTelemetryConfiguration( - config.OpenTelemetryConfiguration{ - LoggerProvider: &config.LoggerProvider{ - Processors: cfg.Processors, - }, - }, - ), - ) - - if err != nil { - return nil, err - } - // Copied from NewProductionConfig. zapCfg := &zap.Config{ Level: zap.NewAtomicLevelAt(cfg.Level), @@ -49,6 +34,20 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. logger, err := zapCfg.Build(options...) if len(cfg.Processors) > 0 { + sdk, err := config.NewSDK( + config.WithContext(ctx), + config.WithOpenTelemetryConfiguration( + config.OpenTelemetryConfiguration{ + LoggerProvider: &config.LoggerProvider{ + Processors: cfg.Processors, + }, + }, + ), + ) + + if err != nil { + return nil, err + } logger = logger.WithOptions(zap.WrapCore(func(_ zapcore.Core) zapcore.Core { return otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", otelzap.WithLoggerProvider(sdk.LoggerProvider())) })) From c10cbd5d163f6f2b7cb53b2abc5b4ea36a6c4ef4 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Thu, 3 Oct 2024 15:52:11 -0700 Subject: [PATCH 08/13] lint Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index de4ce14fbdf..4e9bff24429 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -33,6 +33,10 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. logger, err := zapCfg.Build(options...) + if err != nil { + return nil, err + } + if len(cfg.Processors) > 0 { sdk, err := config.NewSDK( config.WithContext(ctx), @@ -53,9 +57,6 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. })) } - if err != nil { - return nil, err - } if cfg.Sampling != nil && cfg.Sampling.Enabled { logger = newSampledLogger(logger, cfg.Sampling) } From ca20cb414cb868515bb216932c52daf7f88f2ddb Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:19:58 -0700 Subject: [PATCH 09/13] bump to otelzap 0.6.0 Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- cmd/otelcorecol/go.sum | 4 ++-- internal/e2e/go.sum | 4 ++-- otelcol/go.sum | 4 ++-- otelcol/otelcoltest/go.sum | 4 ++-- service/go.sum | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/otelcorecol/go.sum b/cmd/otelcorecol/go.sum index b26b61d2042..5ebf230eaad 100644 --- a/cmd/otelcorecol/go.sum +++ b/cmd/otelcorecol/go.sum @@ -109,8 +109,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 h1:j8icMXyyqNf6HGuwlYhniPnVsbJIq7n+WirDu3VAJdQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0/go.mod h1:evIOZpl+kAlU5IsaYX2Siw+IbpacAZvXemVsgt70uvw= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 h1:yMkBS9yViCc7U7yeLzJPM2XizlfdVvBRSmsQDWu6qc0= diff --git a/internal/e2e/go.sum b/internal/e2e/go.sum index a363cb82140..ae0305e8300 100644 --- a/internal/e2e/go.sum +++ b/internal/e2e/go.sum @@ -101,8 +101,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 h1:j8icMXyyqNf6HGuwlYhniPnVsbJIq7n+WirDu3VAJdQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0/go.mod h1:evIOZpl+kAlU5IsaYX2Siw+IbpacAZvXemVsgt70uvw= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 h1:yMkBS9yViCc7U7yeLzJPM2XizlfdVvBRSmsQDWu6qc0= diff --git a/otelcol/go.sum b/otelcol/go.sum index 5607df4a7f0..d2c482061e8 100644 --- a/otelcol/go.sum +++ b/otelcol/go.sum @@ -107,8 +107,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 h1:j8icMXyyqNf6HGuwlYhniPnVsbJIq7n+WirDu3VAJdQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0/go.mod h1:evIOZpl+kAlU5IsaYX2Siw+IbpacAZvXemVsgt70uvw= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= diff --git a/otelcol/otelcoltest/go.sum b/otelcol/otelcoltest/go.sum index 5607df4a7f0..d2c482061e8 100644 --- a/otelcol/otelcoltest/go.sum +++ b/otelcol/otelcoltest/go.sum @@ -107,8 +107,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 h1:j8icMXyyqNf6HGuwlYhniPnVsbJIq7n+WirDu3VAJdQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0/go.mod h1:evIOZpl+kAlU5IsaYX2Siw+IbpacAZvXemVsgt70uvw= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= diff --git a/service/go.sum b/service/go.sum index 49e945e2da8..de8ad1301fb 100644 --- a/service/go.sum +++ b/service/go.sum @@ -99,8 +99,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0 h1:DKXgQ+nDW41ErBPLbRrrytiwfSBIP6v9i7uUKCDMnAc= -go.opentelemetry.io/contrib/bridges/otelzap v0.5.0/go.mod h1:ljh3EKpTWP9AWcaH+XRfMOUQlICdeMMk5MJvk7Xu4MQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 h1:j8icMXyyqNf6HGuwlYhniPnVsbJIq7n+WirDu3VAJdQ= +go.opentelemetry.io/contrib/bridges/otelzap v0.6.0/go.mod h1:evIOZpl+kAlU5IsaYX2Siw+IbpacAZvXemVsgt70uvw= go.opentelemetry.io/contrib/config v0.10.0 h1:2JknAzMaYjxrHkTnZh3eOme/Y2P5eHE2SWfhfV6Xd6c= go.opentelemetry.io/contrib/config v0.10.0/go.mod h1:aND2M6/KfNkntI5cyvHriR/zvZgPf8j9yETdSmvpfmc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s= From 0d370bab1e4b5aff37d4bc376e9ab839fbb2e20f Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:01:17 -0700 Subject: [PATCH 10/13] update logger to use NewTee to preserve existing logger config Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index 4e9bff24429..a5956223ee1 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -45,6 +45,8 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. LoggerProvider: &config.LoggerProvider{ Processors: cfg.Processors, }, + // TODO: add resource + // Resource: &config.Resource{}, }, ), ) @@ -52,9 +54,16 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. if err != nil { return nil, err } - logger = logger.WithOptions(zap.WrapCore(func(_ zapcore.Core) zapcore.Core { - return otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", otelzap.WithLoggerProvider(sdk.LoggerProvider())) + + logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { + return zapcore.NewTee( + logger.Core(), + otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", + otelzap.WithLoggerProvider(sdk.LoggerProvider()), + ), + ) })) + } if cfg.Sampling != nil && cfg.Sampling.Enabled { From 02461fc55593afd87539531d7e93e068fb7e7d13 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:42:59 -0700 Subject: [PATCH 11/13] add test, return logger provider in factory Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/go.mod | 2 +- service/service.go | 11 +++- service/telemetry/factory.go | 7 +- service/telemetry/factory_impl.go | 8 ++- service/telemetry/factory_test.go | 4 +- service/telemetry/logger.go | 50 ++++++++------ service/telemetry/logger_test.go | 105 ++++++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 30 deletions(-) create mode 100644 service/telemetry/logger_test.go diff --git a/service/go.mod b/service/go.mod index 742a9b912fc..4a84602a8d1 100644 --- a/service/go.mod +++ b/service/go.mod @@ -46,6 +46,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0 go.opentelemetry.io/otel/exporters/prometheus v0.53.0 go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.31.0 + go.opentelemetry.io/otel/log v0.7.0 go.opentelemetry.io/otel/metric v1.31.0 go.opentelemetry.io/otel/sdk v1.31.0 go.opentelemetry.io/otel/sdk/metric v1.31.0 @@ -107,7 +108,6 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.31.0 // indirect go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.7.0 // indirect go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 // indirect - go.opentelemetry.io/otel/log v0.7.0 // indirect go.opentelemetry.io/otel/sdk/log v0.7.0 // indirect go.opentelemetry.io/proto/otlp v1.3.1 // indirect golang.org/x/net v0.30.0 // indirect diff --git a/service/service.go b/service/service.go index 1c4d9a7dccf..e5215414597 100644 --- a/service/service.go +++ b/service/service.go @@ -11,6 +11,7 @@ import ( "fmt" "runtime" + "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/noop" sdkresource "go.opentelemetry.io/otel/sdk/resource" @@ -92,6 +93,7 @@ type Service struct { telemetrySettings component.TelemetrySettings host *graph.Host collectorConf *confmap.Conf + loggerProvider log.LoggerProvider } // New creates a new Service, its telemetry, and Components. @@ -122,10 +124,11 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) { ZapOptions: set.LoggingOptions, } - logger, err := telFactory.CreateLogger(ctx, telset, &cfg.Telemetry) + logger, lp, err := telFactory.CreateLogger(ctx, telset, &cfg.Telemetry) if err != nil { return nil, fmt.Errorf("failed to create logger: %w", err) } + srv.loggerProvider = lp tracerProvider, err := telFactory.CreateTracerProvider(ctx, telset, &cfg.Telemetry) if err != nil { @@ -250,6 +253,12 @@ func (srv *Service) shutdownTelemetry(ctx context.Context) error { err = multierr.Append(err, fmt.Errorf("failed to shutdown tracer provider: %w", shutdownErr)) } } + + if prov, ok := srv.loggerProvider.(shutdownable); ok { + if shutdownErr := prov.Shutdown(ctx); shutdownErr != nil { + err = multierr.Append(err, fmt.Errorf("failed to shutdown logger provider: %w", shutdownErr)) + } + } return err } diff --git a/service/telemetry/factory.go b/service/telemetry/factory.go index 83129efbbb4..fde93bba2c2 100644 --- a/service/telemetry/factory.go +++ b/service/telemetry/factory.go @@ -8,6 +8,7 @@ import ( "time" "go.opentelemetry.io/contrib/config" + "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" "go.uber.org/zap" @@ -50,7 +51,7 @@ type Factory interface { CreateDefaultConfig() component.Config // CreateLogger creates a logger. - CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error) + CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, log.LoggerProvider, error) // CreateTracerProvider creates a TracerProvider. CreateTracerProvider(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) @@ -65,9 +66,9 @@ type Factory interface { // NewFactory creates a new Factory. func NewFactory() Factory { return newFactory(createDefaultConfig, - withLogger(func(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error) { + withLogger(func(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, log.LoggerProvider, error) { c := *cfg.(*Config) - return newLogger(ctx, c.Logs, set.ZapOptions) + return newLogger(ctx, set, c) }), withTracerProvider(func(ctx context.Context, set Settings, cfg component.Config) (trace.TracerProvider, error) { c := *cfg.(*Config) diff --git a/service/telemetry/factory_impl.go b/service/telemetry/factory_impl.go index 439012723a0..364afbde9d6 100644 --- a/service/telemetry/factory_impl.go +++ b/service/telemetry/factory_impl.go @@ -6,6 +6,8 @@ package telemetry // import "go.opentelemetry.io/collector/service/telemetry" import ( "context" + "go.opentelemetry.io/otel/log" + lognoop "go.opentelemetry.io/otel/log/noop" "go.opentelemetry.io/otel/metric" metricnoop "go.opentelemetry.io/otel/metric/noop" "go.opentelemetry.io/otel/trace" @@ -45,7 +47,7 @@ func (f *factory) CreateDefaultConfig() component.Config { } // createLoggerFunc is the equivalent of Factory.CreateLogger. -type createLoggerFunc func(context.Context, Settings, component.Config) (*zap.Logger, error) +type createLoggerFunc func(context.Context, Settings, component.Config) (*zap.Logger, log.LoggerProvider, error) // withLogger overrides the default no-op logger. func withLogger(createLogger createLoggerFunc) factoryOption { @@ -54,9 +56,9 @@ func withLogger(createLogger createLoggerFunc) factoryOption { }) } -func (f *factory) CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, error) { +func (f *factory) CreateLogger(ctx context.Context, set Settings, cfg component.Config) (*zap.Logger, log.LoggerProvider, error) { if f.createLoggerFunc == nil { - return zap.NewNop(), nil + return zap.NewNop(), lognoop.NewLoggerProvider(), nil } return f.createLoggerFunc(ctx, set, cfg) } diff --git a/service/telemetry/factory_test.go b/service/telemetry/factory_test.go index 86ba38cd460..09cda6b7861 100644 --- a/service/telemetry/factory_test.go +++ b/service/telemetry/factory_test.go @@ -99,7 +99,7 @@ func TestTelemetryConfiguration(t *testing.T) { t.Run(tt.name, func(t *testing.T) { f := NewFactory() set := Settings{ZapOptions: []zap.Option{}} - logger, err := f.CreateLogger(context.Background(), set, tt.cfg) + logger, _, err := f.CreateLogger(context.Background(), set, tt.cfg) if tt.success { require.NoError(t, err) assert.NotNil(t, logger) @@ -158,7 +158,7 @@ func TestSampledLogger(t *testing.T) { f := NewFactory() ctx := context.Background() set := Settings{ZapOptions: []zap.Option{}} - logger, err := f.CreateLogger(ctx, set, tt.cfg) + logger, _, err := f.CreateLogger(ctx, set, tt.cfg) require.NoError(t, err) assert.NotNil(t, logger) }) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index a5956223ee1..1021922633b 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -8,22 +8,24 @@ import ( "go.opentelemetry.io/contrib/bridges/otelzap" "go.opentelemetry.io/contrib/config" + "go.opentelemetry.io/otel/log" + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) -func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap.Logger, error) { +func newLogger(ctx context.Context, set Settings, cfg Config) (*zap.Logger, log.LoggerProvider, error) { // Copied from NewProductionConfig. zapCfg := &zap.Config{ - Level: zap.NewAtomicLevelAt(cfg.Level), - Development: cfg.Development, - Encoding: cfg.Encoding, + Level: zap.NewAtomicLevelAt(cfg.Logs.Level), + Development: cfg.Logs.Development, + Encoding: cfg.Logs.Encoding, EncoderConfig: zap.NewProductionEncoderConfig(), - OutputPaths: cfg.OutputPaths, - ErrorOutputPaths: cfg.ErrorOutputPaths, - DisableCaller: cfg.DisableCaller, - DisableStacktrace: cfg.DisableStacktrace, - InitialFields: cfg.InitialFields, + OutputPaths: cfg.Logs.OutputPaths, + ErrorOutputPaths: cfg.Logs.ErrorOutputPaths, + DisableCaller: cfg.Logs.DisableCaller, + DisableStacktrace: cfg.Logs.DisableStacktrace, + InitialFields: cfg.Logs.InitialFields, } if zapCfg.Encoding == "console" { @@ -31,46 +33,54 @@ func newLogger(ctx context.Context, cfg LogsConfig, options []zap.Option) (*zap. zapCfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder } - logger, err := zapCfg.Build(options...) + logger, err := zapCfg.Build(set.ZapOptions...) if err != nil { - return nil, err + return nil, nil, err } - if len(cfg.Processors) > 0 { + var lp log.LoggerProvider + + if len(cfg.Logs.Processors) > 0 { + sch := semconv.SchemaURL + res := config.Resource{ + SchemaUrl: &sch, + Attributes: attributes(set, cfg), + } sdk, err := config.NewSDK( config.WithContext(ctx), config.WithOpenTelemetryConfiguration( config.OpenTelemetryConfiguration{ LoggerProvider: &config.LoggerProvider{ - Processors: cfg.Processors, + Processors: cfg.Logs.Processors, }, - // TODO: add resource - // Resource: &config.Resource{}, + Resource: &res, }, ), ) if err != nil { - return nil, err + return nil, nil, err } + lp = sdk.LoggerProvider() + logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { return zapcore.NewTee( logger.Core(), otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", - otelzap.WithLoggerProvider(sdk.LoggerProvider()), + otelzap.WithLoggerProvider(lp), ), ) })) } - if cfg.Sampling != nil && cfg.Sampling.Enabled { - logger = newSampledLogger(logger, cfg.Sampling) + if cfg.Logs.Sampling != nil && cfg.Logs.Sampling.Enabled { + logger = newSampledLogger(logger, cfg.Logs.Sampling) } - return logger, nil + return logger, lp, nil } func newSampledLogger(logger *zap.Logger, sc *LogsSamplingConfig) *zap.Logger { diff --git a/service/telemetry/logger_test.go b/service/telemetry/logger_test.go new file mode 100644 index 00000000000..de901d5142c --- /dev/null +++ b/service/telemetry/logger_test.go @@ -0,0 +1,105 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package telemetry // import "go.opentelemetry.io/collector/service/telemetry" + +import ( + "context" + "errors" + "reflect" + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/contrib/config" + "go.uber.org/zap/zapcore" +) + +func TestNewLogger(t *testing.T) { + tests := []struct { + name string + wantCoreType any + wantErr error + cfg Config + }{ + { + name: "no log config", + cfg: Config{}, + wantErr: errors.New("no encoder name specified"), + wantCoreType: nil, + }, + { + name: "log config with no processors", + cfg: Config{ + Logs: LogsConfig{ + Level: zapcore.DebugLevel, + Development: true, + Encoding: "console", + DisableCaller: true, + DisableStacktrace: true, + InitialFields: map[string]any{"fieldKey": "filed-value"}, + }, + }, + wantCoreType: "*zapcore.ioCore", + }, + { + name: "log config with processors and invalid config", + cfg: Config{ + Logs: LogsConfig{ + Encoding: "console", + Processors: []config.LogRecordProcessor{ + { + Batch: &config.BatchLogRecordProcessor{ + Exporter: config.LogRecordExporter{ + OTLP: &config.OTLP{}, + }, + }, + }, + }, + }, + }, + wantErr: errors.New("unsupported protocol \"\""), + }, + { + name: "log config with processors", + cfg: Config{ + Logs: LogsConfig{ + Level: zapcore.DebugLevel, + Development: true, + Encoding: "console", + DisableCaller: true, + DisableStacktrace: true, + InitialFields: map[string]any{"fieldKey": "filed-value"}, + Processors: []config.LogRecordProcessor{ + { + Batch: &config.BatchLogRecordProcessor{ + Exporter: config.LogRecordExporter{ + Console: config.Console{}, + }, + }, + }, + }, + }, + }, + wantCoreType: "zapcore.multiCore", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + l, lp, err := newLogger(context.Background(), Settings{}, tt.cfg) + if tt.wantErr != nil { + require.ErrorContains(t, err, tt.wantErr.Error()) + require.Nil(t, tt.wantCoreType) + } else { + require.NoError(t, err) + gotType := reflect.TypeOf(l.Core()).String() + require.Equal(t, tt.wantCoreType, gotType) + type shutdownable interface { + Shutdown(context.Context) error + } + if prov, ok := lp.(shutdownable); ok { + require.NoError(t, prov.Shutdown(context.Background())) + } + } + }) + } +} From ba20612627b1207d4e5c04d5c068fe0edf49b9c9 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:08:29 -0700 Subject: [PATCH 12/13] tidy Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- cmd/otelcorecol/go.mod | 1 + internal/e2e/go.mod | 1 + otelcol/go.mod | 1 + otelcol/otelcoltest/go.mod | 1 + service/go.mod | 1 + 5 files changed, 5 insertions(+) diff --git a/cmd/otelcorecol/go.mod b/cmd/otelcorecol/go.mod index 9970c1fb567..44490b8cc92 100644 --- a/cmd/otelcorecol/go.mod +++ b/cmd/otelcorecol/go.mod @@ -117,6 +117,7 @@ require ( go.opentelemetry.io/collector/receiver/receiverprofiles v0.112.0 // indirect go.opentelemetry.io/collector/semconv v0.112.0 // indirect go.opentelemetry.io/collector/service v0.112.0 // indirect + go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect go.opentelemetry.io/contrib/config v0.10.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect diff --git a/internal/e2e/go.mod b/internal/e2e/go.mod index 51e501efdee..f13f49a2ba1 100644 --- a/internal/e2e/go.mod +++ b/internal/e2e/go.mod @@ -96,6 +96,7 @@ require ( go.opentelemetry.io/collector/processor/processortest v0.112.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.112.0 // indirect go.opentelemetry.io/collector/semconv v0.112.0 // indirect + go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect go.opentelemetry.io/contrib/config v0.10.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect diff --git a/otelcol/go.mod b/otelcol/go.mod index 439d26e2a48..378ed38087c 100644 --- a/otelcol/go.mod +++ b/otelcol/go.mod @@ -82,6 +82,7 @@ require ( go.opentelemetry.io/collector/processor/processorprofiles v0.112.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.112.0 // indirect go.opentelemetry.io/collector/semconv v0.112.0 // indirect + go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect go.opentelemetry.io/contrib/propagators/b3 v1.31.0 // indirect go.opentelemetry.io/otel v1.31.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0 // indirect diff --git a/otelcol/otelcoltest/go.mod b/otelcol/otelcoltest/go.mod index 33dfbf29b3c..36e6a1c6f0b 100644 --- a/otelcol/otelcoltest/go.mod +++ b/otelcol/otelcoltest/go.mod @@ -80,6 +80,7 @@ require ( go.opentelemetry.io/collector/processor/processorprofiles v0.112.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.112.0 // indirect go.opentelemetry.io/collector/semconv v0.112.0 // indirect + go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 // indirect go.opentelemetry.io/contrib/config v0.10.0 // indirect go.opentelemetry.io/contrib/propagators/b3 v1.31.0 // indirect go.opentelemetry.io/otel v1.31.0 // indirect diff --git a/service/go.mod b/service/go.mod index 4a84602a8d1..b73de532061 100644 --- a/service/go.mod +++ b/service/go.mod @@ -39,6 +39,7 @@ require ( go.opentelemetry.io/collector/receiver v0.112.0 go.opentelemetry.io/collector/receiver/receiverprofiles v0.112.0 go.opentelemetry.io/collector/semconv v0.112.0 + go.opentelemetry.io/contrib/bridges/otelzap v0.6.0 go.opentelemetry.io/contrib/config v0.10.0 go.opentelemetry.io/contrib/propagators/b3 v1.31.0 go.opentelemetry.io/otel v1.31.0 From 1eef143158faddee4a742183c37e4e5f66354328 Mon Sep 17 00:00:00 2001 From: Alex Boten <223565+codeboten@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:22:47 -0700 Subject: [PATCH 13/13] fix lint Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- service/telemetry/logger.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/telemetry/logger.go b/service/telemetry/logger.go index 1021922633b..904c5084e8f 100644 --- a/service/telemetry/logger.go +++ b/service/telemetry/logger.go @@ -67,7 +67,7 @@ func newLogger(ctx context.Context, set Settings, cfg Config) (*zap.Logger, log. logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core { return zapcore.NewTee( - logger.Core(), + c, otelzap.NewCore("go.opentelemetry.io/collector/service/telemetry", otelzap.WithLoggerProvider(lp), ),