Sha256: d03e9f524652466c75bef3bf529570a424c3c90a07114fd2e6decd4a2ce29bb2

Contents?: true

Size: 1.88 KB

Versions: 22

Compression:

Stored size: 1.88 KB

Contents

require 'ddtrace/contrib/analytics'
require 'ddtrace/contrib/active_support/notifications/event'
require 'ddtrace/contrib/action_cable/ext'

module Datadog
  module Contrib
    module ActionCable
      # Defines basic behaviors for an event.
      module Event
        def self.included(base)
          base.send(:include, ActiveSupport::Notifications::Event)
          base.send(:extend, ClassMethods)
        end

        # Class methods for events.
        module ClassMethods
          def span_options
            { service: configuration[:service_name] }
          end

          def tracer
            -> { configuration[:tracer] }
          end

          def configuration
            Datadog.configuration[:action_cable]
          end
        end
      end

      # Defines behavior for the first event of a thread execution.
      #
      # This event is not expected to be nested with other event,
      # but to start a fresh tracing context.
      module RootContextEvent
        def self.included(base)
          base.send(:include, ActiveSupport::Notifications::Event)
          base.send(:extend, ClassMethods)
        end

        # Class methods for events.
        module ClassMethods
          include Contrib::ActionCable::Event::ClassMethods

          def subscription(*args)
            super.tap do |subscription|
              subscription.before_trace { ensure_clean_context! }
            end
          end

          private

          # Context objects are thread-bound.
          # If an integration re-uses threads, context from a previous trace
          # could leak into the new trace. This "cleans" current context,
          # preventing such a leak.
          def ensure_clean_context!
            return unless configuration[:tracer].call_context.current_span
            configuration[:tracer].provider.context = Context.new
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
ddtrace-0.45.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.44.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.43.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.42.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.41.0 lib/ddtrace/contrib/action_cable/event.rb
ls-trace-0.2.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.40.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.39.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.38.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.37.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.36.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.35.2 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.35.1 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.35.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.34.2 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.34.1 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.34.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.33.1 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.33.0 lib/ddtrace/contrib/action_cable/event.rb
ddtrace-0.32.0 lib/ddtrace/contrib/action_cable/event.rb