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

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

Overview

Patcher enables patching of ‘redis’ module. This is used in monkey.rb to automatically apply patches

Class Method Summary collapse

Class Method Details

.patchObject

patch applies our patch if needed



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

def patch
  if !@patched && (defined?(::Redis::VERSION) && \
                   Gem::Version.new(::Redis::VERSION) >= Gem::Version.new('3.0.0'))
    begin
      # do not require these by default, but only when actually patching
      require 'ddtrace/monkey'
      require 'ddtrace/ext/app_types'
      require 'ddtrace/contrib/redis/tags'
      require 'ddtrace/contrib/redis/quantize'

      patch_redis()
      patch_redis_client()

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

.patch_redisObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 38

def patch_redis
  ::Redis.module_eval do
    def datadog_pin=(pin)
      # Forward the pin to client, which actually traces calls.
      Datadog::Pin.onto(client, pin)
    end

    def datadog_pin
      # Get the pin from client, which actually traces calls.
      Datadog::Pin.get_from(client)
    end
  end
end

.patch_redis_clientObject

rubocop:disable Metrics/MethodLength



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 53

def patch_redis_client
  ::Redis::Client.class_eval do
    alias_method :initialize_without_datadog, :initialize
    Datadog::Monkey.without_warnings do
      remove_method :initialize
    end

    def initialize(*args)
      pin = Datadog::Pin.new(SERVICE, app: 'redis', app_type: Datadog::Ext::AppTypes::DB)
      pin.onto(self)
      initialize_without_datadog(*args)
    end

    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('redis.command') do |span|
        span.service = pin.service
        span.span_type = Datadog::Ext::Redis::TYPE
        span.resource = Datadog::Contrib::Redis::Quantize.format_command_args(*args)
        span.set_tag(Datadog::Ext::Redis::RAWCMD, span.resource)
        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('redis.command') do |span|
        span.service = pin.service
        span.span_type = Datadog::Ext::Redis::TYPE
        commands = args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.format_command_args(c) }
        span.resource = commands.join("\n")
        span.set_tag(Datadog::Ext::Redis::RAWCMD, span.resource)
        Datadog::Contrib::Redis::Tags.set_common_tags(self, span)

        response = call_pipeline_without_datadog(*args, &block)
      end

      response
    end
  end
end

.patched?Boolean

patched? tells wether patch has been successfully applied



110
111
112
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 110

def patched?
  @patched
end