Sha256: 0cdcfdfd169d7d190e26e1ff19f537e7a8d98bdc728a7f9cc74feae5cabfcccc

Contents?: true

Size: 1.94 KB

Versions: 27

Compression:

Stored size: 1.94 KB

Contents

require 'ddtrace/ext/app_types'
require 'ddtrace/sync_writer'
require 'ddtrace/contrib/analytics'
require 'ddtrace/contrib/sidekiq/ext'
require 'resque'

module Datadog
  module Contrib
    module Resque
      # Uses Resque job hooks to create traces
      module ResqueJob
        def around_perform(*_)
          return yield unless datadog_configuration && tracer

          tracer.trace(Ext::SPAN_JOB, span_options) do |span|
            span.resource = name
            span.span_type = Datadog::Ext::AppTypes::WORKER
            # Set analytics sample rate
            if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
              Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate])
            end
            yield
          end
        end

        def after_perform_shutdown_tracer(*_)
          shutdown_tracer_when_forked!
        end

        def on_failure_shutdown_tracer(*_)
          shutdown_tracer_when_forked!
        end

        def shutdown_tracer_when_forked!
          tracer.shutdown! if forked?
        end

        private

        def forked?
          pin = Datadog::Pin.get_from(::Resque)
          return false unless pin
          pin.config[:forked] == true
        end

        def span_options
          { service: datadog_configuration[:service_name] }
        end

        def tracer
          datadog_configuration.tracer
        end

        def datadog_configuration
          Datadog.configuration[:resque]
        end
      end
    end
  end
end

Resque.after_fork do
  configuration = Datadog.configuration[:resque]
  next if configuration.nil?

  # Add a pin, marking the job as forked.
  # Used to trigger shutdown in forks for performance reasons.
  Datadog::Pin.new(
    configuration[:service_name],
    config: { forked: true }
  ).onto(::Resque)

  # Clean the state so no CoW happens
  next if configuration[:tracer].nil?
  configuration[:tracer].provider.context = nil
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
ls-trace-0.1.2 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.33.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.33.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.32.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.31.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.31.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.30.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.30.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.29.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.26.1 lib/ddtrace/contrib/resque/resque_job.rb
ls-trace-0.1.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.29.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.28.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.27.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.26.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.25.1 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.25.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.24.0 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.23.3 lib/ddtrace/contrib/resque/resque_job.rb
ddtrace-0.23.2 lib/ddtrace/contrib/resque/resque_job.rb