Sha256: 8574870de39b0ef2e0b07ef415c12c1e5c52c5c1cc276288ee7b5d371136ea74

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

require 'ddtrace/ext/manual_tracing'
require 'ddtrace/ext/priority'

module Datadog
  # Defines analytics behavior
  module ForcedTracing
    class << self
      def keep(span)
        return if span.nil? || span.context.nil?
        span.context.sampling_priority = Datadog::Ext::Priority::USER_KEEP
      end

      def drop(span)
        return if span.nil? || span.context.nil?
        span.context.sampling_priority = Datadog::Ext::Priority::USER_REJECT
      end
    end

    # Extension for Datadog::Span
    module Span
      def self.included(base)
        if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
          base.class_eval do
            # Instance methods
            include InstanceMethodsCompatibility
            include InstanceMethods
          end
        else
          base.send(:prepend, InstanceMethods)
        end
      end

      # Compatibility shim for Rubies not supporting `.prepend`
      # NOTE: This patching strategy only works because it has been applied after
      #       Datadog::Analytics::Span. Any changes will likely break this.
      module InstanceMethodsCompatibility
        def self.included(base)
          base.send(:alias_method, :set_tag_without_forced_tracing, :set_tag)

          define_method :set_tag do |*args, &block|
            set_tag_without_forced_tracing(*args, &block)
          end
        end
      end

      # Instance methods
      module InstanceMethods
        def set_tag(key, value)
          # Configure sampling priority if they give us a forced tracing tag
          # DEV: Do not set if the value they give us is explicitly "false"
          case key
          when Ext::ManualTracing::TAG_KEEP
            ForcedTracing.keep(self) unless value == false
          when Ext::ManualTracing::TAG_DROP
            ForcedTracing.drop(self) unless value == false
          else
            # Otherwise, set the tag normally.
            super if defined?(super)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ddtrace-0.26.1 lib/ddtrace/forced_tracing.rb
ddtrace-0.26.0 lib/ddtrace/forced_tracing.rb
ddtrace-0.25.1 lib/ddtrace/forced_tracing.rb
ddtrace-0.25.0 lib/ddtrace/forced_tracing.rb