Skip to content

Commit

Permalink
added context with span
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Jul 31, 2023
1 parent 7d5b6dc commit 79b602f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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
14 changes: 14 additions & 0 deletions trace/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ 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")

Check failure on line 60 in trace/span_test.go

View workflow job for this annotation

GitHub Actions / lint

assignments should only be cuddled with other assignments (wsl)

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

0 comments on commit 79b602f

Please sign in to comment.