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: better trace and span ID randomness #1472

Merged
merged 2 commits into from
Sep 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ddtrace/tracer/sqlcomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type SQLCommentCarrier struct {

// Inject injects a span context in the carrier's Query field as a comment.
func (c *SQLCommentCarrier) Inject(spanCtx ddtrace.SpanContext) error {
c.SpanID = random.Uint64()
c.SpanID = generateSpanID(now())
tags := make(map[string]string)
switch c.Mode {
case SQLInjectionUndefined:
Expand Down
9 changes: 8 additions & 1 deletion ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
}
id := opts.SpanID
if id == 0 {
id = random.Uint64()
id = generateSpanID(startTime)
}
// span defaults
span := &span{
Expand Down Expand Up @@ -498,6 +498,13 @@ func (t *tracer) StartSpan(operationName string, options ...ddtrace.StartSpanOpt
return span
}

// generateSpanID returns a random uint64 that has been XORd with the startTime.
// This is done to get around the 32-bit random seed limitation that may create collisions if there is a large number
// of go services all generating spans.
func generateSpanID(startTime int64) uint64 {
return random.Uint64() ^ uint64(startTime)
}

// applyPPROFLabels applies pprof labels for the profiler's code hotspots and
// endpoint filtering feature to span. When span finishes, any pprof labels
// found in ctx are restored.
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (t *tracer) newChildSpan(name string, parent *span) *span {

var (
// timeMultiplicator specifies by how long to extend waiting times.
// It may be altered in some envinronment (like AppSec) where things
// It may be altered in some environments (like AppSec) where things
// move slower and could otherwise create flaky tests.
timeMultiplicator = time.Duration(1)

Expand Down