Skip to content

Commit

Permalink
add exclude target for the sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
bmansoob committed Sep 11, 2024
1 parent fdafe07 commit 0e3dac0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/app_profiler/sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "app_profiler/sampler/config"
module AppProfiler
module Sampler
@exclude_targets_result = {}
class << self
def profile_params(request, config)
profile_params_for(request.path, config)
Expand All @@ -18,6 +19,12 @@ def profile_params_for(target, config)

def sample?(config, target)
return false if Kernel.rand > config.sample_rate
return false if @exclude_targets_result[target]

if config.exclude_targets.any? { |t| target.match?(t) }
@exclude_targets_result[target] = true
return false
end

return false unless config.targets.any? { |t| target.match?(t) }

Expand Down
6 changes: 4 additions & 2 deletions lib/app_profiler/sampler/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module AppProfiler
module Sampler
class Config
attr_reader :sample_rate, :targets, :cpu_interval, :backends_probability
attr_reader :sample_rate, :targets, :exclude_targets, :cpu_interval, :backends_probability

SAMPLE_RATE = 0.001 # 0.1%
TARGETS = ["/"]
Expand All @@ -18,7 +18,8 @@ def initialize(sample_rate: SAMPLE_RATE,
backends_config: {
stackprof: StackprofConfig.new,
},
paths: nil)
paths: nil,
exclude_targets: [])

if sample_rate < 0.0 || sample_rate > 1.0
raise ArgumentError, "sample_rate must be between 0 and 1"
Expand All @@ -32,6 +33,7 @@ def initialize(sample_rate: SAMPLE_RATE,
@targets = paths || targets
@backends_config = backends_config
@backends_probability = backends_probability
@exclude_targets = exclude_targets
end

def get_backend_config(backend_name)
Expand Down
13 changes: 13 additions & 0 deletions test/app_profiler/sampler/sampler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class SamplerTest < TestCase
assert_nil(Sampler.profile_params(request, config))
end


test "exclude_targets are respected" do
Kernel.stubs(:rand).returns(0.1)
config = Config.new(
sample_rate: 1.0,
targets: ["/foo"],
exclude_targets: ["/foo/bar"],
)

request = RequestParameters.new(Rack::Request.new({ "PATH_INFO" => "/foo/bar" }))
assert_nil(Sampler.profile_params(request, config))
end

test "mixed backend probabilities" do
skip("Vernier not supported") unless AppProfiler.vernier_supported?

Expand Down

0 comments on commit 0e3dac0

Please sign in to comment.