Skip to content

Commit

Permalink
Add more context to new relic error tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroHamzehT authored and lucasgomide committed Dec 14, 2021
1 parent fd8d6e7 commit 8f9b557
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ def agent
def transaction_name!(*opts)
agent.transaction_name!(opts)
end

def add_custom_attributes(custom_attributes)
agent.add_custom_attributes(custom_attributes)
end
end
end
2 changes: 2 additions & 0 deletions lib/rails_cloud_tasks/instrumentation/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsCloudTasks
module Instrumentation
class Default
def transaction_name!(*opts); end

def add_custom_attributes(custom_attributes); end
end
end
end
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/instrumentation/new_relic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def transaction_name!(opts)
agent.set_transaction_name(*opts)
end

def add_custom_attributes(custom_attributes)
agent.add_custom_attributes(custom_attributes)
end

def agent
@agent ||= ::NewRelic::Agent
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/rack/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def call(env)
request = ::Rack::Request.new(env)
job_args = extract_args(request)

RailsCloudTasks::Instrumentation.add_custom_attributes(
{ request_body: job_args }
)

job_class.perform_now(*job_args)

response(200, {})
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/rack/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def call(env)
"RailsCloudTasks/#{job['job_class']}/perform_now"
)

RailsCloudTasks::Instrumentation.add_custom_attributes(
{ request_body: job['arguments'] }
)

ActiveJob::Base.execute(job)
response(200, {})
rescue Rack::InvalidPayloadError => e
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_cloud_tasks/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsCloudTasks
VERSION = '0.0.9'.freeze
VERSION = '0.0.10'.freeze
end
20 changes: 19 additions & 1 deletion spec/rails_cloud_tasks/instrumentation/new_relic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
subject(:intrumentation_new_relic) { described_class.new }

let(:agent_client) do
Class.new { def self.set_transaction_name(opts); end } # rubocop:disable Naming/AccessorMethodName
Class.new do
def self.set_transaction_name(opts); end # rubocop:disable Naming/AccessorMethodName
def self.add_custom_attributes(custom_attributes); end
end
end

let(:agent) { class_spy(agent_client) }
Expand All @@ -21,4 +24,19 @@
expect(agent).to have_received(:set_transaction_name).with(params)
end
end

describe '#add_custom_attributes' do
subject(:add_custom_attributes) { intrumentation_new_relic.add_custom_attributes(params) }

let(:params) { { request_body: 'spec body' } }

before do
allow(intrumentation_new_relic).to receive(:agent).and_return(agent)
end

it do
add_custom_attributes
expect(agent).to have_received(:add_custom_attributes).with(params)
end
end
end
7 changes: 7 additions & 0 deletions spec/rails_cloud_tasks/rack/jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
before do
allow(DummyJob).to receive(:perform_now).with(*args).and_return(:ok)
allow(RailsCloudTasks::Instrumentation).to receive(:transaction_name!)
allow(RailsCloudTasks::Instrumentation).to receive(:add_custom_attributes)
end

it do
Expand All @@ -27,6 +28,12 @@
.with("RailsCloudTasks/#{job_class}/perform_now")
end

it do
call
expect(RailsCloudTasks::Instrumentation).to have_received(:add_custom_attributes)
.with({ request_body: args })
end

context 'when job is successfully attempted' do
its(:first) { is_expected.to eq 200 }
its(:second) { is_expected.to eq('Content-Type' => 'application/json') }
Expand Down
11 changes: 10 additions & 1 deletion spec/rails_cloud_tasks/rack/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{ 'rack.input' => StringIO.new(payload.to_json) }
end

let(:arguments) { [123] }

let(:payload) do
{
job: {
Expand All @@ -14,7 +16,7 @@
provider_job_id: nil,
queue_name: 'test-queue',
priority: nil,
arguments: [123],
arguments: arguments,
executions: 0,
locale: 'en'
}
Expand All @@ -27,6 +29,7 @@
before do
allow(ActiveJob::Base).to receive(:execute).and_return(:ok)
allow(RailsCloudTasks::Instrumentation).to receive(:transaction_name!)
allow(RailsCloudTasks::Instrumentation).to receive(:add_custom_attributes)
end

it do
Expand All @@ -35,6 +38,12 @@
.with("RailsCloudTasks/#{payload[:job][:job_class]}/perform_now")
end

it do
call
expect(RailsCloudTasks::Instrumentation).to have_received(:add_custom_attributes)
.with({ request_body: arguments })
end

context 'when job is successfully attempted' do
its(:first) { is_expected.to eq 200 }
its(:second) { is_expected.to eq('Content-Type' => 'application/json') }
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_cloud_tasks/version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
describe RailsCloudTasks::VERSION do
it { is_expected.to eq '0.0.9' }
it { is_expected.to eq '0.0.10' }
end

0 comments on commit 8f9b557

Please sign in to comment.