Sha256: 8027356613019180f1bfa66b7f9e5d46759316a6c4ec22d0d15bc819738598a7

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

require 'ddtrace/contrib/sidekiq/tracing'
require 'ddtrace/contrib/analytics'

module Datadog
  module Contrib
    module Sidekiq
      # Tracer is a Sidekiq server-side middleware which traces executed jobs
      class ServerTracer
        include Tracing

        def initialize(options = {})
          super
          @sidekiq_service = options[:service_name] || configuration[:service_name]
        end

        def call(worker, job, queue)
          resource = job_resource(job)

          service = service_from_worker_config(resource) || @sidekiq_service
          set_service_info(service)

          @tracer.trace(Ext::SPAN_JOB, service: service, span_type: Datadog::Ext::AppTypes::WORKER) do |span|
            span.resource = resource
            # Set analytics sample rate
            if Contrib::Analytics.enabled?(configuration[:analytics_enabled])
              Contrib::Analytics.set_sample_rate(span, configuration[:analytics_sample_rate])
            end
            span.set_tag(Ext::TAG_JOB_ID, job['jid'])
            span.set_tag(Ext::TAG_JOB_RETRY, job['retry'])
            span.set_tag(Ext::TAG_JOB_QUEUE, job['queue'])
            span.set_tag(Ext::TAG_JOB_WRAPPER, job['class']) if job['wrapped']
            span.set_tag(Ext::TAG_JOB_DELAY, 1000.0 * (Time.now.utc.to_f - job['enqueued_at'].to_f))

            yield
          end
        end

        private

        def configuration
          Datadog.configuration[:sidekiq]
        end

        def service_from_worker_config(resource)
          # Try to get the Ruby class from the resource name.
          worker_klass = begin
            Object.const_get(resource)
          rescue NameError
            nil
          end

          if worker_klass.respond_to?(:datadog_tracer_config)
            worker_klass.datadog_tracer_config[:service_name]
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.22.0 lib/ddtrace/contrib/sidekiq/server_tracer.rb
ddtrace-0.21.2 lib/ddtrace/contrib/sidekiq/server_tracer.rb
ddtrace-0.21.1 lib/ddtrace/contrib/sidekiq/server_tracer.rb
ddtrace-0.21.0 lib/ddtrace/contrib/sidekiq/server_tracer.rb
ddtrace-0.20.0 lib/ddtrace/contrib/sidekiq/server_tracer.rb