From 93ee25ec295d488b1af7783086b795a0642ceec9 Mon Sep 17 00:00:00 2001 From: Diana Shevchenko <40775148+dianashevchenko@users.noreply.github.com> Date: Wed, 8 May 2024 16:24:33 +0200 Subject: [PATCH] ddtrace/tracer: remove agent_psr when rule_psr is set (#2668) --- ddtrace/tracer/rules_sampler.go | 4 +++- ddtrace/tracer/sampler_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ddtrace/tracer/rules_sampler.go b/ddtrace/tracer/rules_sampler.go index 3deaf845ec..b37ab2ea29 100644 --- a/ddtrace/tracer/rules_sampler.go +++ b/ddtrace/tracer/rules_sampler.go @@ -518,7 +518,8 @@ func (rs *traceRulesSampler) sampleRules(span *span) bool { } func (rs *traceRulesSampler) applyRate(span *span, rate float64, now time.Time, sampler samplernames.SamplerName) { - span.SetTag(keyRulesSamplerAppliedRate, rate) + span.setMetric(keyRulesSamplerAppliedRate, rate) + delete(span.Metrics, keySamplingPriorityRate) if !sampledByRate(span.TraceID, rate) { span.setSamplingPriority(ext.PriorityUserReject, sampler) return @@ -614,6 +615,7 @@ func (rs *singleSpanRulesSampler) apply(span *span) bool { return false } } + delete(span.Metrics, keySamplingPriorityRate) span.setMetric(keySpanSamplingMechanism, float64(samplernames.SingleSpan)) span.setMetric(keySingleSpanSamplingRuleRate, rate) if rule.MaxPerSecond != 0 { diff --git a/ddtrace/tracer/sampler_test.go b/ddtrace/tracer/sampler_test.go index 53ae901de9..89c1810836 100644 --- a/ddtrace/tracer/sampler_test.go +++ b/ddtrace/tracer/sampler_test.go @@ -1065,6 +1065,19 @@ func TestRulesSampler(t *testing.T) { s.Finish() assert.EqualValues(t, s.(*span).Metrics[keySamplingPriority], 2) }) + + t.Run("no-agent_psr-with-rules-sampling", func(t *testing.T) { + t.Setenv("DD_TRACE_SAMPLING_RULES", `[{"resource": "keep_me", "sample_rate": 0}]`) + _, _, _, stop := startTestTracer(t) + defer stop() + + s, _ := StartSpanFromContext(context.Background(), "whatever") + s.SetTag(ext.ResourceName, "keep_me") + s.Finish() + span := s.(*span) + assert.NotContains(t, span.Metrics, keySamplingPriorityRate) + assert.Contains(t, span.Metrics, keyRulesSamplerAppliedRate) + }) } func TestSamplingRuleUnmarshal(t *testing.T) {