Skip to content

Commit

Permalink
[exporter/logging] Create logging exporter logger from service logger (
Browse files Browse the repository at this point in the history
…#5677)

Build the logger on the logging exporter's logger from the one on `TelemetrySettings`, so that the logger honors the configuration settings on `telemetry::logs` and the configuration options on `CollectorSettings.LoggingOptions`.

Depends on #5678

**Link to tracking Issue:** Fixes #5652

**Documentation:** Documented the feature gate.
  • Loading branch information
mx-psi authored Jul 15, 2022
1 parent a7c047b commit 5f506a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `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)
- Expose `pcommon.NewSliceFromRaw` function (#5679)
- `loggingexporter`: create the exporter's logger from the service's logger (#5677)

### 🧰 Bug fixes 🧰

Expand Down
44 changes: 12 additions & 32 deletions exporter/loggingexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

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
}

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
}

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)
}

0 comments on commit 5f506a9

Please sign in to comment.