From a831d516f02bb01216c4c1bb6464286840ab45d1 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Wed, 13 Jul 2022 16:26:25 +0200 Subject: [PATCH] [exporter/logging] Decouple `loglevel` field from level of logged messages (#5678) * [exporter/logging] Decouple `loglevel` field from level of logged messages * Update CHANGELOG.md Co-authored-by: Alex Boten --- CHANGELOG.md | 1 + exporter/loggingexporter/factory.go | 6 ++-- exporter/loggingexporter/logging_exporter.go | 35 ++++++++++--------- .../loggingexporter/logging_exporter_test.go | 13 ++++--- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 330e197e129..898f1c4a2f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add `linux-ppc64le` architecture to cross build tests in CI - `client`: perform case insensitive lookups in case the requested metadata value isn't found (#5646) +- `loggingexporter`: Decouple `loglevel` field from level of logged messages (#5678) ### 🧰 Bug fixes 🧰 diff --git a/exporter/loggingexporter/factory.go b/exporter/loggingexporter/factory.go index 87404fd9ce1..4dc64da9d68 100644 --- a/exporter/loggingexporter/factory.go +++ b/exporter/loggingexporter/factory.go @@ -59,7 +59,7 @@ func createTracesExporter(_ context.Context, set component.ExporterCreateSetting return nil, err } - return newTracesExporter(config, exporterLogger, set) + return newTracesExporter(cfg, exporterLogger, set) } func createMetricsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.MetricsExporter, error) { @@ -70,7 +70,7 @@ func createMetricsExporter(_ context.Context, set component.ExporterCreateSettin return nil, err } - return newMetricsExporter(config, exporterLogger, set) + return newMetricsExporter(cfg, exporterLogger, set) } func createLogsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.LogsExporter, error) { @@ -81,7 +81,7 @@ func createLogsExporter(_ context.Context, set component.ExporterCreateSettings, return nil, err } - return newLogsExporter(config, exporterLogger, set) + return newLogsExporter(cfg, exporterLogger, set) } func createLogger(cfg *Config) (*zap.Logger, error) { diff --git a/exporter/loggingexporter/logging_exporter.go b/exporter/loggingexporter/logging_exporter.go index 0b066fd19ff..dc57319429a 100644 --- a/exporter/loggingexporter/logging_exporter.go +++ b/exporter/loggingexporter/logging_exporter.go @@ -23,7 +23,6 @@ import ( "go.uber.org/zap/zapcore" "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/loggingexporter/internal/otlptext" @@ -33,6 +32,7 @@ import ( ) type loggingExporter struct { + logLevel zapcore.Level logger *zap.Logger logsMarshaler plog.Marshaler metricsMarshaler pmetric.Marshaler @@ -41,7 +41,7 @@ type loggingExporter struct { func (s *loggingExporter) pushTraces(_ context.Context, td ptrace.Traces) error { s.logger.Info("TracesExporter", zap.Int("#spans", td.SpanCount())) - if !s.logger.Core().Enabled(zapcore.DebugLevel) { + if s.logLevel != zapcore.DebugLevel { return nil } @@ -49,14 +49,14 @@ func (s *loggingExporter) pushTraces(_ context.Context, td ptrace.Traces) error if err != nil { return err } - s.logger.Debug(string(buf)) + s.logger.Info(string(buf)) return nil } func (s *loggingExporter) pushMetrics(_ context.Context, md pmetric.Metrics) error { s.logger.Info("MetricsExporter", zap.Int("#metrics", md.MetricCount())) - if !s.logger.Core().Enabled(zapcore.DebugLevel) { + if s.logLevel != zapcore.DebugLevel { return nil } @@ -64,14 +64,14 @@ func (s *loggingExporter) pushMetrics(_ context.Context, md pmetric.Metrics) err if err != nil { return err } - s.logger.Debug(string(buf)) + s.logger.Info(string(buf)) return nil } func (s *loggingExporter) pushLogs(_ context.Context, ld plog.Logs) error { s.logger.Info("LogsExporter", zap.Int("#logs", ld.LogRecordCount())) - if !s.logger.Core().Enabled(zapcore.DebugLevel) { + if s.logLevel != zapcore.DebugLevel { return nil } @@ -79,12 +79,13 @@ func (s *loggingExporter) pushLogs(_ context.Context, ld plog.Logs) error { if err != nil { return err } - s.logger.Debug(string(buf)) + s.logger.Info(string(buf)) return nil } -func newLoggingExporter(logger *zap.Logger) *loggingExporter { +func newLoggingExporter(logger *zap.Logger, logLevel zapcore.Level) *loggingExporter { return &loggingExporter{ + logLevel: logLevel, logger: logger, logsMarshaler: otlptext.NewTextLogsMarshaler(), metricsMarshaler: otlptext.NewTextMetricsMarshaler(), @@ -94,10 +95,10 @@ func newLoggingExporter(logger *zap.Logger) *loggingExporter { // newTracesExporter creates an exporter.TracesExporter that just drops the // received data and logs debugging messages. -func newTracesExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.TracesExporter, error) { - s := newLoggingExporter(logger) +func newTracesExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.TracesExporter, error) { + s := newLoggingExporter(logger, cfg.LogLevel) return exporterhelper.NewTracesExporter( - config, + cfg, set, s.pushTraces, exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}), @@ -111,10 +112,10 @@ func newTracesExporter(config config.Exporter, logger *zap.Logger, set component // newMetricsExporter creates an exporter.MetricsExporter that just drops the // received data and logs debugging messages. -func newMetricsExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.MetricsExporter, error) { - s := newLoggingExporter(logger) +func newMetricsExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.MetricsExporter, error) { + s := newLoggingExporter(logger, cfg.LogLevel) return exporterhelper.NewMetricsExporter( - config, + cfg, set, s.pushMetrics, exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}), @@ -128,10 +129,10 @@ func newMetricsExporter(config config.Exporter, logger *zap.Logger, set componen // newLogsExporter creates an exporter.LogsExporter that just drops the // received data and logs debugging messages. -func newLogsExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.LogsExporter, error) { - s := newLoggingExporter(logger) +func newLogsExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.LogsExporter, error) { + s := newLoggingExporter(logger, cfg.LogLevel) return exporterhelper.NewLogsExporter( - config, + cfg, set, s.pushLogs, exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}), diff --git a/exporter/loggingexporter/logging_exporter_test.go b/exporter/loggingexporter/logging_exporter_test.go index 17122ed6877..bac0fdc7ed7 100644 --- a/exporter/loggingexporter/logging_exporter_test.go +++ b/exporter/loggingexporter/logging_exporter_test.go @@ -21,10 +21,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" + "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest" "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/config" "go.opentelemetry.io/collector/internal/testdata" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" @@ -32,7 +32,8 @@ import ( ) func TestLoggingTracesExporterNoErrors(t *testing.T) { - lte, err := newTracesExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings()) + f := NewFactory() + lte, err := newTracesExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings()) require.NotNil(t, lte) assert.NoError(t, err) @@ -43,7 +44,8 @@ func TestLoggingTracesExporterNoErrors(t *testing.T) { } func TestLoggingMetricsExporterNoErrors(t *testing.T) { - lme, err := newMetricsExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings()) + f := NewFactory() + lme, err := newMetricsExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings()) require.NotNil(t, lme) assert.NoError(t, err) @@ -57,7 +59,8 @@ func TestLoggingMetricsExporterNoErrors(t *testing.T) { } func TestLoggingLogsExporterNoErrors(t *testing.T) { - lle, err := newLogsExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings()) + f := NewFactory() + lle, err := newLogsExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings()) require.NotNil(t, lle) assert.NoError(t, err) @@ -68,7 +71,7 @@ func TestLoggingLogsExporterNoErrors(t *testing.T) { } func TestLoggingExporterErrors(t *testing.T) { - le := newLoggingExporter(zaptest.NewLogger(t)) + le := newLoggingExporter(zaptest.NewLogger(t), zapcore.DebugLevel) require.NotNil(t, le) errWant := errors.New("my error")