diff --git a/Gemfile b/Gemfile index e94e9eb..6adbb64 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ gem "database_cleaner-active_record", "~> 2.0" gem "standard", "~> 1.3" gem "simplecov", "~> 0.21.0" gem "pry" +gem "webmock", "~> 3.14" # Dummy app dependencies gem "sqlite3", "~> 1.4" diff --git a/README.md b/README.md index 7a46087..ac9b9c9 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Change configuration in an initializer if you need: IoToResponsePayloadRatio.configure do |config| config.publish = :notifications # defaults to :logs config.warn_threshold = 0.8 # defaults to 0 + config.adapters = [:active_record, :net_http] # defaults to [:active_record] end ``` diff --git a/lib/io_to_response_payload_ratio.rb b/lib/io_to_response_payload_ratio.rb index 5d9cb5c..7f93c71 100644 --- a/lib/io_to_response_payload_ratio.rb +++ b/lib/io_to_response_payload_ratio.rb @@ -7,6 +7,7 @@ require "io_to_response_payload_ratio/adapters/base_adapter" require "io_to_response_payload_ratio/adapters/active_record_adapter" +require "io_to_response_payload_ratio/adapters/net_http_adapter" require "io_to_response_payload_ratio/publishers/base_publisher" require "io_to_response_payload_ratio/publishers/logs_publisher" @@ -16,7 +17,7 @@ module IoToResponsePayloadRatio NAMESPACE = :io_to_response_payload_ratio - ADAPTERS = [ActiveRecordAdapter].freeze + ADAPTERS = [ActiveRecordAdapter, NetHttpAdapter].freeze PUBLISHERS = [LogsPublisher, NotificationsPublisher].freeze class << self diff --git a/lib/io_to_response_payload_ratio/adapters/net_http_adapter.rb b/lib/io_to_response_payload_ratio/adapters/net_http_adapter.rb new file mode 100644 index 0000000..bbe8961 --- /dev/null +++ b/lib/io_to_response_payload_ratio/adapters/net_http_adapter.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "net/http" +require "io_to_response_payload_ratio/patches/net_http_adapter_patch" + +module IoToResponsePayloadRatio + class NetHttpAdapter < BaseAdapter + def self.kind + :net_http + end + + def initialize! + ActiveSupport.on_load(:after_initialize) do + Net::HTTP.prepend(NetHttpAdapterPatch) + + if defined?(::WebMock) + WebMock::HttpLibAdapters::NetHttpAdapter + .instance_variable_get(:@webMockNetHTTP) + .prepend(NetHttpAdapterPatch) + end + end + end + end +end diff --git a/lib/io_to_response_payload_ratio/patches/net_http_adapter_patch.rb b/lib/io_to_response_payload_ratio/patches/net_http_adapter_patch.rb new file mode 100644 index 0000000..6911340 --- /dev/null +++ b/lib/io_to_response_payload_ratio/patches/net_http_adapter_patch.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module IoToResponsePayloadRatio + module NetHttpAdapterPatch + def request(*args, &block) + super do |response| + if response.body && IoToResponsePayloadRatio.aggregator.active? + IoToResponsePayloadRatio.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize) + end + end + end + end +end diff --git a/spec/io_to_response_payload_ratio/adapters/net_http_adapter_spec.rb b/spec/io_to_response_payload_ratio/adapters/net_http_adapter_spec.rb new file mode 100644 index 0000000..a5696bf --- /dev/null +++ b/spec/io_to_response_payload_ratio/adapters/net_http_adapter_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +RSpec.describe IoToResponsePayloadRatio::NetHttpAdapter do + let(:aggregator) { IoToResponsePayloadRatio.aggregator } + let(:body) { "Hello, World!" } + let(:url) { "www.example.com" } + + before do + stub_request(:get, url).to_return body: body + end + + before do + aggregator.start! + end + + after do + aggregator.stop! + end + + context "when aggregator is inactive" do + before do + aggregator.stop! + end + + it "does nothing" do + expect(aggregator).not_to receive(:increment) + + Net::HTTP.get url, "/" + end + end + + it "increments aggregator by request's body bytesize" do + allow(aggregator).to receive(:increment) + + Net::HTTP.get url, "/" + + expect(aggregator).to have_received(:increment).with(described_class.kind, body.bytesize) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3c3d4d8..7fb89a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "webmock/rspec" require "simplecov" SimpleCov.start if ENV["COVERAGE"]