Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/global/internal/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestDefaultSDK(t *testing.T) {

return pusher
}(stdout.Options{
File: out,
Writer: out,
DoNotPrintTime: true,
})

Expand Down
10 changes: 5 additions & 5 deletions exporter/metric/stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/metric/stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion exporter/trace/stdout/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
8 changes: 3 additions & 5 deletions exporter/trace/stdout/stdout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down