Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/app_profiler/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def profile(env, params)
end

return response unless profile && after_profile(env, profile)
return response if profile[:samples] == 0

action.call(
profile,
Expand Down
16 changes: 15 additions & 1 deletion test/app_profiler/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ class MiddlewareTest < TestCase
middleware.call(mock_request_env(opt: opt))
end

test "profile without samples will not be uploaded" do
profile = AppProfiler::BaseProfile.from_stackprof(stackprof_profile(samples: 0))
AppProfiler.expects(:run).returns(profile)
AppProfiler.middleware.any_instance.expects(:after_profile).with { |_env, value| value == profile }.returns(true)
AppProfiler.middleware.action.expects(:call).never
middleware = AppProfiler::Middleware.new(app_env)
opt = { AppProfiler.request_profile_header => "mode=cpu;interval=2000" }
middleware.call(mock_request_env(opt: opt))
end

test "should not profile if #before_profile returns false" do
AppProfiler.expects(:run).never
AppProfiler.middleware.any_instance.stubs(:before_profile).returns(false)
Expand Down Expand Up @@ -482,7 +492,11 @@ def with_otel_instrumentation_enabled
end

def app_env
->(_) { [200, {}, ["OK"]] }
lambda do |_env|
# Ensure requests expected to produce profiles run long enough to be sampled.
1_000_000.times { Object.new } if AppProfiler.running?
[200, {}, ["OK"]]
end
end

def mock_request_env(path: "/", opt: {})
Expand Down
Loading