Skip to content

Commit

Permalink
Add specs for Loga.attach_context
Browse files Browse the repository at this point in the history
* Sidekiq
* Sinatra
* ActionMailer
* Rails
  • Loading branch information
gnkostov authored and bliof-fc committed Jun 17, 2019
1 parent 1b31f37 commit 389d498
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 75 deletions.
29 changes: 29 additions & 0 deletions spec/fixtures/rails32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update
@id = params[:id]
render '/user'
end

def attach_context
Loga.attach_context(animal: 'monkey')

render json: { hello: :world }
end
end

class FakeMailer < ActionMailer::Base
Expand All @@ -68,13 +74,36 @@ def basic_mail
end
end

class FakeMailerWithContext < ActionMailer::Base
default from: 'notifications@example.com'

def self.send_email
Loga.attach_context(tshirt_size: 'XL')
Loga.attach_context(height: 155)
Loga.attach_context(weight: 122)

basic_mail.deliver
end

def basic_mail
mail(
to: 'user@example.com',
subject: 'Welcome to My Awesome Site',
body: 'Banana muffin',
content_type: 'text/html',
)
end
end

Dummy.routes.append do
get 'ok' => 'application#ok'
get 'error' => 'application#error'
get 'show' => 'application#show'
post 'users' => 'application#create'
get 'new' => 'application#new'
put 'users/:id' => 'application#update'

get 'attach_context' => 'application#attach_context'
end

Dummy.initialize!
29 changes: 29 additions & 0 deletions spec/fixtures/rails40.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update
@id = params[:id]
render '/user'
end

def attach_context
Loga.attach_context(animal: 'monkey')

render json: { hello: :world }
end
end

class FakeMailer < ActionMailer::Base
Expand All @@ -68,13 +74,36 @@ def basic_mail
end
end

class FakeMailerWithContext < ActionMailer::Base
default from: 'notifications@example.com'

def self.send_email
Loga.attach_context(tshirt_size: 'XL')
Loga.attach_context(height: 155)
Loga.attach_context(weight: 122)

basic_mail.deliver
end

def basic_mail
mail(
to: 'user@example.com',
subject: 'Welcome to My Awesome Site',
body: 'Banana muffin',
content_type: 'text/html',
)
end
end

Dummy.routes.append do
get 'ok' => 'application#ok'
get 'error' => 'application#error'
get 'show' => 'application#show'
post 'users' => 'application#create'
get 'new' => 'application#new'
put 'users/:id' => 'application#update'

get 'attach_context' => 'application#attach_context'
end

Dummy.initialize!
29 changes: 29 additions & 0 deletions spec/fixtures/rails42.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update
@id = params[:id]
render '/user'
end

def attach_context
Loga.attach_context(animal: 'monkey')

render json: { hello: :world }
end
end

class FakeMailer < ActionMailer::Base
Expand All @@ -68,13 +74,36 @@ def basic_mail
end
end

class FakeMailerWithContext < ActionMailer::Base
default from: 'notifications@example.com'

def self.send_email
Loga.attach_context(tshirt_size: 'XL')
Loga.attach_context(height: 155)
Loga.attach_context(weight: 122)

basic_mail.deliver_now
end

def basic_mail
mail(
to: 'user@example.com',
subject: 'Welcome to My Awesome Site',
body: 'Banana muffin',
content_type: 'text/html',
)
end
end

Dummy.routes.append do
get 'ok' => 'application#ok'
get 'error' => 'application#error'
get 'show' => 'application#show'
post 'users' => 'application#create'
get 'new' => 'application#new'
put 'users/:id' => 'application#update'

get 'attach_context' => 'application#attach_context'
end

Dummy.initialize!
29 changes: 29 additions & 0 deletions spec/fixtures/rails50.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update
@id = params[:id]
render '/user'
end

def attach_context
Loga.attach_context(animal: 'monkey')

render json: { hello: :world }
end
end

class FakeMailer < ActionMailer::Base
Expand All @@ -68,13 +74,36 @@ def basic_mail
end
end

class FakeMailerWithContext < ActionMailer::Base
default from: 'notifications@example.com'

def self.send_email
Loga.attach_context(tshirt_size: 'XL')
Loga.attach_context(height: 155)
Loga.attach_context(weight: 122)

basic_mail.deliver_now
end

def basic_mail
mail(
to: 'user@example.com',
subject: 'Welcome to My Awesome Site',
body: 'Banana muffin',
content_type: 'text/html',
)
end
end

Dummy.routes.append do
get 'ok' => 'application#ok'
get 'error' => 'application#error'
get 'show' => 'application#show'
post 'users' => 'application#create'
get 'new' => 'application#new'
put 'users/:id' => 'application#update'

get 'attach_context' => 'application#attach_context'
end

Dummy.initialize!
29 changes: 29 additions & 0 deletions spec/fixtures/rails52.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update
@id = params[:id]
render '/user'
end

def attach_context
Loga.attach_context(animal: 'monkey')

render json: { hello: :world }
end
end

class FakeMailer < ActionMailer::Base
Expand All @@ -68,13 +74,36 @@ def basic_mail
end
end

class FakeMailerWithContext < ActionMailer::Base
default from: 'notifications@example.com'

def self.send_email
Loga.attach_context(tshirt_size: 'XL')
Loga.attach_context(height: 155)
Loga.attach_context(weight: 122)

basic_mail.deliver_now
end

def basic_mail
mail(
to: 'user@example.com',
subject: 'Welcome to My Awesome Site',
body: 'Banana muffin',
content_type: 'text/html',
)
end
end

Dummy.routes.append do
get 'ok' => 'application#ok'
get 'error' => 'application#error'
get 'show' => 'application#show'
post 'users' => 'application#create'
get 'new' => 'application#new'
put 'users/:id' => 'application#update'

get 'attach_context' => 'application#attach_context'
end

Dummy.initialize!
12 changes: 12 additions & 0 deletions spec/integration/rails/action_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,17 @@

expect(last_log_entry).to have_key('_unique_id')
end

it 'attaches custom context' do
FakeMailerWithContext.send_email

expected_context = {
'_tshirt_size' => 'XL',
'_height' => 155,
'_weight' => 122,
}

expect(last_log_entry).to include(expected_context)
end
end
end
55 changes: 35 additions & 20 deletions spec/integration/rails/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

let(:log_entries) do
entries = []

STREAM.tap do |s|
s.rewind
entries = s.read.split("\n").map { |line| JSON.parse(line) }
s.close
s.reopen
end

entries
end

Expand All @@ -22,43 +24,56 @@

it 'preserves rails parameters' do
get '/show'

expect(json_response).to eq('action' => 'show', 'controller' => 'application')
end

it 'includes the controller name and action' do
get '/ok'

expect(last_log_entry).to include('_request.controller' => 'ApplicationController#ok')
end

describe 'LogSubscriber' do
describe 'ActionController' do
let(:action_controller_notifications) do
log_entries.select { |e| e.to_json =~ /Processing by|Completed/ }
end
describe 'ActionController::LogSubscriber' do
let(:action_controller_notifications) do
log_entries.select { |e| e.to_json =~ /Processing by|Completed/ }
end

it 'silences ActionController::LogSubscriber' do
get '/show'
expect(action_controller_notifications).to be_empty
end
it 'silences ActionController::LogSubscriber' do
get '/show'

expect(action_controller_notifications).to be_empty
end
end

describe 'ActionView' do
let(:action_view_notifications) do
log_entries.select { |e| e.to_json =~ /Rendered/ }
end
describe 'ActionView::LogSubscriber' do
let(:action_view_notifications) do
log_entries.select { |e| e.to_json =~ /Rendered/ }
end

it 'silences ActionView::LogSubscriber' do
put '/users/5'
expect(action_view_notifications).to be_empty
end
it 'silences ActionView::LogSubscriber' do
put '/users/5'

expect(action_view_notifications).to be_empty
end
end

describe 'when request causes ActionDispatch 404' do
context 'when request causes ActionDispatch 404' do
it 'does not log ActionDispatch::DebugExceptions' do
get '/not_found', {}, 'HTTP_X_REQUEST_ID' => '471a34dc'
expect(log_entries.count).to eq(1)
expect(last_log_entry['short_message']).to eq('GET /not_found 404 in 0ms')

aggregate_failures do
expect(log_entries.count).to eq(1)
expect(last_log_entry['short_message']).to eq('GET /not_found 404 in 0ms')
end
end
end

context 'when custom context is attached' do
it 'adds context to the log' do
get '/attach_context'

expect(last_log_entry['_animal']).to eq 'monkey'
end
end
end
Loading

0 comments on commit 389d498

Please sign in to comment.