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

fix: Fix stdouttrace/example_test to make the trace_id same. #4855

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions exporters/stdout/stdouttrace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
"context"
"fmt"
"log"
"testing"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
StLeoX marked this conversation as resolved.
Show resolved Hide resolved
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -38,20 +39,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 +63,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 @@ -77,11 +78,11 @@ func InstallExportPipeline(ctx context.Context) (func(context.Context) error, er
return tracerProvider.Shutdown, nil
}

func Example() {
func TestExample(t *testing.T) {
StLeoX marked this conversation as resolved.
Show resolved Hide resolved
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 +92,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)
StLeoX marked this conversation as resolved.
Show resolved Hide resolved
log.Println("the answer is", ans)
}