Skip to content

Commit

Permalink
[exporter/logging] Decouple loglevel field from level of logged mes…
Browse files Browse the repository at this point in the history
…sages (#5678)

* [exporter/logging] Decouple `loglevel` field from level of logged messages

* Update CHANGELOG.md

Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
mx-psi and Alex Boten authored Jul 13, 2022
1 parent d48e930 commit a831d51
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧰

Expand Down
6 changes: 3 additions & 3 deletions exporter/loggingexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
35 changes: 18 additions & 17 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +32,7 @@ import (
)

type loggingExporter struct {
logLevel zapcore.Level
logger *zap.Logger
logsMarshaler plog.Marshaler
metricsMarshaler pmetric.Marshaler
Expand All @@ -41,50 +41,51 @@ 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
}

buf, err := s.tracesMarshaler.MarshalTraces(td)
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
}

buf, err := s.metricsMarshaler.MarshalMetrics(md)
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
}

buf, err := s.logsMarshaler.MarshalLogs(ld)
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(),
Expand All @@ -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}),
Expand All @@ -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}),
Expand All @@ -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}),
Expand Down
13 changes: 8 additions & 5 deletions exporter/loggingexporter/logging_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ 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"
"go.opentelemetry.io/collector/pdata/ptrace"
)

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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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")
Expand Down

0 comments on commit a831d51

Please sign in to comment.