Sha256: 317ac66a9353cfff9ad6ca716b0ff17d528706cd705bf2fe66d55b793e41a964

Contents?: true

Size: 1.66 KB

Versions: 9

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

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|
              max_attempts = Delayed::Worker.max_attempts
              enqueued_count = Delayed::Job.count
              pending_count = Delayed::Job.where(attempts: 0, locked_at: nil).count
              instrumenter.call(job, max_attempts, enqueued_count, pending_count, *args, &block)
            end
          end
        end

        Delayed::Worker.plugins << plugin
      end
    end

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

    def call(job, max_attempts, enqueued_count, pending_count, *args, &block)
      success = false
      start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
      attempts = job.attempts + 1 # Increment because we're adding the current attempt
      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,
        attempts: attempts,
        max_attempts: max_attempts,
        enqueued: enqueued_count,
        pending: pending_count
      )
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
prometheus_exporter-0.7.0 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.6.0 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.5.3 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.5.2 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.5.1 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.5.0 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.17 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.16 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.15 lib/prometheus_exporter/instrumentation/delayed_job.rb