From 71b3e3d3b83832048bec88dafd4ea5533afb0a79 Mon Sep 17 00:00:00 2001 From: hiromi-ogawa Date: Fri, 20 Sep 2024 10:28:26 +0900 Subject: [PATCH 1/2] Support Unicorn in Promenade::Raindrops::Middleware --- lib/promenade/raindrops/middleware.rb | 9 +++- spec/promenade/raindrops/middleware_spec.rb | 47 +++++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/lib/promenade/raindrops/middleware.rb b/lib/promenade/raindrops/middleware.rb index 55173ed..c312336 100644 --- a/lib/promenade/raindrops/middleware.rb +++ b/lib/promenade/raindrops/middleware.rb @@ -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 diff --git a/spec/promenade/raindrops/middleware_spec.rb b/spec/promenade/raindrops/middleware_spec.rb index ed8c402..d82793a 100644 --- a/spec/promenade/raindrops/middleware_spec.rb +++ b/spec/promenade/raindrops/middleware_spec.rb @@ -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 From 121bac5cb5bbcfa35b5f4822944dadbf7881c298 Mon Sep 17 00:00:00 2001 From: hiromi-ogawa Date: Fri, 20 Sep 2024 10:45:54 +0900 Subject: [PATCH 2/2] Use raindrops middleware only when either pitchfork or unicorn is loaded --- lib/promenade/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/promenade/railtie.rb b/lib/promenade/railtie.rb index 3835a85..1455b1f 100644 --- a/lib/promenade/railtie.rb +++ b/lib/promenade/railtie.rb @@ -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