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 12, 2024
1 parent fdafe07 commit 387b01b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ All the configuration parameters are optional and have default values. The defau
| -------- | ------- |
| sample_rate (0.0 - 1.0) | 0.001 (0.1 %) |
| targets (request paths, job_names etc ) | ['/'] |
| exclude_targets (request paths, job_names etc ) | ['/ping'] |
| stackprof_probability | 1.0 |
| vernier_probability | 0.0 |

Expand Down
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
@excluded_cache = {}
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 @excluded_cache[target]

if config.exclude_targets.any? { |t| target.match?(t) }
@excluded_cache[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
12 changes: 12 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,18 @@ 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 387b01b

Please sign in to comment.