Class: PrometheusExporter::Instrumentation::DelayedJob

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

Constant Summary collapse

JOB_CLASS_REGEXP =
%r{job_class: (\w+:{0,2})+}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ DelayedJob

Returns a new instance of DelayedJob.



22
23
24
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 22

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

Class Method Details

.register_plugin(client: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 6

def register_plugin(client: nil)
  instrumenter = self.new(client: client)
  return unless defined?(Delayed::Plugin)

  plugin = Class.new(Delayed::Plugin) do
    callbacks do |lifecycle|
      lifecycle.around(:invoke_job) do |job, *args, &block|
        instrumenter.call(job, *args, &block)
      end
    end
  end

  Delayed::Worker.plugins << plugin
end

Instance Method Details

#call(job, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/prometheus_exporter/instrumentation/delayed_job.rb', line 26

def call(job, *args, &block)
  success = false
  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  result = block.call(job, *args)
  success = true
  result
ensure
  duration = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start

  @client.send_json(
    type: "delayed_job",
    name: job.handler.to_s.match(JOB_CLASS_REGEXP).to_a[1].to_s,
    success: success,
    duration: duration
  )
end