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



19
20
21
22
23
24
25
26
27
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 19

def patch
  # 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
end

.patch_redis_clientObject

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



31
32
33
34
35
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
91
# File 'lib/ddtrace/contrib/redis/patcher.rb', line 31

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[:service_name],
          app: Ext::APP,
          app_type: Datadog::Ext::AppTypes::DB,
          tracer: datadog_configuration[:tracer]
        )
        pin.onto(self)
      end
    end

    private

    def datadog_configuration
      Datadog.configuration[:redis, options]
    end
  end
end

.target_versionObject



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

def target_version
  Integration.version
end