Module: Datadog::Contrib::Redis::Patcher

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/redis/patcher.rb

Overview

Patcher enables patching of ‘redis’ module.

Class Method Summary collapse

Methods included from Patcher

included

Class Method Details

.patchObject

patch applies our patch if needed



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 18

def patch
  do_once(:redis) do
    begin
      # do not require these by default, but only when actually patching
      require 'redis'
      require 'ddtrace/ext/app_types'
      require 'ddtrace/contrib/redis/tags'
      require 'ddtrace/contrib/redis/quantize'

      patch_redis_client
    rescue StandardError => e
      Datadog::Tracer.log.error("Unable to apply Redis integration: #{e}")
    end
  end
end

.patch_redis_clientObject

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/BlockLength



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 36

def patch_redis_client
  ::Redis::Client.class_eval do
    alias_method :call_without_datadog, :call
    remove_method :call
    def call(*args, &block)
      pin = Datadog::Pin.get_from(self)
      return call_without_datadog(*args, &block) unless pin && pin.tracer

      response = nil
      pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_COMMAND) do |span|
        span.service = pin.service
        span.span_type = Datadog::Contrib::Redis::Ext::TYPE
        span.resource = Datadog::Contrib::Redis::Quantize.format_command_args(*args)
        Datadog::Contrib::Redis::Tags.set_common_tags(self, span)

        response = call_without_datadog(*args, &block)
      end

      response
    end

    alias_method :call_pipeline_without_datadog, :call_pipeline
    remove_method :call_pipeline
    def call_pipeline(*args, &block)
      pin = Datadog::Pin.get_from(self)
      return call_pipeline_without_datadog(*args, &block) unless pin && pin.tracer

      response = nil
      pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_COMMAND) do |span|
        span.service = pin.service
        span.span_type = Datadog::Contrib::Redis::Ext::TYPE
        commands = args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.format_command_args(c) }
        span.resource = commands.join("\n")
        Datadog::Contrib::Redis::Tags.set_common_tags(self, span)
        span.set_metric Datadog::Contrib::Redis::Ext::METRIC_PIPELINE_LEN, commands.length

        response = call_pipeline_without_datadog(*args, &block)
      end

      response
    end

    def datadog_pin
      @datadog_pin ||= begin
        pin = Datadog::Pin.new(
          Datadog.configuration[:redis][:service_name],
          app: Ext::APP,
          app_type: Datadog::Ext::AppTypes::DB,
          tracer: Datadog.configuration[:redis][:tracer]
        )
        pin.onto(self)
      end
    end
  end
end

.patched?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 13

def patched?
  done?(:redis)
end