Skip to content

Commit

Permalink
new config
Browse files Browse the repository at this point in the history
  • Loading branch information
KapustaB committed Mar 25, 2024
1 parent fa2bbbb commit 43cad3b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 303 deletions.
2 changes: 2 additions & 0 deletions lib/treblle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def configuration

def configure
yield(configuration)

configuration.validate_credentials! if configuration.enabled_environment?
end
end
end
18 changes: 16 additions & 2 deletions lib/treblle/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Configuration
ssn
].freeze

attr_accessor :restricted_endpoints, :whitelisted_endpoints, :enabled_environments,
attr_accessor :restricted_endpoints, :whitelisted_endpoints,
:api_key, :project_id, :app_version
attr_reader :sensitive_attrs
attr_reader :sensitive_attrs, :enabled_environments

def initialize
@restricted_endpoints = []
Expand All @@ -38,13 +38,27 @@ def sensitive_attrs=(attributes)
@sensitive_attrs = attributes ? DEFAULT_SENSITIVE_ATTRS + attributes : DEFAULT_SENSITIVE_ATTRS
end

def enabled_environments=(value)
@enabled_environments = Array(value)
end

def enabled_environment?
return false if enabled_environments.empty?

enabled_environments.include?(environment)
end

def validate_credentials!
raise Errors::MissingApiKeyError if api_key.to_s.empty?
raise Errors::MissingProjectIdError if project_id.to_s.empty?
end

private

def environment
@environment || ::Rails.env
end

def whitelisted_endpoint?(request_url)
Array(whitelisted_endpoints).any? do |endpoint|
request_url.start_with?(endpoint)
Expand Down
21 changes: 11 additions & 10 deletions lib/treblle/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ def call(env)

def call_with_treblle_monitoring(env)
started_at = Time.now
begin
response = @app.call(env)
rescue Exception => e
handle_monitoring(env, response, started_at, exception: true)
raise e
end

response = @app.call(env)
status, _headers, _rack_response = response

handle_monitoring(env, response, started_at) if status < 400

handle_monitoring(env, response, started_at)
response
end

def handle_monitoring(env, rack_response, started_at)
configuration.validate_credentials!

def handle_monitoring(env, rack_response, started_at, exception: false)
request = RequestBuilder.new(env).build
response = ResponseBuilder.new(rack_response).build
payload = GeneratePayload.new(request: request, response: response, started_at: started_at).call
payload = GeneratePayload.new(request: request, response: response, started_at: started_at,
exception: exception).call

Dispatcher.new(payload: payload).call
rescue StandardError => e
log_error(e.message)
end

def should_monitor?(env)
configuration.monitoring_enabled?(env['PATH_INFO'])
configuration.enabled_environment? && configuration.monitoring_enabled?(env['PATH_INFO'])
end
end
end
61 changes: 0 additions & 61 deletions lib/treblle/rails/capture_exceptions.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/treblle/rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'treblle/rails/capture_exceptions'
require 'treblle/middleware'
require 'rails'

module Treblle
module Rails
class Railtie < ::Rails::Railtie
initializer 'treblle.install_middleware' do |app|
app.config.middleware.insert_before ActionDispatch::ShowExceptions, Treblle::Rails::CaptureExceptions
app.config.middleware.insert_after ActionDispatch::ShowExceptions, Treblle::Middleware
end
end
end
Expand Down
45 changes: 44 additions & 1 deletion spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
before do
config.api_key = 'your-api-key'
config.project_id = 'your-project-id'
config.enabled_environments = 'development'

allow(Rails).to receive(:env).and_return('development')
end

context '#monitoring_enabled?' do
Expand Down Expand Up @@ -117,7 +120,7 @@
expect(config.whitelisted_endpoints).to eq('/custom_endpoint/')
end

describe 'when whitelisted_endpoints is an array' do
context 'when whitelisted_endpoints is an array' do
before do
config.whitelisted_endpoints = ['/api/', '/custom_endpoint/']
end
Expand All @@ -127,4 +130,44 @@
end
end
end

describe '#enabled_environments=' do
it 'converts the value to an array' do
subject.enabled_environments = 'production'
expect(subject.enabled_environments).to eq(['production'])

subject.enabled_environments = %w[staging production]
expect(subject.enabled_environments).to eq(%w[staging production])

subject.enabled_environments = nil
expect(subject.enabled_environments).to eq([])
end
end

describe '#enabled_environment?' do
before do
allow(subject).to receive(:environment).and_return('production')
end

context 'when enabled_environments is empty' do
it 'returns false' do
subject.enabled_environments = []
expect(subject.enabled_environment?).to be_falsey
end
end

context 'when environment is included in enabled_environments' do
it 'returns true' do
subject.enabled_environments = ['production']
expect(subject.enabled_environment?).to be_truthy
end
end

context 'when environment is not included in enabled_environments' do
it 'returns false' do
subject.enabled_environments = ['staging']
expect(subject.enabled_environment?).to be_falsey
end
end
end
end
45 changes: 29 additions & 16 deletions spec/lib/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
let(:request_url) { '/api/some_endpoint' }

before do
allow_any_instance_of(Treblle::Configuration).to receive(:enabled_environment?).and_return(true)
allow_any_instance_of(Treblle::Configuration).to receive(:api_key).and_return('your_api_key')
allow_any_instance_of(Treblle::Configuration).to receive(:project_id).and_return('project_id')
allow_any_instance_of(Treblle::Dispatcher).to receive(:get_uri)
Expand Down Expand Up @@ -163,13 +164,16 @@
)
end

# This is scenario is handled by Exception Middleware
it 'fails to make an HTTP request to Treblle and handles the failure' do
it 'makes a successful HTTP request to Treblle and logs the response' do
stub = stub_request(:post, treblle_url).to_return(status: 200, body: 'OK')
expect(Logger).to receive_message_chain(:new, :info).with(/Successfully sent to Treblle:/)

subject.call(env)
status, headers, response_body = subject.call(env)

expect(stub).not_to have_been_requested
expect(stub).to have_been_requested
expect(status).to be 404
expect(response_body).to eq('OK')
expect(headers['REQUEST_METHOD']).to eq('GET')
end
end

Expand All @@ -182,13 +186,16 @@
)
end

# This is scenario is handled by Exception Middleware
it 'fails to make an HTTP request to Treblle and handles the failure' do
it 'makes a successful HTTP request to Treblle and logs the response' do
stub = stub_request(:post, treblle_url).to_return(status: 200, body: 'OK')
expect(Logger).to receive_message_chain(:new, :info).with(/Successfully sent to Treblle:/)

subject.call(env)
status, headers, response_body = subject.call(env)

expect(stub).not_to have_been_requested
expect(stub).to have_been_requested
expect(status).to be 401
expect(response_body).to eq('OK')
expect(headers['REQUEST_METHOD']).to eq('GET')
end
end

Expand All @@ -201,13 +208,16 @@
)
end

# This is scenario is handled by Exception Middleware
it 'fails to make an HTTP request to Treblle and handles the failure' do
it 'makes a successful HTTP request to Treblle and logs the response' do
stub = stub_request(:post, treblle_url).to_return(status: 200, body: 'OK')
expect(Logger).to receive_message_chain(:new, :info).with(/Successfully sent to Treblle:/)

subject.call(env)
status, headers, response_body = subject.call(env)

expect(stub).not_to have_been_requested
expect(stub).to have_been_requested
expect(status).to be 403
expect(response_body).to eq('OK')
expect(headers['REQUEST_METHOD']).to eq('GET')
end
end

Expand All @@ -220,13 +230,16 @@
)
end

# This is scenario is handled by Exception Middleware
it 'fails to make an HTTP request to Treblle and handles the failure' do
it 'makes a successful HTTP request to Treblle and logs the response' do
stub = stub_request(:post, treblle_url).to_return(status: 200, body: 'OK')
expect(Logger).to receive_message_chain(:new, :info).with(/Successfully sent to Treblle:/)

subject.call(env)
status, headers, response_body = subject.call(env)

expect(stub).not_to have_been_requested
expect(stub).to have_been_requested
expect(status).to be 500
expect(response_body).to eq('OK')
expect(headers['REQUEST_METHOD']).to eq('GET')
end
end
end
Loading

0 comments on commit 43cad3b

Please sign in to comment.