Class: PrometheusExporter::Instrumentation::Sidekiq

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/instrumentation/sidekiq.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Sidekiq

Returns a new instance of Sidekiq.



36
37
38
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 36

def initialize(client: nil)
  @client = client || PrometheusExporter::Client.default
end

Class Method Details

.death_handlerObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 14

def self.death_handler
  -> (job, ex) do
    job_is_fire_and_forget = job["retry"] == false

    worker_class = Object.const_get(job["class"])
    worker_custom_labels = self.get_worker_custom_labels(worker_class)

    unless job_is_fire_and_forget
      PrometheusExporter::Client.default.send_json(
        type: "sidekiq",
        name: job["class"],
        dead: true,
        custom_labels: worker_custom_labels
      )
    end
  end
end

.get_worker_custom_labels(worker_class) ⇒ Object



32
33
34
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 32

def self.get_worker_custom_labels(worker_class)
  worker_class.respond_to?(:custom_labels) ? worker_class.custom_labels : {}
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 40

def call(worker, msg, queue)
  success = false
  shutdown = false
  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  result = yield
  success = true
  result
rescue ::Sidekiq::Shutdown => e
  shutdown = true
  raise e
ensure
  duration = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start
  @client.send_json(
    type: "sidekiq",
    name: get_name(worker, msg),
    queue: queue,
    success: success,
    shutdown: shutdown,
    duration: duration,
    custom_labels: self.class.get_worker_custom_labels(worker.class)
  )
end