Skip to content

Commit

Permalink
ddtrace/tracer: allow changes of priority even when the root is non-l…
Browse files Browse the repository at this point in the history
…ocal (#1241)

The tracer was not allowing the user to override the sampling decision
in downstream services, causing a manual keep to not work for traces
where the root span wasn't local.

Co-authored-by: Gabriel Aszalos <gabriel.aszalos@gmail.com>
  • Loading branch information
dumontg and gbbr committed Apr 28, 2022
1 parent 4edd4a8 commit 11df452
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
29 changes: 29 additions & 0 deletions ddtrace/tracer/span_test.go
Expand Up @@ -8,13 +8,15 @@ package tracer
import (
"errors"
"fmt"
"math"
"os"
"strings"
"sync/atomic"
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/internal/samplernames"

"github.com/DataDog/datadog-agent/pkg/obfuscate"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -348,6 +350,33 @@ func TestSpanSetTagError(t *testing.T) {
})
}

func TestTraceManualKeepAndManualDrop(t *testing.T) {
for _, scenario := range []struct {
tag string
keep bool
p int // priority
}{
{ext.ManualKeep, true, 0},
{ext.ManualDrop, false, 1},
} {
t.Run(fmt.Sprintf("%s/local", scenario.tag), func(t *testing.T) {
tracer := newTracer()
span := tracer.newRootSpan("root span", "my service", "my resource")
span.SetTag(scenario.tag, true)
assert.Equal(t, scenario.keep, shouldKeep(span))
})

t.Run(fmt.Sprintf("%s/non-local", scenario.tag), func(t *testing.T) {
tracer := newTracer()
spanCtx := &spanContext{traceID: 42, spanID: 42}
spanCtx.setSamplingPriority("", scenario.p, samplernames.Upstream, math.NaN())
span := tracer.StartSpan("non-local root span", ChildOf(spanCtx)).(*span)
span.SetTag(scenario.tag, true)
assert.Equal(t, scenario.keep, shouldKeep(span))
})
}
}

func TestSpanSetDatadogTags(t *testing.T) {
assert := assert.New(t)

Expand Down
5 changes: 0 additions & 5 deletions ddtrace/tracer/spancontext.go
Expand Up @@ -221,11 +221,6 @@ func (t *trace) setSamplingPriorityLocked(service string, p int, sampler sampler
if t.locked {
return
}
if t.root == nil {
// this trace is distributed (no local root); modifications
// to the sampling priority are not allowed.
t.locked = true
}
if t.priority == nil {
t.priority = new(float64)
}
Expand Down
15 changes: 0 additions & 15 deletions ddtrace/tracer/spancontext_test.go
Expand Up @@ -242,21 +242,6 @@ func TestSpanFinishPriority(t *testing.T) {
assert.Fail("span not found")
}

func TestTracePriorityLocked(t *testing.T) {
assert := assert.New(t)
ddHeaders := TextMapCarrier(map[string]string{
DefaultTraceIDHeader: "2",
DefaultParentIDHeader: "2",
DefaultPriorityHeader: "2",
})

ctx, err := NewPropagator(nil).Extract(ddHeaders)
assert.Nil(err)
sctx, ok := ctx.(*spanContext)
assert.True(ok)
assert.True(sctx.trace.locked)
}

func TestNewSpanContext(t *testing.T) {
t.Run("basic", func(t *testing.T) {
span := &span{
Expand Down

0 comments on commit 11df452

Please sign in to comment.