Skip to content

Commit

Permalink
fix: Fix stdouttrace/example_test to make the trace_id same. (#4855)
Browse files Browse the repository at this point in the history
* fix: Fix stdouttrace/example_test to make the trace_id same.

* fix: restore Example

---------

Co-authored-by: Damien Mathieu <damien.mathieu@elastic.co>
  • Loading branch information
StLeoX and dmathieu committed Jan 30, 2024
1 parent e7de571 commit 8d3ae4c
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions exporters/stdout/stdouttrace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ var tracer = otel.GetTracerProvider().Tracer(
trace.WithSchemaURL(semconv.SchemaURL),
)

func add(ctx context.Context, x, y int64) int64 {
func add(ctx context.Context, x, y int64) (context.Context, int64) {
var span trace.Span
_, span = tracer.Start(ctx, "Addition")
ctx, span = tracer.Start(ctx, "Addition")
defer span.End()

return x + y
return ctx, x + y
}

func multiply(ctx context.Context, x, y int64) int64 {
func multiply(ctx context.Context, x, y int64) (context.Context, int64) {
var span trace.Span
_, span = tracer.Start(ctx, "Multiplication")
ctx, span = tracer.Start(ctx, "Multiplication")
defer span.End()

return x * y
return ctx, x * y
}

func Resource() *resource.Resource {
Expand All @@ -62,7 +62,7 @@ func Resource() *resource.Resource {
)
}

func InstallExportPipeline(ctx context.Context) (func(context.Context) error, error) {
func InstallExportPipeline() (func(context.Context) error, error) {
exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
return nil, fmt.Errorf("creating stdout exporter: %w", err)
Expand All @@ -81,7 +81,7 @@ func Example() {
ctx := context.Background()

// Registers a tracer Provider globally.
shutdown, err := InstallExportPipeline(ctx)
shutdown, err := InstallExportPipeline()
if err != nil {
log.Fatal(err)
}
Expand All @@ -91,5 +91,8 @@ func Example() {
}
}()

log.Println("the answer is", add(ctx, multiply(ctx, multiply(ctx, 2, 2), 10), 2))
ctx, ans := multiply(ctx, 2, 2)
ctx, ans = multiply(ctx, ans, 10)
ctx, ans = add(ctx, ans, 2)
log.Println("the answer is", ans)
}

0 comments on commit 8d3ae4c

Please sign in to comment.