Skip to content

Commit

Permalink
make WithWriter a shortcut to WithEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Sep 14, 2023
1 parent 9ab5ae3 commit 8040f1a
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions exporters/stdout/stdoutmetric/config.go
Expand Up @@ -23,7 +23,6 @@ import (

// config contains options for the exporter.
type config struct {
writer io.Writer
prettyPrint bool
encoder *encoderHolder
temporalitySelector metric.TemporalitySelector
Expand All @@ -38,16 +37,15 @@ func newConfig(options ...Option) config {
cfg = opt.apply(cfg)
}

if cfg.writer == nil {
cfg.writer = os.Stdout
if cfg.encoder == nil {
enc := json.NewEncoder(os.Stdout)
cfg.encoder = &encoderHolder{encoder: enc}
}

if cfg.encoder == nil {
enc := json.NewEncoder(cfg.writer)
if cfg.prettyPrint {
enc.SetIndent("", "\t")
if cfg.prettyPrint {
if e, ok := cfg.encoder.encoder.(*json.Encoder); ok {
e.SetIndent("", "\t")
}
cfg.encoder = &encoderHolder{encoder: enc}
}

if cfg.temporalitySelector == nil {
Expand Down Expand Up @@ -86,10 +84,7 @@ func WithEncoder(encoder Encoder) Option {
// WithWriter sets the export stream destination.
// If `WithEncoder` is also used, this option will have no effect.
func WithWriter(w io.Writer) Option {
return optionFunc(func(c config) config {
c.writer = w
return c
})
return WithEncoder(json.NewEncoder(w))
}

// WithPrettyPrint sets the export stream format to use JSON.
Expand Down

0 comments on commit 8040f1a

Please sign in to comment.