Skip to content

Commit

Permalink
one enabled status
Browse files Browse the repository at this point in the history
  • Loading branch information
bmansoob committed Sep 5, 2024
1 parent e9e35aa commit 3e7665c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
22 changes: 20 additions & 2 deletions lib/app_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ module Viewer
mattr_reader :profile_enqueue_failure, default: nil
mattr_reader :after_process_queue, default: nil
mattr_accessor :forward_metadata_on_upload, default: false
mattr_accessor :profile_sampler_enabled, default: false
mattr_accessor :profile_sampler_killswitch_on, default: nil

mattr_accessor :profile_sampler_config

class << self
Expand Down Expand Up @@ -117,6 +116,25 @@ def backend=(new_backend)
@profiler_backend = new_profiler_backend
end

def profile_sampler_enabled=(value)
if value.is_a?(Proc)
raise ArgumentError,
"profile_sampler_enabled must be a proc or a lambda that accepts no argument" if value.arity != 0
else
raise ArgumentError, "Must be TrueClass or FalseClass" unless [TrueClass, FalseClass].include?(value.class)
end

@profile_sampler_enabled = value
end

def profile_sampler_enabled
return false unless defined?(@profile_sampler_enabled)

@profile_sampler_enabled.is_a?(Proc) ? @profile_sampler_enabled.call : @profile_sampler_enabled
rescue
false
end

def backend_for(backend_name)
if vernier_supported? &&
backend_name == AppProfiler::Backend::VernierBackend.name
Expand Down
2 changes: 0 additions & 2 deletions lib/app_profiler/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def profile(env, params)

def profile_params(params)
return params if params.valid?

return unless AppProfiler.profile_sampler_enabled
return if AppProfiler.profile_sampler_killswitch_on&.call

AppProfiler::Sampler.profile_params(params, AppProfiler.profile_sampler_config)
end
Expand Down
1 change: 0 additions & 1 deletion lib/app_profiler/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Railtie < Rails::Railtie
AppProfiler.backend = app.config.app_profiler.profiler_backend || :stackprof
AppProfiler.forward_metadata_on_upload = app.config.app_profiler.forward_metadata_on_upload || false
AppProfiler.profile_sampler_enabled = app.config.app_profiler.profile_sampler_enabled || false
AppProfiler.profile_sampler_killswitch_on = app.config.app_profiler.profile_sampler_killswitch_on || nil
AppProfiler.profile_sampler_config = app.config.app_profiler.profile_sampler_config ||
AppProfiler::Sampler::Config.new
end
Expand Down
8 changes: 3 additions & 5 deletions test/app_profiler/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,16 @@ def call(env)
end
end

test "request is not sampled when killswitch is on" do
old_killswitch = AppProfiler.profile_sampler_killswitch_on
test "request is not sampled when sampler is not enabled via Proc" do
with_google_cloud_storage do
AppProfiler.profile_sampler_config = AppProfiler::Sampler::Config.new(sample_rate: 1.0)
AppProfiler.profile_sampler_killswitch_on = -> { true }
AppProfiler.profile_sampler_enabled = -> { false }
assert_profiles_dumped(0) do
middleware = AppProfiler::Middleware.new(app_env)
response = middleware.call(mock_request_env(path: "/"))
assert_nil(response[1]["X-Profile-Async"])
end
end
ensure
AppProfiler.profile_sampler_killswitch_on = old_killswitch
end

private
Expand All @@ -421,6 +418,7 @@ def with_profile_sampler_enabled
AppProfiler.profile_sampler_enabled = true
yield
ensure
puts "status #{old_status}"
AppProfiler.profile_sampler_enabled = old_status
end

Expand Down

0 comments on commit 3e7665c

Please sign in to comment.