1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-04 09:43:23 +02:00

[trace] add option.Writer for stdout output (#404)

* add option.File for stdout output

* Unify the naming of `Writer` field (io.Writer) in Options
This commit is contained in:
Cheng-Lung Sung 2020-01-03 00:40:37 +08:00 committed by Liz Fong-Jones
parent 501cd75de6
commit 5eb457a119
5 changed files with 18 additions and 14 deletions

View File

@ -222,7 +222,7 @@ func TestDefaultSDK(t *testing.T) {
return pusher
}(stdout.Options{
File: out,
Writer: out,
DoNotPrintTime: true,
})

View File

@ -35,8 +35,8 @@ var _ export.Exporter = &Exporter{}
// Options are the options to be used when initializing a stdout export.
type Options struct {
// File is the destination. If not set, os.Stdout is used.
File io.Writer
// Writer is the destination. If not set, os.Stdout is used.
Writer io.Writer
// PrettyPrint will pretty the json representation of the span,
// making it print "pretty". Default is false.
@ -81,8 +81,8 @@ type expoQuantile struct {
}
func New(options Options) (*Exporter, error) {
if options.File == nil {
options.File = os.Stdout
if options.Writer == nil {
options.Writer = os.Stdout
}
if options.Quantiles == nil {
options.Quantiles = []float64{0.5, 0.9, 0.99}
@ -218,7 +218,7 @@ func (e *Exporter) Export(_ context.Context, checkpointSet export.CheckpointSet)
}
if err == nil {
fmt.Fprintln(e.options.File, string(data))
fmt.Fprintln(e.options.Writer, string(data))
} else {
return err
}

View File

@ -34,7 +34,7 @@ type testFixture struct {
func newFixture(t *testing.T, options stdout.Options) testFixture {
buf := &bytes.Buffer{}
options.File = buf
options.Writer = buf
options.DoNotPrintTime = true
exp, err := stdout.New(options)
if err != nil {
@ -70,7 +70,7 @@ func TestStdoutInvalidQuantile(t *testing.T) {
func TestStdoutTimestamp(t *testing.T) {
var buf bytes.Buffer
exporter, err := stdout.New(stdout.Options{
File: &buf,
Writer: &buf,
DoNotPrintTime: false,
})
if err != nil {

View File

@ -25,6 +25,9 @@ import (
// Options are the options to be used when initializing a stdout export.
type Options struct {
// Writer is the destination. If not set, os.Stdout is used.
Writer io.Writer
// PrettyPrint will pretty the json representation of the span,
// making it print "pretty". Default is false.
PrettyPrint bool
@ -37,9 +40,12 @@ type Exporter struct {
}
func NewExporter(o Options) (*Exporter, error) {
if o.Writer == nil {
o.Writer = os.Stdout
}
return &Exporter{
pretty: o.PrettyPrint,
outputWriter: os.Stdout,
outputWriter: o.Writer,
}, nil
}

View File

@ -30,15 +30,13 @@ import (
)
func TestExporter_ExportSpan(t *testing.T) {
ex, err := NewExporter(Options{})
// write to buffer for testing
var b bytes.Buffer
ex, err := NewExporter(Options{Writer: &b})
if err != nil {
t.Errorf("Error constructing stdout exporter %s", err)
}
// override output writer for testing
var b bytes.Buffer
ex.outputWriter = &b
// setup test span
now := time.Now()
traceID, _ := core.TraceIDFromHex("0102030405060708090a0b0c0d0e0f10")