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(options = { client: nil }) ⇒ Sidekiq

Returns a new instance of Sidekiq.



46
47
48
49
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 46

def initialize(options = { client: nil })
  @client =
    options.fetch(:client, nil) || PrometheusExporter::Client.default
end

Class Method Details

.death_handlerObject



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

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, job)

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

.get_worker_custom_labels(worker_class, msg) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 33

def self.get_worker_custom_labels(worker_class, msg)
  return {} unless worker_class.respond_to?(:custom_labels)

  # TODO remove when version 3.0.0 is released
  method_arity = worker_class.method(:custom_labels).arity

  if method_arity > 0
    worker_class.custom_labels(msg)
  else
    worker_class.custom_labels
  end
end

Instance Method Details

#call(worker, msg, queue) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prometheus_exporter/instrumentation/sidekiq.rb', line 51

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: self.class.get_name(worker.class.to_s, msg),
    queue: queue,
    success: success,
    shutdown: shutdown,
    duration: duration,
    custom_labels: self.class.get_worker_custom_labels(worker.class, msg)
  )
end