Skip to content

Commit

Permalink
Track the number of executions of Active Job jobs (#1077)
Browse files Browse the repository at this point in the history
When a job is retried, the execution count goes up every time. To give
more insights into how many times a job has been retried, track the
executions as a tag.

Executions are zero when the job is run the first time for some reason,
so +1 the value so it makes more sense to a human being reading it.
Calling `to_i` on the job metadata just to make sure we don't error on
any weird value type.
  • Loading branch information
tombruijn authored May 8, 2024
1 parent 27db8d6 commit d6d233d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changesets/track-active-job-executions-per-job.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Track Active Job executions per job. When a job is retried the "executions" metadata for Active Job jobs goes up by one for every retry. We now track this as the `executions` tag on the job sample.
2 changes: 2 additions & 0 deletions lib/appsignal/hooks/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def self.transaction_tags_for(job)
tags[:queue] = queue if queue
priority = job["priority"]
tags[:priority] = priority if priority
executions = job["executions"]
tags[:executions] = executions.to_i + 1 if executions
tags
end

Expand Down
61 changes: 50 additions & 11 deletions spec/lib/appsignal/hooks/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def perform(*_args)
"params" => [],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => queue
"queue" => queue,
"executions" => 1
}
)
)
Expand Down Expand Up @@ -220,7 +221,8 @@ def perform(*_args)
"params" => [],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => queue
"queue" => queue,
"executions" => 1
}
)
)
Expand Down Expand Up @@ -269,7 +271,14 @@ def perform(*_args)

transaction = last_transaction
transaction_hash = transaction.to_h
expect(transaction_hash).to include("error" => nil)
expect(transaction_hash).to include(
"error" => nil,
"sample_data" => hash_including(
"tags" => hash_including(
"executions" => 1
)
)
)
end

it "reports error when discarding the job" do
Expand All @@ -291,7 +300,12 @@ def perform(*_args)
"name" => "RuntimeError",
"message" => "uh oh",
"backtrace" => kind_of(String)
}
},
"sample_data" => hash_including(
"tags" => hash_including(
"executions" => 2
)
)
)
end
end
Expand Down Expand Up @@ -341,6 +355,24 @@ def perform(*_args)
end
end

context "with retries" do
it "reports the number of retries as executions" do
with_test_adapter do
expect do
queue_job(ActiveJobErrorWithRetryTestJob)
end.to raise_error(RuntimeError, "uh oh")
end

transaction = last_transaction
transaction_hash = transaction.to_h
expect(transaction_hash).to include(
"sample_data" => hash_including(
"tags" => hash_including("executions" => 2)
)
)
end
end

context "when wrapped in another transaction" do
it "does not create a new transaction or close the currently open one" do
current_transaction = background_job_transaction
Expand All @@ -366,7 +398,8 @@ def perform(*_args)
"params" => [],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => queue
"queue" => queue,
"executions" => 1
}
)
)
Expand Down Expand Up @@ -509,7 +542,8 @@ def welcome(_first_arg = nil, _second_arg = nil)
"deliver_now"] + active_job_args_wrapper,
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand All @@ -529,7 +563,8 @@ def welcome(_first_arg = nil, _second_arg = nil)
"deliver_now"] + active_job_args_wrapper(:args => method_expected_args),
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand All @@ -553,7 +588,8 @@ def welcome(_first_arg = nil, _second_arg = nil)
] + active_job_args_wrapper(:params => parameterized_expected_args),
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand Down Expand Up @@ -594,7 +630,8 @@ def welcome(*_args)
],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand All @@ -620,7 +657,8 @@ def welcome(*_args)
],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand Down Expand Up @@ -648,7 +686,8 @@ def welcome(*_args)
],
"tags" => {
"active_job_id" => kind_of(String),
"queue" => "mailers"
"queue" => "mailers",
"executions" => 1
}
)
)
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/appsignal/integrations/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def expect_transaction_to_have_sidekiq_event(transaction_hash)
end
end
let(:expected_tags) do
{}.tap do |hash|
{ "executions" => 1 }.tap do |hash|
hash["active_job_id"] = kind_of(String)
if DependencyHelper.rails_version >= Gem::Version.new("5.0.0")
hash["provider_job_id"] = kind_of(String)
Expand Down

0 comments on commit d6d233d

Please sign in to comment.