Skip to content

Commit

Permalink
initial ignore logic flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Hartweg committed Jan 3, 2025
1 parent ffadd8b commit d610eb0
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions lib/scout_apm/sampling.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
module ScoutApm
class Sampling
attr_reader :global_sample_rate, :sample_endpoints, :sample_uri_regex, :sample_jobs, :ignore_uri_regex, :ignore_jobs

def initialize(config)
@global_sample_rate = config.value('sample_rate')
@sampled_endpoints = individual_sample_to_hash(config.value('sampled_endpoints'))
@sampled_jobs = individual_sample_to_hash(config.value('sampled_jobs'))
@ignored_endpoints = config.value('ignored_endpoints').split(',')
@ignored_jobs = config.value('ignored_jobs').split(',')
# 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(',')
# TODO make this safer/smarter
end

def ignored?(transaction)
# a bunch of logic to determine if a transaction should be ignored
return
# global sample check
if global_sample_rate
return true if sample?(global_sample_rate)
end

# job or endpoint?
# check 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]])
end
end

false
end

private
Expand All @@ -27,5 +50,29 @@ def individual_sample_to_hash(sampling_config)
end
sample_hash
end

def create_uri_regex(prefixes)
regexes = Array(prefixes).
reject{|prefix| prefix == ""}.
map {|prefix| %r{\A#{prefix}} }
Regexp.union(*regexes)
end

def ignore_uri?(uri)
!! ignore_uri_regex.match(uri)
end

def sample_uri?(uri)
!! sample_uri_regex.match(uri)
end

def ignore_job?(job_name)
@ignored_jobs.include?(job_name)
end

def sample?(rate)
rand * 100 > rate
end

end
end

0 comments on commit d610eb0

Please sign in to comment.