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

ddtrace/tracer: Ensure SpanLink access is safe from corruption #2975

Merged
merged 3 commits into from
Nov 14, 2024
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: 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should an assertion be made here to make sure that the casting doesn't fail? Otherwise what is the point of this declaration? 🤔

Copy link
Contributor Author

@mtoffl01 mtoffl01 Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code won't compile if this fails; this is a common code pattern for "checking that a concrete type fulfills an interface" in Go.
https://dev.to/kittipat1413/checking-if-a-type-satisfies-an-interface-in-go-432n#:~:text=In%20Go%2C%20to%20check%20if,now%2C%20not%20at%20runtime.%E2%80%9D

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
Loading