Sha256: c2fbd68ca83b234031577a0294a9b9ebb182b72b77bb87a816ddde701a2029c4

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# typed: true

require 'concurrent/executor/executor_service'

module Datadog
  module Tracing
    module Contrib
      module ConcurrentRuby
        # wraps existing executor to carry over trace context
        class ContextCompositeExecutorService
          extend Forwardable
          include Concurrent::ExecutorService

          attr_accessor :composited_executor

          def initialize(composited_executor)
            @composited_executor = composited_executor
          end

          # post method runs the task within composited executor - in a different thread
          def post(*args, &task)
            tracer = Tracing.send(:tracer)
            parent_context = tracer.provider.context

            @composited_executor.post(*args) do
              begin
                original_context = tracer.provider.context
                tracer.provider.context = parent_context
                yield
              ensure
                # Restore context in case the current thread gets reused
                tracer.provider.context = original_context
              end
            end
          end

          def datadog_configuration
            Datadog.configuration.tracing[:concurrent_ruby]
          end

          delegate [:can_overflow?, :serialized?] => :composited_executor
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddtrace-1.0.0 lib/datadog/tracing/contrib/concurrent_ruby/context_composite_executor_service.rb
ddtrace-1.0.0.beta2 lib/datadog/tracing/contrib/concurrent_ruby/context_composite_executor_service.rb