Module: Datadog::Contrib::Elasticsearch::Patcher

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

Overview

Patcher enables patching of ‘elasticsearch/transport’ 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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 22

def patch
  if !@patched && (defined?(::Elasticsearch::Transport::VERSION) && \
                   Gem::Version.new(::Elasticsearch::Transport::VERSION) >= Gem::Version.new('1.0.0'))
    begin
      require 'uri'
      require 'json'
      require 'ddtrace/monkey'
      require 'ddtrace/pin'
      require 'ddtrace/ext/app_types'
      require 'ddtrace/contrib/elasticsearch/quantize'

      patch_elasticsearch_transport_client()

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

.patch_elasticsearch_transport_clientObject

rubocop:disable Metrics/MethodLength



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
92
93
94
95
96
97
98
99
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 44

def patch_elasticsearch_transport_client
  ::Elasticsearch::Transport::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: 'elasticsearch', app_type: Datadog::Ext::AppTypes::DB)
      pin.onto(self)
      initialize_without_datadog(*args)
    end

    alias_method :perform_request_without_datadog, :perform_request
    remove_method :perform_request

    def perform_request(*args)
      pin = Datadog::Pin.get_from(self)
      return perform_request_without_datadog(*args) unless pin && pin.tracer

      method = args[0]
      path = args[1]
      params = args[2]
      body = args[3]
      full_url = URI.parse(path)

      url = full_url.path
      response = nil
      pin.tracer.trace('elasticsearch.query') do |span|
        begin
          span.service = pin.service
          span.span_type = SPAN_TYPE

          # load JSON for the following fields unless they're already strings
          params = JSON.generate(params) if params && !params.is_a?(String)
          body = JSON.generate(body) if body && !body.is_a?(String)

          span.set_tag(METHOD, method)
          span.set_tag(URL, url)
          span.set_tag(PARAMS, params) if params
          span.set_tag(BODY, body) if body

          quantized_url = Datadog::Contrib::Elasticsearch::Quantize.format_url(url)
          span.resource = "#{method} #{quantized_url}"
        rescue StandardError => e
          Datadog::Tracer.log.error(e.message)
        ensure
          # the call is still executed
          response = perform_request_without_datadog(*args)
          span.set_tag('http.status_code', response.status)
        end
      end
      response
    end
  end
end

.patched?Boolean

patched? tells wether patch has been successfully applied

Returns:

  • (Boolean)


102
103
104
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 102

def patched?
  @patched
end