Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ GEM

PLATFORMS
arm64-darwin-23
arm64-darwin-24
x86_64-darwin-21
x86_64-linux

Expand Down
10 changes: 5 additions & 5 deletions lib/yabeda/activejob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.install!
tags: %i[queue activejob executions],
buckets: LONG_RUNNING_JOB_RUNTIME_BUCKETS

histogram :latency, comment: "The job latency, the difference in seconds between enqueued and running time",
histogram :latency, comment: "The job latency, the difference in seconds between scheduled and running time",
unit: :seconds, per: :activejob,
tags: %i[queue activejob executions],
buckets: LONG_RUNNING_JOB_RUNTIME_BUCKETS
Expand Down Expand Up @@ -93,13 +93,13 @@ def self.install!
# rubocop: enable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize

def self.job_latency(event)
enqueue_time = event.payload[:job].enqueued_at
return nil unless enqueue_time.present?
scheduled_time = event.payload[:job].scheduled_at || event.payload[:job].enqueued_at
return nil unless scheduled_time.present?

enqueue_time = parse_event_time(enqueue_time)
scheduled_time = parse_event_time(scheduled_time)
perform_at_time = parse_event_time(event.end)

perform_at_time - enqueue_time
perform_at_time - scheduled_time
end

def self.ms2s(milliseconds)
Expand Down
58 changes: 50 additions & 8 deletions spec/yabeda/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,78 @@

describe "#job_latency" do
# Rails 7.1.4 and above
it "returns the correct latency from end_time in seconds" do
start_time = Time.now
it "returns the correct latency from end_time in seconds for job without a wait time" do
enqueued_time = Time.now
job = HelloJob.new
job.enqueued_at = start_time
job.enqueued_at = enqueued_time
event = ActiveSupport::Notifications::Event.new(
"perform_start.active_job",
nil,
nil,
1,
{ job: job },
)
end_time_in_s = 1.minute.from_now(start_time).to_f
end_time_in_s = 1.minute.from_now(enqueued_time).to_f
allow(event).to receive(:end).and_return(end_time_in_s)

expect(described_class.job_latency(event)).to be_within(0.1).of(60.0)
end

# Rails 7.1.4 and above
it "returns the correct latency from end_time in seconds for job with wait time" do
enqueued_time = Time.now
wait = 1.minute
scheduled_time = enqueued_time + wait
job = HelloJob.new
job.enqueued_at = enqueued_time
job.scheduled_at = scheduled_time
event = ActiveSupport::Notifications::Event.new(
"perform_start.active_job",
nil,
nil,
1,
{ job: job },
)
end_time_in_s = 1.minute.from_now(scheduled_time).to_f
allow(event).to receive(:end).and_return(end_time_in_s)

expect(described_class.job_latency(event)).to be_within(0.1).of(60.0)
end

# Rails 7.1.3 and below
it "returns the correct latency from end_time in milliseconds for job without a wait time" do
enqueued_time = Time.now
job = HelloJob.new
job.enqueued_at = enqueued_time
event = ActiveSupport::Notifications::Event.new(
"perform_start.active_job",
nil,
nil,
1,
{ job: job },
)
end_time_in_ms = 1.minute.from_now(enqueued_time).to_f * 1000
allow(event).to receive(:end).and_return(end_time_in_ms)

expect(described_class.job_latency(event)).to be_within(0.1).of(60.0)
end

# Rails 7.1.3 and below
it "returns the correct latency from end_time in milliseconds" do
start_time = Time.now
it "returns the correct latency from end_time in milliseconds for job with a wait time" do
enqueued_time = Time.now
wait = 1.minute
scheduled_time = enqueued_time + wait
job = HelloJob.new
job.enqueued_at = start_time
job.enqueued_at = enqueued_time
job.scheduled_at = scheduled_time
event = ActiveSupport::Notifications::Event.new(
"perform_start.active_job",
nil,
nil,
1,
{ job: job },
)
end_time_in_ms = 1.minute.from_now(start_time).to_f * 1000
end_time_in_ms = 1.minute.from_now(scheduled_time).to_f * 1000
allow(event).to receive(:end).and_return(end_time_in_ms)

expect(described_class.job_latency(event)).to be_within(0.1).of(60.0)
Expand Down
Loading