From 9608e5b789287ae30d5ba57e078f768b474c0b6a Mon Sep 17 00:00:00 2001 From: Cheng-Lung Sung Date: Tue, 31 Dec 2019 13:07:28 +0800 Subject: [PATCH 1/2] add option.File for stdout output --- exporter/trace/stdout/stdout.go | 8 +++++++- exporter/trace/stdout/stdout_test.go | 8 +++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/exporter/trace/stdout/stdout.go b/exporter/trace/stdout/stdout.go index e69795a2b84..0047046e0aa 100644 --- a/exporter/trace/stdout/stdout.go +++ b/exporter/trace/stdout/stdout.go @@ -25,6 +25,9 @@ import ( // 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 + // 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.File == nil { + o.File = os.Stdout + } return &Exporter{ pretty: o.PrettyPrint, - outputWriter: os.Stdout, + outputWriter: o.File, }, nil } diff --git a/exporter/trace/stdout/stdout_test.go b/exporter/trace/stdout/stdout_test.go index 1f4e0c98a9c..dc774173cc7 100644 --- a/exporter/trace/stdout/stdout_test.go +++ b/exporter/trace/stdout/stdout_test.go @@ -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{File: &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") From 9fc43209d62b03e4ed6c78ec824d74613699ed9b Mon Sep 17 00:00:00 2001 From: Cheng-Lung Sung Date: Wed, 1 Jan 2020 03:04:12 +0800 Subject: [PATCH 2/2] Unify the naming of `Writer` field (io.Writer) in Options --- api/global/internal/meter_test.go | 2 +- exporter/metric/stdout/stdout.go | 10 +++++----- exporter/metric/stdout/stdout_test.go | 4 ++-- exporter/trace/stdout/stdout.go | 10 +++++----- exporter/trace/stdout/stdout_test.go | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/api/global/internal/meter_test.go b/api/global/internal/meter_test.go index e48d5b47662..13e1f04f5bb 100644 --- a/api/global/internal/meter_test.go +++ b/api/global/internal/meter_test.go @@ -222,7 +222,7 @@ func TestDefaultSDK(t *testing.T) { return pusher }(stdout.Options{ - File: out, + Writer: out, DoNotPrintTime: true, }) diff --git a/exporter/metric/stdout/stdout.go b/exporter/metric/stdout/stdout.go index 9a25b2c2608..357c48e30ba 100644 --- a/exporter/metric/stdout/stdout.go +++ b/exporter/metric/stdout/stdout.go @@ -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 } diff --git a/exporter/metric/stdout/stdout_test.go b/exporter/metric/stdout/stdout_test.go index ed680709bfa..1f68c61544b 100644 --- a/exporter/metric/stdout/stdout_test.go +++ b/exporter/metric/stdout/stdout_test.go @@ -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 { diff --git a/exporter/trace/stdout/stdout.go b/exporter/trace/stdout/stdout.go index 0047046e0aa..99ad69574aa 100644 --- a/exporter/trace/stdout/stdout.go +++ b/exporter/trace/stdout/stdout.go @@ -25,8 +25,8 @@ import ( // 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. @@ -40,12 +40,12 @@ type Exporter struct { } func NewExporter(o Options) (*Exporter, error) { - if o.File == nil { - o.File = os.Stdout + if o.Writer == nil { + o.Writer = os.Stdout } return &Exporter{ pretty: o.PrettyPrint, - outputWriter: o.File, + outputWriter: o.Writer, }, nil } diff --git a/exporter/trace/stdout/stdout_test.go b/exporter/trace/stdout/stdout_test.go index 1424f6ead38..f619bd0dc6e 100644 --- a/exporter/trace/stdout/stdout_test.go +++ b/exporter/trace/stdout/stdout_test.go @@ -32,7 +32,7 @@ import ( func TestExporter_ExportSpan(t *testing.T) { // write to buffer for testing var b bytes.Buffer - ex, err := NewExporter(Options{File: &b}) + ex, err := NewExporter(Options{Writer: &b}) if err != nil { t.Errorf("Error constructing stdout exporter %s", err) }