Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: flamingo-run/rails-cloud-tasks
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: edukorg/rails-cloud-tasks
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 10 files changed
  • 1 contributor

Commits on Dec 14, 2021

  1. Copy the full SHA
    8bddb09 View commit details
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/rack/jobs.rb
Original file line number Diff line number Diff line change
@@ -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, {})
4 changes: 4 additions & 0 deletions lib/rails_cloud_tasks/rack/tasks.rb
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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
7 changes: 7 additions & 0 deletions spec/rails_cloud_tasks/rack/jobs_spec.rb
Original file line number Diff line number Diff line change
@@ -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') }
11 changes: 10 additions & 1 deletion spec/rails_cloud_tasks/rack/tasks_spec.rb
Original file line number Diff line number Diff line change
@@ -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') }
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