2020-07-22 12:34:44 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 07:05:28 +01:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-22 12:34:44 -07:00
|
|
|
|
2021-06-16 11:32:42 -04:00
|
|
|
package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
2020-07-22 12:34:44 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-06-16 11:32:42 -04:00
|
|
|
defaultWriter = os.Stdout
|
|
|
|
|
defaultPrettyPrint = false
|
|
|
|
|
defaultTimestamps = true
|
2020-07-22 12:34:44 -07:00
|
|
|
)
|
|
|
|
|
|
2021-05-14 22:28:28 +02:00
|
|
|
// config contains options for the STDOUT exporter.
|
|
|
|
|
type config struct {
|
2020-07-22 12:34:44 -07:00
|
|
|
// Writer is the destination. If not set, os.Stdout is used.
|
|
|
|
|
Writer io.Writer
|
|
|
|
|
|
|
|
|
|
// PrettyPrint will encode the output into readable JSON. Default is
|
|
|
|
|
// false.
|
|
|
|
|
PrettyPrint bool
|
|
|
|
|
|
2021-06-16 11:32:42 -04:00
|
|
|
// Timestamps specifies if timestamps should be printed. Default is
|
2020-07-22 12:34:44 -07:00
|
|
|
// true.
|
|
|
|
|
Timestamps bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 22:28:28 +02:00
|
|
|
// newConfig creates a validated Config configured with options.
|
2024-06-22 01:02:07 +02:00
|
|
|
func newConfig(options ...Option) config {
|
2021-05-14 22:28:28 +02:00
|
|
|
cfg := config{
|
2021-06-16 11:32:42 -04:00
|
|
|
Writer: defaultWriter,
|
|
|
|
|
PrettyPrint: defaultPrettyPrint,
|
|
|
|
|
Timestamps: defaultTimestamps,
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
|
|
|
|
for _, opt := range options {
|
2022-02-01 13:51:23 -08:00
|
|
|
cfg = opt.apply(cfg)
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
2024-06-22 01:02:07 +02:00
|
|
|
return cfg
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Option sets the value of an option for a Config.
|
|
|
|
|
type Option interface {
|
2022-02-01 13:51:23 -08:00
|
|
|
apply(config) config
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithWriter sets the export stream destination.
|
|
|
|
|
func WithWriter(w io.Writer) Option {
|
|
|
|
|
return writerOption{w}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type writerOption struct {
|
|
|
|
|
W io.Writer
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-01 13:51:23 -08:00
|
|
|
func (o writerOption) apply(cfg config) config {
|
2021-05-14 22:28:28 +02:00
|
|
|
cfg.Writer = o.W
|
2022-02-01 13:51:23 -08:00
|
|
|
return cfg
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 20:27:28 +02:00
|
|
|
// WithPrettyPrint prettifies the emitted output.
|
2020-07-22 12:34:44 -07:00
|
|
|
func WithPrettyPrint() Option {
|
|
|
|
|
return prettyPrintOption(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type prettyPrintOption bool
|
|
|
|
|
|
2022-02-01 13:51:23 -08:00
|
|
|
func (o prettyPrintOption) apply(cfg config) config {
|
2021-05-14 22:28:28 +02:00
|
|
|
cfg.PrettyPrint = bool(o)
|
2022-02-01 13:51:23 -08:00
|
|
|
return cfg
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithoutTimestamps sets the export stream to not include timestamps.
|
|
|
|
|
func WithoutTimestamps() Option {
|
|
|
|
|
return timestampsOption(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type timestampsOption bool
|
|
|
|
|
|
2022-02-01 13:51:23 -08:00
|
|
|
func (o timestampsOption) apply(cfg config) config {
|
2021-05-14 22:28:28 +02:00
|
|
|
cfg.Timestamps = bool(o)
|
2022-02-01 13:51:23 -08:00
|
|
|
return cfg
|
2020-07-22 12:34:44 -07:00
|
|
|
}
|