Skip to content

Commit 45b37fd

Browse files
authored
SampleRateCoercion preserves vil values or empty strings (#601)
1 parent 8234fb5 commit 45b37fd

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/scout_apm/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def coerce(val)
213213

214214
class SampleRateCoercion
215215
def coerce(val)
216+
return nil if val.nil?
217+
return nil if val.to_s.strip.empty?
216218
v = val.to_f
217219
# Anything above 1 is assumed a percentage for backwards compat, so convert to a decimal
218220
if v > 1

test/unit/config_test.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,24 @@ def test_nullable_integer_coercion
9494

9595
def test_sample_rate_coercion
9696
coercion = ScoutApm::Config::SampleRateCoercion.new
97+
# Preserves nil and empty strings
98+
assert_nil coercion.coerce(nil)
99+
assert_nil coercion.coerce("")
100+
assert_nil coercion.coerce(" ")
101+
# Float strings work
97102
assert_in_delta 1.0, coercion.coerce("1")
98103
assert_in_delta 0.015, coercion.coerce("1.5")
104+
assert_in_delta 0.5, coercion.coerce("0.5")
105+
assert_in_delta 0.01, coercion.coerce("0.01")
106+
# Numbers work
99107
assert_in_delta 1.0, coercion.coerce(1)
100108
assert_in_delta 0.015, coercion.coerce(1.5)
101109
assert_in_delta 0.0, coercion.coerce("0")
102110
assert_in_delta 0.0, coercion.coerce(0)
103-
assert_in_delta 0.0, coercion.coerce("")
104-
assert_in_delta 0.0, coercion.coerce(nil)
105-
assert_in_delta 0.5, coercion.coerce("0.5")
111+
# Backwards compatibility: values > 1 treated as percentages
112+
assert_in_delta 0.15, coercion.coerce("15")
113+
assert_in_delta 0.15, coercion.coerce(15)
114+
# Clamping
106115
assert_in_delta 0, coercion.coerce("-2.5")
107116
end
108117

@@ -150,4 +159,18 @@ def test_job_sample_rate_backwards_compat_with_percentage
150159
ensure
151160
ENV.delete('SCOUT_JOB_SAMPLE_RATE')
152161
end
162+
163+
def test_endpoint_sample_rate_nil_when_not_set
164+
# Nil allows sampling to fall through to global sample_rate
165+
ENV.delete('SCOUT_ENDPOINT_SAMPLE_RATE')
166+
conf = ScoutApm::Config.without_file(@context)
167+
assert_nil conf.value('endpoint_sample_rate')
168+
end
169+
170+
def test_job_sample_rate_nil_when_not_set
171+
# Nil allows sampling to fall through to global sample_rate
172+
ENV.delete('SCOUT_JOB_SAMPLE_RATE')
173+
conf = ScoutApm::Config.without_file(@context)
174+
assert_nil conf.value('job_sample_rate')
175+
end
153176
end

0 commit comments

Comments
 (0)