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

Fix the error with accessing a non-existent attribute in the config #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
1 change: 1 addition & 0 deletions bc-prometheus-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem supports Rails 5.2+, so we'll need to not limit this to 6.0.

spec.add_development_dependency 'rake', '>= 10.0'
spec.add_development_dependency 'rspec', '>= 3.8'
spec.add_development_dependency 'rspec_junit_formatter', '>= 0.4'
Expand Down
8 changes: 6 additions & 2 deletions lib/bigcommerce/prometheus/instrumentors/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions spec/bigcommerce/prometheus/instrumentors/web_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_relative 'simplecov_helper'
require 'bigcommerce/prometheus'
require 'pry'
require 'rails'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gem is used in non-rails systems, so we shouldn't pull in this dependency here.

Dir["#{File.join(File.dirname(__FILE__), 'support')}/**/*.rb"].sort.each { |f| require f }

RSpec.configure do |config|
Expand Down