Module: Yabeda::Shoryuken

Defined in:
lib/yabeda/shoryuken.rb,
lib/yabeda/shoryuken/config.rb,
lib/yabeda/shoryuken/version.rb,
lib/yabeda/shoryuken/client_middleware.rb,
lib/yabeda/shoryuken/server_middleware.rb

Overview

Yabeda Shoryuken integration

Defined Under Namespace

Classes: ClientMiddleware, Config, ServerMiddleware

Constant Summary collapse

LONG_RUNNING_JOB_RUNTIME_BUCKETS =
[
  0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard (from Prometheus)
  30, 60, 120, 300, 1800, 3600, 21_600, # Sidekiq tasks may be very long-running
].freeze
VERSION =
'0.1.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.jobs_started_atObject

Hash of hashes containing all currently running jobs’ start timestamps to calculate maximum durations of currently running not yet completed jobs { { queue: “default”, worker: “SomeJob” } => { “jid1” => 100500, “jid2” => 424242 } }



97
98
99
# File 'lib/yabeda/shoryuken.rb', line 97

def jobs_started_at
  @jobs_started_at
end

Class Method Details

.configObject



19
20
21
# File 'lib/yabeda/shoryuken.rb', line 19

def self.config
  @config ||= Config.new
end

.custom_tags(worker, sqs_msg) ⇒ Object



88
89
90
91
92
# File 'lib/yabeda/shoryuken.rb', line 88

def custom_tags(worker, sqs_msg)
  return {} unless worker.respond_to?(:yabeda_tags)

  worker.method(:yabeda_tags).arity.zero? ? worker.yabeda_tags : worker.yabeda_tags(*sqs_msg.attributes)
end

.labelize(worker_class, sqs_msg, queue, body = nil) ⇒ Object



69
70
71
# File 'lib/yabeda/shoryuken.rb', line 69

def labelize(worker_class, sqs_msg, queue, body = nil)
  { queue: queue, worker: worker_name(worker_class, sqs_msg, body) }
end

.track_max_job_runtimeObject



99
100
101
102
103
104
105
106
# File 'lib/yabeda/shoryuken.rb', line 99

def track_max_job_runtime
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  ::Yabeda::Shoryuken.jobs_started_at.each do |labels, jobs|
    oldest_job_started_at = jobs.values.min
    oldest_job_duration = oldest_job_started_at ? (now - oldest_job_started_at).round(3) : 0
    Yabeda.shoryuken.running_job_runtime.set(labels, oldest_job_duration)
  end
end

.worker_name(worker_class, sqs_msg, body = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yabeda/shoryuken.rb', line 73

def worker_name(worker_class, sqs_msg, body = nil)
  if ::Shoryuken.active_job? \
    && !sqs_msg.is_a?(Array) \
    && sqs_msg.message_attributes \
    && sqs_msg.message_attributes['shoryuken_class'] \
    && sqs_msg.message_attributes['shoryuken_class'][:string_value] \
    == ActiveJob::QueueAdapters::ShoryukenAdapter::JobWrapper.to_s \
    && body

    "ActiveJob/#{body['job_class']}"
  else
    worker_class.to_s
  end
end