Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Nov 6, 2024
1 parent 00fe7ee commit 14c43d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ddtrace/tracer/rules_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,23 +510,39 @@ func (rs *traceRulesSampler) sampleRules(span *span) bool {
}

func (rs *traceRulesSampler) applyRate(span *span, rate float64, now time.Time, sampler samplernames.SamplerName) {
fmt.Printf("[DEBUG] knuthFactor: %d\n", knuthFactor)
fmt.Printf("[DEBUG] Entering applyRate with rate: %f for span: %s, TraceID: %d\n", rate, span.Name, span.TraceID)
span.Lock()
defer span.Unlock()

span.setMetric(keyRulesSamplerAppliedRate, rate)
delete(span.Metrics, keySamplingPriorityRate)
if !sampledByRate(span.TraceID, rate) {

// Calculate sampling decision
samplingCalc := span.TraceID * knuthFactor
threshold := uint64(rate * math.MaxUint64)
shouldSample := samplingCalc < threshold

fmt.Printf("[DEBUG] Sampling calculation: TraceID=%d, knuthFactor=%d, calc=%d, threshold=%d, shouldSample=%v\n",
span.TraceID, knuthFactor, samplingCalc, threshold, shouldSample)

if !shouldSample {
fmt.Printf("[DEBUG] Rejecting trace due to sampling rate\n")
span.setSamplingPriorityLocked(ext.PriorityUserReject, sampler)
return
}

sampled, rate := rs.limiter.allowOne(now)
sampled, limitRate := rs.limiter.allowOne(now)
fmt.Printf("[DEBUG] Rate limiter decision: sampled=%v, rate=%f\n", sampled, limitRate)

if sampled {
fmt.Printf("[DEBUG] Keeping trace: passed both sampling and rate limiting\n")
span.setSamplingPriorityLocked(ext.PriorityUserKeep, sampler)
} else {
fmt.Printf("[DEBUG] Rejecting trace: failed rate limiting\n")
span.setSamplingPriorityLocked(ext.PriorityUserReject, sampler)
}
span.setMetric(keyRulesSamplerLimiterRate, rate)
span.setMetric(keyRulesSamplerLimiterRate, limitRate)
}

// limit returns the rate limit set in the rules sampler, controlled by DD_TRACE_RATE_LIMIT, and
Expand Down
2 changes: 2 additions & 0 deletions ddtrace/tracer/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tracer

import (
"encoding/json"
"fmt"
"io"
"math"
"sync"
Expand Down Expand Up @@ -84,6 +85,7 @@ func (r *rateSampler) Sample(spn ddtrace.Span) bool {
// sampledByRate verifies if the number n should be sampled at the specified
// rate.
func sampledByRate(n uint64, rate float64) bool {
fmt.Printf("[DEBUG] Rate: %f", rate)
if rate < 1 {
return n*knuthFactor < uint64(rate*math.MaxUint64)
}
Expand Down

0 comments on commit 14c43d4

Please sign in to comment.