-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API-26059 Log durations of some sidekiq jobs (#12474)
* API-26059 Adds generic way to opt a job into benchmark logging
- Loading branch information
1 parent
882dd03
commit 2a0bead
Showing
7 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'benchmark' | ||
|
||
module Sidekiq | ||
module BenchmarkLoggingWorker | ||
# This module serves to mark workers which should log benchmarking information. | ||
# Actual logging takes place in Sidekiq::Middleware::BenchmarkLoggingMiddleware. | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'benchmark' | ||
|
||
module Sidekiq::Middleware | ||
class BenchmarkLoggingMiddleware | ||
# include Sidekiq::BenchmarkLoggingWorker in your sidekiq job to apply this middleware to it | ||
def call(worker, _job, _queue, &block) | ||
if worker.is_a?(Sidekiq::BenchmarkLoggingWorker) | ||
m = Benchmark.measure(&block) | ||
data = { real: m.real.round, system: m.stime.round, total: m.total.round, user: m.utime.round } | ||
Rails.logger.info "Sidekiq job #{worker.class.name} benchmark: #{data.to_json}" | ||
else | ||
block.call | ||
end | ||
end | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
modules/appeals_api/app/workers/appeals_api/flipper_status_alert.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
modules/va_forms/app/workers/va_forms/flipper_status_alert.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
modules/vba_documents/app/workers/vba_documents/flipper_status_alert.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
spec/lib/sidekiq/middleware/benchmark_logging_middleware_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'sidekiq/benchmark_logging_worker' | ||
require 'sidekiq/middleware/benchmark_logging_middleware' | ||
|
||
class NormalTestWorker | ||
include Sidekiq::Worker | ||
|
||
def perform; end | ||
end | ||
|
||
class BenchmarkLoggingTestWorker < NormalTestWorker | ||
include Sidekiq::BenchmarkLoggingWorker | ||
end | ||
|
||
RSpec.describe Sidekiq::Middleware::BenchmarkLoggingMiddleware do | ||
let(:worker) { NormalTestWorker.new } | ||
let(:job) { {} } | ||
let(:queue) { 'default' } | ||
let(:middleware) { described_class.new } | ||
|
||
describe '#call' do | ||
context 'when the job does not include the benchmark logging module' do | ||
it 'does not log benchmark data' do | ||
expect(Rails.logger).not_to receive(:info) | ||
|
||
middleware.call(worker, job, queue) {} | ||
end | ||
end | ||
|
||
context 'when the job includes the benchmark logging module' do | ||
let(:worker) { BenchmarkLoggingTestWorker.new } | ||
let(:flag_name) { :sidekiq_benchmark_logging } | ||
|
||
it 'logs benchmark data' do | ||
expect(Rails.logger).to receive(:info) do |message| | ||
expect(message) | ||
.to eq('Sidekiq job BenchmarkLoggingTestWorker benchmark: {"real":0,"system":0,"total":0,"user":0}') | ||
end | ||
|
||
middleware.call(worker, job, queue) {} | ||
end | ||
end | ||
end | ||
end |