Skip to content

Commit

Permalink
pkg/tracer: added single span sampling at trace finish
Browse files Browse the repository at this point in the history
  • Loading branch information
dianashevchenko committed Jun 29, 2022
1 parent 1f9b759 commit d056ab1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions ddtrace/tracer/single_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package tracer

import (
"math"
"time"

"golang.org/x/time/rate"

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

// singleSpanRulesSampler allows a user-defined list of rules to apply to spans
Expand Down Expand Up @@ -54,7 +56,7 @@ func (rs *singleSpanRulesSampler) apply(span *span) bool {
}

func (rs *singleSpanRulesSampler) applyRate(span *span, rule SamplingRule, rate float64, now time.Time) {
span.SetTag(keyRulesSamplerAppliedRate, rate)
span.setMetric(keyRulesSamplerAppliedRate, rate)
if !sampledByRate(span.SpanID, rate) {
span.setSamplingPriority(ext.PriorityUserReject, samplernames.RuleRate, rate)
return
Expand Down
3 changes: 3 additions & 0 deletions ddtrace/tracer/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ func (t *trace) finishedOne(s *span) {
if !ok {
return
}
for i := range t.spans {
tr.singleSpanRulesSampling.apply(t.spans[i])
}
// we have a tracer that can receive completed traces.
atomic.AddInt64(&tr.spansFinished, int64(len(t.spans)))
sd := samplingDecision(atomic.LoadInt64((*int64)(&t.samplingDecision)))
Expand Down
22 changes: 22 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,28 @@ func TestSamplingDecision(t *testing.T) {
assert.Equal(t, "", span.context.trace.tags[keyUpstreamServices])
assert.Equal(t, decisionDrop, span.context.trace.samplingDecision)
})
t.Run("client_dropped", func(t *testing.T) {
os.Setenv("DD_SPAN_SAMPLING_RULES", `[{"service": "test_*","name":"*_1", "sample_rate": 1.0, "max_per_second": 15.0}]`)
defer os.Unsetenv("DD_SPAN_SAMPLING_RULES")
tracer, _, _, stop := startTestTracer(t)
defer stop()
tracer.config.agent.DropP0s = true
tracer.config.sampler = NewRateSampler(0)
tracer.prioritySampling.defaultRate = 0
tracer.config.serviceName = "test_service"
span := tracer.StartSpan("name_1").(*span)
child := tracer.StartSpan("name_2", ChildOf(span.context))
child.Finish()
span.Finish()
assert.Equal(t, float64(ext.PriorityAutoReject), span.Metrics[keySamplingPriority])
// this trace won't be sent to the agent,
// therefore not necessary to populate keyUpstreamServices
assert.Equal(t, "", span.context.trace.tags[keyUpstreamServices])
assert.Equal(t, decisionDrop, span.context.trace.samplingDecision)
assert.Equal(t, 8.0, span.Metrics[ext.SpanSamplingMechanism])
assert.Equal(t, 1.0, span.Metrics[ext.SingleSpanSamplingRuleRate])
assert.Equal(t, 15.0, span.Metrics[ext.SingleSpanSamplingMPS])
})
}

func TestTracerRuntimeMetrics(t *testing.T) {
Expand Down

0 comments on commit d056ab1

Please sign in to comment.