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

TT-8809 ContextWithSpan #22

Merged
merged 2 commits into from
Jul 31, 2023
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
5 changes: 5 additions & 0 deletions trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func SpanFromContext(ctx context.Context) Span {
return trace.SpanFromContext(ctx)
}

// ContextWithSpan returns a child context from ctx with span embedded
func ContextWithSpan(ctx context.Context, span Span) context.Context {
return trace.ContextWithSpan(ctx, span)
}

// NewSpanFromContext creates a new span from the given context.
// If the context already has a span attached to it, the new span will be a child of the existing span.
// If the context does not have a span attached to it, the new span will be a root span.
Expand Down
15 changes: 15 additions & 0 deletions trace/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func TestSpanFromContext(t *testing.T) {
}
}

func TestContextWithSpan(t *testing.T) {
provider, err := NewProvider(
WithContext(context.Background()),
WithConfig(&config.OpenTelemetry{Enabled: true}),
)
assert.NoError(t, err)

_, sampleSpan := provider.Tracer().Start(context.Background(), "sample span")

ctx := ContextWithSpan(context.Background(), sampleSpan)
if got := SpanFromContext(ctx); got != sampleSpan {
t.Errorf("ContextWithSpan() = %v, want %v", got, sampleSpan)
}
}

func TestNewSpanFromContext(t *testing.T) {
assert := assert.New(t)

Expand Down
Loading