From 8d3ae4c5ede1a4c6ceb769f43cc759fdbf3d28f8 Mon Sep 17 00:00:00 2001 From: Leo Xie Date: Wed, 31 Jan 2024 01:19:23 +0800 Subject: [PATCH] fix: Fix stdouttrace/example_test to make the trace_id same. (#4855) * fix: Fix stdouttrace/example_test to make the trace_id same. * fix: restore Example --------- Co-authored-by: Damien Mathieu --- exporters/stdout/stdouttrace/example_test.go | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/exporters/stdout/stdouttrace/example_test.go b/exporters/stdout/stdouttrace/example_test.go index fd5425c830e..33164852343 100644 --- a/exporters/stdout/stdouttrace/example_test.go +++ b/exporters/stdout/stdouttrace/example_test.go @@ -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 { @@ -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) @@ -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) } @@ -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) }