Sha256: 887311b72023c32570e0679ffd699f34687b2f58530f99584a9cc72a7a6b9acc

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module PrometheusExporter::Instrumentation
  class DelayedJob
    JOB_CLASS_REGEXP = %r{job_class: (\w+:{0,2})+}.freeze

    class << self
      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
    end

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

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prometheus_exporter-0.3.1 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.3.0 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.2.0 lib/prometheus_exporter/instrumentation/delayed_job.rb