Skip to content

Commit

Permalink
ddtrace/tracer: update sample rate configuration parsing to avoid log…
Browse files Browse the repository at this point in the history
…ging a warning when empty (#2751)
  • Loading branch information
zacharycmontoya authored and eliottness committed Jun 19, 2024
1 parent c7dfa2a commit d5a0169
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 12 additions & 0 deletions ddtrace/tracer/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ func TestLogSamplingRules(t *testing.T) {
assert.Regexp(logPrefixRegexp+` WARN: DIAGNOSTICS Error\(s\) parsing sampling rules: found errors:\n\tat index 4: ignoring rule {Rate:9\.10}: rate is out of \[0\.0, 1\.0] range$`, tp.Logs()[0])
}

func TestLogDefaultSampleRate(t *testing.T) {
assert := assert.New(t)
tp := new(log.RecordLogger)
tp.Ignore("appsec: ", telemetry.LogPrefix)
log.UseLogger(tp)
t.Setenv("DD_TRACE_SAMPLE_RATE", ``)
_, _, _, stop := startTestTracer(t, WithLogger(tp))
defer stop()

assert.Len(tp.Logs(), 0)
}

func TestLogAgentReachable(t *testing.T) {
assert := assert.New(t)
tp := new(log.RecordLogger)
Expand Down
20 changes: 12 additions & 8 deletions ddtrace/tracer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,19 @@ const partialFlushMinSpansDefault = 1000
func newConfig(opts ...StartOption) *config {
c := new(config)
c.sampler = NewAllSampler()
defaultRate, err := strconv.ParseFloat(getDDorOtelConfig("sampleRate"), 64)
if err != nil {
log.Warn("ignoring DD_TRACE_SAMPLE_RATE, error: %v", err)
defaultRate = math.NaN()
} else if defaultRate < 0.0 || defaultRate > 1.0 {
log.Warn("ignoring DD_TRACE_SAMPLE_RATE: out of range %f", defaultRate)
defaultRate = math.NaN()
sampleRate := math.NaN()
if r := getDDorOtelConfig("sampleRate"); r != "" {
var err error
sampleRate, err = strconv.ParseFloat(r, 64)
if err != nil {
log.Warn("ignoring DD_TRACE_SAMPLE_RATE, error: %v", err)
sampleRate = math.NaN()
} else if sampleRate < 0.0 || sampleRate > 1.0 {
log.Warn("ignoring DD_TRACE_SAMPLE_RATE: out of range %f", sampleRate)
sampleRate = math.NaN()
}
}
c.globalSampleRate = defaultRate
c.globalSampleRate = sampleRate
c.httpClientTimeout = time.Second * 10 // 10 seconds

if v := os.Getenv("OTEL_LOGS_EXPORTER"); v != "" {
Expand Down

0 comments on commit d5a0169

Please sign in to comment.