diff --git a/lib/rails_cloud_tasks/instrumentation.rb b/lib/rails_cloud_tasks/instrumentation.rb index 3aa52d2..7fa8be1 100644 --- a/lib/rails_cloud_tasks/instrumentation.rb +++ b/lib/rails_cloud_tasks/instrumentation.rb @@ -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 diff --git a/lib/rails_cloud_tasks/instrumentation/default.rb b/lib/rails_cloud_tasks/instrumentation/default.rb index 8ed24ce..97b6bc4 100644 --- a/lib/rails_cloud_tasks/instrumentation/default.rb +++ b/lib/rails_cloud_tasks/instrumentation/default.rb @@ -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 diff --git a/lib/rails_cloud_tasks/instrumentation/new_relic.rb b/lib/rails_cloud_tasks/instrumentation/new_relic.rb index 8ce90aa..25606cb 100644 --- a/lib/rails_cloud_tasks/instrumentation/new_relic.rb +++ b/lib/rails_cloud_tasks/instrumentation/new_relic.rb @@ -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 diff --git a/lib/rails_cloud_tasks/rack/jobs.rb b/lib/rails_cloud_tasks/rack/jobs.rb index d68542b..99e0b1f 100644 --- a/lib/rails_cloud_tasks/rack/jobs.rb +++ b/lib/rails_cloud_tasks/rack/jobs.rb @@ -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, {}) diff --git a/lib/rails_cloud_tasks/rack/tasks.rb b/lib/rails_cloud_tasks/rack/tasks.rb index 4f2f9ae..6ebe284 100644 --- a/lib/rails_cloud_tasks/rack/tasks.rb +++ b/lib/rails_cloud_tasks/rack/tasks.rb @@ -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 diff --git a/lib/rails_cloud_tasks/version.rb b/lib/rails_cloud_tasks/version.rb index 5b16471..b479031 100644 --- a/lib/rails_cloud_tasks/version.rb +++ b/lib/rails_cloud_tasks/version.rb @@ -1,3 +1,3 @@ module RailsCloudTasks - VERSION = '0.0.9'.freeze + VERSION = '0.0.10'.freeze end diff --git a/spec/rails_cloud_tasks/instrumentation/new_relic_spec.rb b/spec/rails_cloud_tasks/instrumentation/new_relic_spec.rb index 3f82083..c4f36b4 100644 --- a/spec/rails_cloud_tasks/instrumentation/new_relic_spec.rb +++ b/spec/rails_cloud_tasks/instrumentation/new_relic_spec.rb @@ -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) } @@ -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 diff --git a/spec/rails_cloud_tasks/rack/jobs_spec.rb b/spec/rails_cloud_tasks/rack/jobs_spec.rb index b47d2bb..06f3481 100644 --- a/spec/rails_cloud_tasks/rack/jobs_spec.rb +++ b/spec/rails_cloud_tasks/rack/jobs_spec.rb @@ -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 @@ -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') } diff --git a/spec/rails_cloud_tasks/rack/tasks_spec.rb b/spec/rails_cloud_tasks/rack/tasks_spec.rb index 4ecf499..7f67b3e 100644 --- a/spec/rails_cloud_tasks/rack/tasks_spec.rb +++ b/spec/rails_cloud_tasks/rack/tasks_spec.rb @@ -6,6 +6,8 @@ { 'rack.input' => StringIO.new(payload.to_json) } end + let(:arguments) { [123] } + let(:payload) do { job: { @@ -14,7 +16,7 @@ provider_job_id: nil, queue_name: 'test-queue', priority: nil, - arguments: [123], + arguments: arguments, executions: 0, locale: 'en' } @@ -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 @@ -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') } diff --git a/spec/rails_cloud_tasks/version_spec.rb b/spec/rails_cloud_tasks/version_spec.rb index c7365ca..afd86be 100644 --- a/spec/rails_cloud_tasks/version_spec.rb +++ b/spec/rails_cloud_tasks/version_spec.rb @@ -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