module Datadog::Contrib::SuckerPunch::Instrumentation

Defines instrumentation patches for the `sucker_punch` gem

Public Instance Methods

__run_perform(*args) click to toggle source
# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 19
def __run_perform(*args)
  pin = Datadog::Pin.get_from(::SuckerPunch)
  pin.tracer.provider.context = Datadog::Context.new

  __with_instrumentation(Ext::SPAN_PERFORM) do |span|
    span.resource = "PROCESS #{self}"

    # Set analytics sample rate
    if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
      Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate])
    end

    # Measure service stats
    Contrib::Analytics.set_measured(span)

    __run_perform_without_datadog(*args)
  end
rescue => e
  ::SuckerPunch.__exception_handler.call(e, self, args)
end
__with_instrumentation(name) { |span| ... } click to toggle source
# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 74
def __with_instrumentation(name)
  pin = Datadog::Pin.get_from(::SuckerPunch)

  pin.tracer.trace(name) do |span|
    span.service = pin.service
    span.span_type = pin.app_type
    span.set_tag(Ext::TAG_QUEUE, to_s)
    yield span
  end
end
datadog_configuration() click to toggle source
# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 70
def datadog_configuration
  Datadog.configuration[:sucker_punch]
end
patch!() click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 15
def patch!
  # rubocop:disable Metrics/BlockLength
  ::SuckerPunch::Job::ClassMethods.class_eval do
    alias_method :__run_perform_without_datadog, :__run_perform
    def __run_perform(*args)
      pin = Datadog::Pin.get_from(::SuckerPunch)
      pin.tracer.provider.context = Datadog::Context.new

      __with_instrumentation(Ext::SPAN_PERFORM) do |span|
        span.resource = "PROCESS #{self}"

        # Set analytics sample rate
        if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
          Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate])
        end

        # Measure service stats
        Contrib::Analytics.set_measured(span)

        __run_perform_without_datadog(*args)
      end
    rescue => e
      ::SuckerPunch.__exception_handler.call(e, self, args)
    end
    ruby2_keywords :__run_perform if respond_to?(:ruby2_keywords, true)

    alias_method :__perform_async, :perform_async
    def perform_async(*args)
      __with_instrumentation(Ext::SPAN_PERFORM_ASYNC) do |span|
        span.resource = "ENQUEUE #{self}"

        # Measure service stats
        Contrib::Analytics.set_measured(span)

        __perform_async(*args)
      end
    end
    ruby2_keywords :perform_async if respond_to?(:ruby2_keywords, true)

    alias_method :__perform_in, :perform_in
    def perform_in(interval, *args)
      __with_instrumentation(Ext::SPAN_PERFORM_IN) do |span|
        span.resource = "ENQUEUE #{self}"
        span.set_tag(Ext::TAG_PERFORM_IN, interval)

        # Measure service stats
        Contrib::Analytics.set_measured(span)

        __perform_in(interval, *args)
      end
    end
    ruby2_keywords :perform_in if respond_to?(:ruby2_keywords, true)

    private

    def datadog_configuration
      Datadog.configuration[:sucker_punch]
    end

    def __with_instrumentation(name)
      pin = Datadog::Pin.get_from(::SuckerPunch)

      pin.tracer.trace(name) do |span|
        span.service = pin.service
        span.span_type = pin.app_type
        span.set_tag(Ext::TAG_QUEUE, to_s)
        yield span
      end
    end
  end
end
perform_async(*args) click to toggle source
# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 42
def perform_async(*args)
  __with_instrumentation(Ext::SPAN_PERFORM_ASYNC) do |span|
    span.resource = "ENQUEUE #{self}"

    # Measure service stats
    Contrib::Analytics.set_measured(span)

    __perform_async(*args)
  end
end
perform_in(interval, *args) click to toggle source
# File lib/ddtrace/contrib/sucker_punch/instrumentation.rb, line 55
def perform_in(interval, *args)
  __with_instrumentation(Ext::SPAN_PERFORM_IN) do |span|
    span.resource = "ENQUEUE #{self}"
    span.set_tag(Ext::TAG_PERFORM_IN, interval)

    # Measure service stats
    Contrib::Analytics.set_measured(span)

    __perform_in(interval, *args)
  end
end