Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Unicorn in Promenade::Raindrops::Middleware #69

Merged
merged 2 commits into from
Sep 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/promenade/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Railtie < ::Rails::Railtie
Rails.application.config.middleware.insert 0,
Promenade::Client::Rack::HTTPRequestQueueTimeCollector

if defined?(::Raindrops)
if defined?(::Raindrops) && (defined?(::Pitchfork) || defined?(::Unicorn))
require "promenade/raindrops/middleware"
Rails.application.config.middleware.use Promenade::Raindrops::Middleware
end
Expand Down
9 changes: 8 additions & 1 deletion lib/promenade/raindrops/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ def call(env)
private

def tcp_listener_names
::Pitchfork.listener_names
if defined?(::Pitchfork)
::Pitchfork.listener_names
elsif defined?(::Unicorn)
::Unicorn.listener_names
else
raise StandardError,
"Promenade::Raindrops::Middleware expects either ::Pitchfork or ::Unicorn to be defined"
end
end

def instrument
Expand Down
47 changes: 38 additions & 9 deletions spec/promenade/raindrops/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,48 @@
RSpec.describe Promenade::Raindrops::Middleware do
let(:app) { double(:app, call: nil) }
let(:listener_address) { "127.0.0.1:#{ENV.fetch('PORT', 3000)}" }
let(:pitchfork) { class_double("Pitchfork").as_stubbed_const }

before do
allow(pitchfork).to receive(:listener_names).and_return([listener_address])
shared_examples "middleware" do
it "is add it's instrumentaion to the rack.after_reply" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const

after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)

expect(stats).to have_received(:instrument).with(listener_address: listener_address)
end
end

it "is add it's instrumentaion to the rack.after_reply" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const
context "when Pitchfork is defined" do
let(:pitchfork) { class_double("Pitchfork").as_stubbed_const }

before do
allow(pitchfork).to receive(:listener_names).and_return([listener_address])
end

it_behaves_like "middleware"
end

context "when Unicorn is defined" do
let(:unicorn) { class_double("Unicorn").as_stubbed_const }

before do
allow(unicorn).to receive(:listener_names).and_return([listener_address])
end

it_behaves_like "middleware"
end

after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)
context "when neither Pitchfork nor Unicorn is defined" do
it "raises an error" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const

expect(stats).to have_received(:instrument).with(listener_address: listener_address)
expect do
after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)
end.to raise_error "Promenade::Raindrops::Middleware expects either ::Pitchfork or ::Unicorn to be defined"
end
end
end