Sha256: 125dff3a181ba1b1191a913e6274dfdab954aae08c8af9dc1310d5222a99ce25

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 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|
              max_attempts = Delayed::Worker.max_attempts
              instrumenter.call(job, max_attempts, *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, *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
      )
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
prometheus_exporter-0.4.10 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.9 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.8 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.7 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.6 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.5 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.4 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.3 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.2 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.1 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.4.0 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.3.4 lib/prometheus_exporter/instrumentation/delayed_job.rb
prometheus_exporter-0.3.3 lib/prometheus_exporter/instrumentation/delayed_job.rb