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: fixed resampling to occur on root span only #2824

Merged
merged 2 commits into from
Aug 22, 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
84 changes: 84 additions & 0 deletions ddtrace/tracer/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1637,3 +1637,87 @@ func TestSetGlobalSampleRate(t *testing.T) {
assert.Equal(t, 0.0, rs.globalRate)
assert.False(t, b)
}

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

t.Run("no-ctx-propagation", func(t *testing.T) {
Start(WithSamplingRules([]SamplingRule{
TagsResourceRule(map[string]string{"tag": "20"}, "", "", "", 1),
TagsResourceRule(nil, "root", "", "", 0),
}))
tr := internal.GetGlobalTracer()
defer tr.Stop()

root := tr.StartSpan("mysql.root", ResourceName("root"))
child := tr.StartSpan("mysql.child", ChildOf(root.Context()))
child.SetTag("tag", 20)

// root span should be sampled with the second rule
// sampling decision is 0, thus "_dd.limit_psr" is not present
assert.Contains(root.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.Equal(0., root.(*span).Metrics[keyRulesSamplerAppliedRate])
assert.NotContains(root.(*span).Metrics, keyRulesSamplerLimiterRate)

// neither"_dd.limit_psr", nor "_dd.rule_psr" should be present
// on the child span
assert.NotContains(child.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(child.(*span).Metrics, keyRulesSamplerLimiterRate)

// setting this tag would change the result of sampling,
// which will occur after the span is finished
root.SetTag("tag", 20)
child.Finish()

// first sampling rule is applied, the sampling decision is 1
// and the "_dd.limit_psr" is present
root.Finish()
assert.Equal(1., root.(*span).Metrics[keyRulesSamplerAppliedRate])
assert.Contains(root.(*span).Metrics, keyRulesSamplerLimiterRate)

// neither"_dd.limit_psr", nor "_dd.rule_psr" should be present
// on the child span
assert.NotContains(child.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(child.(*span).Metrics, keyRulesSamplerLimiterRate)
})

t.Run("with-ctx-propagation", func(t *testing.T) {
Start(WithSamplingRules([]SamplingRule{
TagsResourceRule(map[string]string{"tag": "20"}, "", "", "", 1),
TagsResourceRule(nil, "root", "", "", 0),
}))
tr := internal.GetGlobalTracer()
defer tr.Stop()

root := tr.StartSpan("mysql.root", ResourceName("root"))
child := tr.StartSpan("mysql.child", ChildOf(root.Context()))
child.SetTag("tag", 20)

// root span should be sampled with the second rule
// sampling decision is 0, thus "_dd.limit_psr" is not present
assert.Equal(0., root.(*span).Metrics[keyRulesSamplerAppliedRate])
assert.Contains(root.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(root.(*span).Metrics, keyRulesSamplerLimiterRate)

// neither"_dd.limit_psr", nor "_dd.rule_psr" should be present
// on the child span
assert.NotContains(child.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(child.(*span).Metrics, keyRulesSamplerLimiterRate)

// context propagation locks the span, so no re-sampling should occur
tr.Inject(root.Context(), TextMapCarrier(map[string]string{}))
root.SetTag("tag", 20)

child.Finish()

// re-sampling should not occur
root.Finish()
assert.NotContains(child.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(root.(*span).Metrics, keyRulesSamplerLimiterRate)

// neither"_dd.limit_psr", nor "_dd.rule_psr" should be present
// on the child span
assert.NotContains(child.(*span).Metrics, keyRulesSamplerAppliedRate)
assert.NotContains(child.(*span).Metrics, keyRulesSamplerLimiterRate)
})
}
8 changes: 5 additions & 3 deletions ddtrace/tracer/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,11 @@ func (s *span) Finish(opts ...ddtrace.FinishOption) {
s.SetTag("go_execution_traced", "partial")
}

if tr, ok := internal.GetGlobalTracer().(*tracer); ok && tr.rulesSampling.traces.enabled() {
if !s.context.trace.isLocked() && s.context.trace.propagatingTag(keyDecisionMaker) != "-4" {
tr.rulesSampling.SampleTrace(s)
if s.root() == s {
if tr, ok := internal.GetGlobalTracer().(*tracer); ok && tr.rulesSampling.traces.enabled() {
if !s.context.trace.isLocked() && s.context.trace.propagatingTag(keyDecisionMaker) != "-4" {
tr.rulesSampling.SampleTrace(s)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,9 @@ func (t *tracer) sample(span *span) {
if t.rulesSampling.SampleTraceGlobalRate(span) {
return
}
if t.rulesSampling.SampleTrace(span) {
return
}
t.prioritySampling.apply(span)
}

Expand Down
Loading