Skip to content

Commit

Permalink
initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Hartweg committed Jan 3, 2025
1 parent 27849c7 commit 2ba429b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/scout_apm/sampling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ def initialize(config)
# web endpoints matched prefix by regex
# jobs matched explicitly by name
@sample_endpoints = individual_sample_to_hash(config.value('sample_endpoints'))
@sample_uri_regex = create_uri_regex(sample_endpoints.keys)
@sample_jobs = individual_sample_to_hash(config.value('sample_jobs'))
@ignore_uri_regex = create_uri_regex(config.value('ignore_endpoints'))
@ignore_jobs = config.value('ignore_jobs').split(',')
@sample_uri_regex = create_uri_regex(sample_endpoints.keys) if sample_endpoints
@ignore_jobs = config.value('ignore_jobs').split(',') if config.value('ignore_jobs')
@sample_jobs = individual_sample_to_hash(config.value('sample_jobs'))
# TODO make this safer/smarter
end

def ignored?(transaction)
def ignore?(transaction)
# global sample check
if global_sample_rate
return true if sample?(global_sample_rate)
end

# job or endpoint?
# check ignored _then_ sampled
# check if ignored _then_ sampled
if transaction.job?
job_name = transaction.layer_finder.job.name
return true if ignore_job?(transaction.job_name)
if sample_jobs.has_key?(transaction.job_name)
return true if sample?(sample_jobs[transaction.job_name])
end
elsif transaction.web?
return true if ignore_uri?(transaction.annotations[:uri])
if sample_uri?(transaction.annotations[:uri])
return true if sample?(sample_endpoints[transaction.annotations[:uri]])
uri = transaction.annotations[:uri]
return true if ignore_uri?(uri)
if sample_uri?(uri)
return true if sample?(uri)
end
end

false
false # not ignored
end

private

def individual_sample_to_hash(sampling_config)
return nil if sampling_config.nil?
# config looks like ['/foo:50','/bar:100']. parse it into hash of string: integer
sample_hash = {}
sampling_config.each do |sample|
Expand All @@ -52,6 +52,7 @@ def individual_sample_to_hash(sampling_config)
end

def create_uri_regex(prefixes)
return nil if prefixes.nil?
regexes = Array(prefixes).
reject{|prefix| prefix == ""}.
map {|prefix| %r{\A#{prefix}} }
Expand All @@ -67,9 +68,15 @@ def sample_uri?(uri)
end

def ignore_job?(job_name)
return false if ignore_jobs.nil?
@ignored_jobs.include?(job_name)
end

def sample_job?(job_name)
return false if sample_jobs.nil?
@sample_jobs.has_key?(job_name)
end

def sample?(rate)
rand * 100 > rate
end
Expand Down
63 changes: 63 additions & 0 deletions test/unit/sampling_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'test_helper'

require 'scout_apm/sampling'

class SamplingTest < Minitest::Test
# tr = ScoutApm::TrackedRequest.new(ScoutApm::AgentContext.new, ScoutApm::FakeStore.new)

def setup
@global_sample_config = FakeConfigOverlay.new(
{
'sample_rate' => 50,
}
)

@individual_config = FakeConfigOverlay.new(
{
'sample_endpoints' => ['/foo:50', '/bar:100'],
'ignore_endpoints' => ['/baz'],
'sample_jobs' => ['joba:50'],
'ignore_jobs' => 'jobb,jobc',
}
)
end

def test_individual_sample_to_hash
sampling = ScoutApm::Sampling.new(@individual_config)
assert_equal({'/foo' => 50, '/bar' => 100}, sampling.individual_sample_to_hash(@individual_config.value('sample_endpoints')))
end

def test_uri_ignore
sampling = ScoutApm::Sampling.new(@individual_config)
assert_equal true, sampling.ignore_uri?('/baz')
assert_equal false, sampling.ignore_uri?('/foo')
end

def test_uri_sample
sampling = ScoutApm::Sampling.new(@individual_config)
assert_equal true, sampling.sample_uri?('/foo')
assert_equal false, sampling.sample_uri?('/baz')
end

def test_job_ignore
sampling = ScoutApm::Sampling.new(@individual_config)
assert_equal true, sampling.ignore_job?('jobb')
assert_equal false, sampling.ignore_job?('joba')
end

def test_job_sample
sampling = ScoutApm::Sampling.new(@individual_config)
assert_equal true, sampling.sample_job?('joba')
assert_equal false, sampling.sample_job?('jobb')
end

def test_sample
sampling = ScoutApm::Sampling.new(@global_sample_config)
sampling.stub(:rand, 1) do
assert_equal(false, sampling.sample?(50))
end
sampling.stub(:rand, 99) do
assert_equal(true, sampling.sample?(50))
end
end
end

0 comments on commit 2ba429b

Please sign in to comment.