From 97efad806657333aa12ad8f08d2a2cafef924699 Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Tue, 28 Sep 2021 17:01:23 +0300 Subject: [PATCH 1/2] Fix accessing to config attrs --- .rspec | 3 ++ bc-prometheus-ruby.gemspec | 1 + .../prometheus/instrumentors/web.rb | 8 +++- .../prometheus/instrumentors/web_spec.rb | 40 +++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 .rspec create mode 100644 spec/bigcommerce/prometheus/instrumentors/web_spec.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/bc-prometheus-ruby.gemspec b/bc-prometheus-ruby.gemspec index 016dfe7..378eea9 100644 --- a/bc-prometheus-ruby.gemspec +++ b/bc-prometheus-ruby.gemspec @@ -36,6 +36,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler-audit', '>= 0.6' spec.add_development_dependency 'null-logger', '>= 0.1' spec.add_development_dependency 'pry', '>= 0.12' + spec.add_development_dependency 'railties', '>= 6.0.0' spec.add_development_dependency 'rake', '>= 10.0' spec.add_development_dependency 'rspec', '>= 3.8' spec.add_development_dependency 'rspec_junit_formatter', '>= 0.4' diff --git a/lib/bigcommerce/prometheus/instrumentors/web.rb b/lib/bigcommerce/prometheus/instrumentors/web.rb index d37b7af..72ca2d3 100644 --- a/lib/bigcommerce/prometheus/instrumentors/web.rb +++ b/lib/bigcommerce/prometheus/instrumentors/web.rb @@ -62,7 +62,7 @@ def server end def setup_before_fork - @app.config.before_fork_callbacks = [] unless @app.config.before_fork_callbacks + @app.config.before_fork_callbacks = [] unless config_defined?(:before_fork_callbacks) @app.config.before_fork_callbacks << lambda do server.add_type_collector(PrometheusExporter::Server::ActiveRecordCollector.new) server.add_type_collector(PrometheusExporter::Server::WebCollector.new) @@ -75,13 +75,17 @@ def setup_before_fork end def setup_after_fork - @app.config.after_fork_callbacks = [] unless @app.config.after_fork_callbacks + @app.config.after_fork_callbacks = [] unless config_defined?(:after_fork_callbacks) @app.config.after_fork_callbacks << lambda do ::Bigcommerce::Prometheus::Integrations::Puma.start(client: Bigcommerce::Prometheus.client) @collectors.each(&:start) end end + def config_defined?(config_name) + @app.config.respond_to?(config_name) && @app.config.public_send(config_name) + end + def setup_middleware @app.middleware.unshift(PrometheusExporter::Middleware, client: Bigcommerce::Prometheus.client) rescue StandardError => e diff --git a/spec/bigcommerce/prometheus/instrumentors/web_spec.rb b/spec/bigcommerce/prometheus/instrumentors/web_spec.rb new file mode 100644 index 0000000..c3c13a3 --- /dev/null +++ b/spec/bigcommerce/prometheus/instrumentors/web_spec.rb @@ -0,0 +1,40 @@ +describe Bigcommerce::Prometheus::Instrumentors::Web do + def run! + described_class.new(app: application).start + end + + let(:application) do + instance_double(Rails::Application).tap do |instance| + allow(instance).to receive(:config).and_return(configuration) + allow(instance).to receive(:middleware).and_return(middleware) + end + end + + let(:configuration) { Rails::Engine::Configuration.new } + let(:middleware) { [] } + + it 'properly handles lack of the fork configs' do + expect(Bigcommerce::Prometheus.logger).not_to receive(:error) + + run! + + expect(configuration.respond_to?(:before_fork_callbacks)).to eq(true) + expect(configuration.respond_to?(:after_fork_callbacks)).to eq(true) + expect(configuration.before_fork_callbacks).to be_an_instance_of(Array) + expect(configuration.after_fork_callbacks).to be_an_instance_of(Array) + end + + context 'when configuration already set' do + before do + configuration.before_fork_callbacks = [1] + configuration.after_fork_callbacks = [1] + end + + it 'just adds another elements' do + run! + + expect(configuration.before_fork_callbacks.size).to eq(2) + expect(configuration.after_fork_callbacks.size).to eq(2) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 07e6061..9b0b19c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ require_relative 'simplecov_helper' require 'bigcommerce/prometheus' require 'pry' +require 'rails' Dir["#{File.join(File.dirname(__FILE__), 'support')}/**/*.rb"].sort.each { |f| require f } RSpec.configure do |config| From 767d3080ea63a0ed354b4420673692ab18437d48 Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Tue, 28 Sep 2021 17:05:53 +0300 Subject: [PATCH 2/2] Remove documentation format --- .rspec | 1 - 1 file changed, 1 deletion(-) diff --git a/.rspec b/.rspec index 34c5164..83e16f8 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,2 @@ ---format documentation --color --require spec_helper