diff --git a/CHANGELOG.md b/CHANGELOG.md index 55ca926e812..a56a4e78f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### 💡 Enhancements 💡 - Add `linux-ppc64le` architecture to cross build tests in CI +- `loggingexporter`: Decouple `loglevel` field from level of logged messages (#5678) +- `loggingexporter`: create the exporter's logger from the service's logger (#5677) ### 🧰 Bug fixes 🧰 diff --git a/exporter/loggingexporter/factory.go b/exporter/loggingexporter/factory.go index 87404fd9ce1..dca2659ebf9 100644 --- a/exporter/loggingexporter/factory.go +++ b/exporter/loggingexporter/factory.go @@ -16,6 +16,7 @@ package loggingexporter // import "go.opentelemetry.io/collector/exporter/loggin import ( "context" + "time" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -53,50 +54,29 @@ func createDefaultConfig() config.Exporter { func createTracesExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.TracesExporter, error) { cfg := config.(*Config) - - exporterLogger, err := createLogger(cfg) - if err != nil { - return nil, err - } - - return newTracesExporter(config, exporterLogger, set) + exporterLogger := createLogger(cfg, set.TelemetrySettings.Logger) + return newTracesExporter(cfg, exporterLogger, set) } func createMetricsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.MetricsExporter, error) { cfg := config.(*Config) - - exporterLogger, err := createLogger(cfg) - if err != nil { - return nil, err - } - - return newMetricsExporter(config, exporterLogger, set) + exporterLogger := createLogger(cfg, set.TelemetrySettings.Logger) + return newMetricsExporter(cfg, exporterLogger, set) } func createLogsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.LogsExporter, error) { cfg := config.(*Config) - - exporterLogger, err := createLogger(cfg) - if err != nil { - return nil, err - } - - return newLogsExporter(config, exporterLogger, set) + exporterLogger := createLogger(cfg, set.TelemetrySettings.Logger) + return newLogsExporter(cfg, exporterLogger, set) } -func createLogger(cfg *Config) (*zap.Logger, error) { - // We take development config as the base since it matches the purpose - // of logging exporter being used for debugging reasons (so e.g. console encoder) - conf := zap.NewDevelopmentConfig() - conf.Level = zap.NewAtomicLevelAt(cfg.LogLevel) - conf.Sampling = &zap.SamplingConfig{ - Initial: cfg.SamplingInitial, - Thereafter: cfg.SamplingThereafter, - } +func createLogger(cfg *Config, logger *zap.Logger) *zap.Logger { + core := zapcore.NewSamplerWithOptions( + logger.Core(), + 1*time.Second, + cfg.SamplingInitial, + cfg.SamplingThereafter, + ) - logginglogger, err := conf.Build() - if err != nil { - return nil, err - } - return logginglogger, nil + return zap.New(core) } 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")