1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-04-11 11:21:59 +02:00

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

* 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
api/global/internal
exporter

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

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

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

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

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