| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" |
| 5 |
|
| 6 |
import ( |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
) |
| 10 |
|
| 11 |
var ( |
| 12 |
defaultWriter = os.Stdout |
| 13 |
defaultPrettyPrint = false |
| 14 |
defaultTimestamps = true |
| 15 |
) |
| 16 |
|
| 17 |
// config contains options for the STDOUT exporter. |
| 18 |
type config struct { |
| 19 |
// Writer is the destination. If not set, os.Stdout is used. |
| 20 |
Writer io.Writer |
| 21 |
|
| 22 |
// PrettyPrint will encode the output into readable JSON. Default is |
| 23 |
// false. |
| 24 |
PrettyPrint bool |
| 25 |
|
| 26 |
// Timestamps specifies if timestamps should be printed. Default is |
| 27 |
// true. |
| 28 |
Timestamps bool |
| 29 |
} |
| 30 |
|
| 31 |
// newConfig creates a validated Config configured with options. |
| 32 |
func newConfig(options ...Option) config { |
| 33 |
cfg := config{ |
| 34 |
Writer: defaultWriter, |
| 35 |
PrettyPrint: defaultPrettyPrint, |
| 36 |
Timestamps: defaultTimestamps, |
| 37 |
} |
| 38 |
for _, opt := range options { |
| 39 |
cfg = opt.apply(cfg) |
| 40 |
} |
| 41 |
return cfg |
| 42 |
} |
| 43 |
|
| 44 |
// Option sets the value of an option for a Config. |
| 45 |
type Option interface { |
| 46 |
apply(config) config |
| 47 |
} |
| 48 |
|
| 49 |
// WithWriter sets the export stream destination. |
| 50 |
func WithWriter(w io.Writer) Option { |
| 51 |
return writerOption{w} |
| 52 |
} |
| 53 |
|
| 54 |
type writerOption struct { |
| 55 |
W io.Writer |
| 56 |
} |
| 57 |
|
| 58 |
func (o writerOption) apply(cfg config) config { |
| 59 |
cfg.Writer = o.W |
| 60 |
return cfg |
| 61 |
} |
| 62 |
|
| 63 |
// WithPrettyPrint prettifies the emitted output. |
| 64 |
func WithPrettyPrint() Option { |
| 65 |
return prettyPrintOption(true) |
| 66 |
} |
| 67 |
|
| 68 |
type prettyPrintOption bool |
| 69 |
|
| 70 |
func (o prettyPrintOption) apply(cfg config) config { |
| 71 |
cfg.PrettyPrint = bool(o) |
| 72 |
return cfg |
| 73 |
} |
| 74 |
|
| 75 |
// WithoutTimestamps sets the export stream to not include timestamps. |
| 76 |
func WithoutTimestamps() Option { |
| 77 |
return timestampsOption(false) |
| 78 |
} |
| 79 |
|
| 80 |
type timestampsOption bool |
| 81 |
|
| 82 |
func (o timestampsOption) apply(cfg config) config { |
| 83 |
cfg.Timestamps = bool(o) |
| 84 |
return cfg |
| 85 |
} |