Skip to content
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
6 changes: 6 additions & 0 deletions ddtrace/tracer/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import (
"github.com/DataDog/dd-trace-go/v2/instrumentation/options"
"github.com/DataDog/dd-trace-go/v2/internal"
illmobs "github.com/DataDog/dd-trace-go/v2/internal/llmobs"
"github.com/DataDog/dd-trace-go/v2/internal/log"
"github.com/DataDog/dd-trace-go/v2/internal/orchestrion"
)

// ContextWithSpan returns a copy of the given context which includes the span s.
// If ctx is nil, a new background context is created to avoid panicking.
func ContextWithSpan(ctx context.Context, s *Span) context.Context {
if ctx == nil {
log.Warn("ContextWithSpan: received nil context, falling back to context.Background()")
ctx = context.Background()
}
Comment thread
genesor marked this conversation as resolved.
newCtx := orchestrion.CtxWithValue(ctx, internal.ActiveSpanKey, s)
Comment thread
genesor marked this conversation as resolved.
return contextWithPropagatedLLMSpan(newCtx, s)
}
Expand Down
22 changes: 17 additions & 5 deletions ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ import (
)

func TestContextWithSpan(t *testing.T) {
want := &Span{spanID: 123}
ctx := ContextWithSpan(context.Background(), want)
got := ctx.Value(internal.ActiveSpanKey)
assert := assert.New(t)
assert.Equal(got, want)
t.Run("OK", func(t *testing.T) {
want := &Span{spanID: 123}
ctx := ContextWithSpan(context.Background(), want)
got := ctx.Value(internal.ActiveSpanKey)
assert := assert.New(t)
assert.Equal(got, want)
})

t.Run("nil context", func(t *testing.T) {
assert.NotPanics(t, func() {
want := &Span{spanID: 123}
ctx := ContextWithSpan(nil, want)
got := ctx.Value(internal.ActiveSpanKey)
assert := assert.New(t)
assert.Equal(got, want)
})
})
}

func TestSpanFromContext(t *testing.T) {
Expand Down
Loading