Skip to content

Commit

Permalink
ddtrace/tracer: Ensure SpanLink access is safe from corruption (#2975)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoffl01 authored Nov 14, 2024
1 parent f28ac98 commit d7cc13a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
5 changes: 1 addition & 4 deletions ddtrace/ddtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ type SpanContextW3C interface {
type SpanContextWithLinks interface {
SpanContext

// SpanLinks returns the span links on the SpanContext.
// SpanLinks returns a copy of the span links on the SpanContext.
SpanLinks() []SpanLink

// Setlinks takes in a slice of SpanLinks and sets them to the SpanContext.
SetLinks(links []SpanLink)
}

// Tracer specifies an implementation of the Datadog tracer which allows starting
Expand Down
10 changes: 4 additions & 6 deletions ddtrace/tracer/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ func TestStartSpanWithSpanLinks(t *testing.T) {
_, _, _, stop := startTestTracer(t)
defer stop()
spanLink := ddtrace.SpanLink{TraceID: 789, TraceIDHigh: 0, SpanID: 789, Attributes: map[string]string{"reason": "terminated_context", "context_headers": "datadog"}, Flags: 0}
var ctx ddtrace.SpanContext
ctx = &spanContext{spanID: 789, traceID: traceIDFrom64Bits(789), spanLinks: []ddtrace.SpanLink{spanLink}}
ctx := &spanContext{spanID: 789, traceID: traceIDFrom64Bits(789), spanLinks: []ddtrace.SpanLink{spanLink}}

t.Run("spanContext with spanLinks satisfies SpanContextWithLinks interface", func(t *testing.T) {
ctxLink, ok := ctx.(ddtrace.SpanContextWithLinks)
assert.True(t, ok)
assert.Equal(t, len(ctxLink.SpanLinks()), 1)
assert.Equal(t, ctxLink.SpanLinks()[0], spanLink)
var _ ddtrace.SpanContextWithLinks = ctx
assert.Equal(t, len(ctx.SpanLinks()), 1)
assert.Equal(t, ctx.SpanLinks()[0], spanLink)
})

t.Run("create span from spancontext with links", func(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ func (c *spanContext) TraceID128Bytes() [16]byte {
return c.traceID
}

// SpanLinks implements ddtrace.SpanContextWithLinks
func (c *spanContext) SpanLinks() []ddtrace.SpanLink {
return c.spanLinks
}

func (c *spanContext) SetLinks(links []ddtrace.SpanLink) {
c.spanLinks = links
cp := make([]ddtrace.SpanLink, len(c.spanLinks))
copy(cp, c.spanLinks)
return cp
}

// ForeachBaggageItem implements ddtrace.SpanContext.
Expand Down

0 comments on commit d7cc13a

Please sign in to comment.