Module: Datadog::Contrib::Elasticsearch::Patcher
- Includes:
- Patcher
- Defined in:
- lib/ddtrace/contrib/elasticsearch/patcher.rb
Overview
Patcher enables patching of ‘elasticsearch’ module.
Class Method Summary collapse
- .patch ⇒ Object
-
.patch_elasticsearch_transport_client ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize.
- .patched? ⇒ Boolean
Methods included from Patcher
Class Method Details
.patch ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 20 def patch do_once(:elasticsearch) do begin require 'uri' require 'json' require 'ddtrace/pin' require 'ddtrace/contrib/elasticsearch/quantize' patch_elasticsearch_transport_client rescue StandardError => e Datadog::Tracer.log.error("Unable to apply Elasticsearch integration: #{e}") end end end |
.patch_elasticsearch_transport_client ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 37 def patch_elasticsearch_transport_client # rubocop:disable Metrics/BlockLength ::Elasticsearch::Transport::Client.class_eval do alias_method :initialize_without_datadog, :initialize Datadog::Patcher.without_warnings do remove_method :initialize end def initialize(*args, &block) tracer = Datadog.configuration[:elasticsearch][:tracer] service = Datadog.configuration[:elasticsearch][:service_name] pin = Datadog::Pin.new( service, app: Datadog::Contrib::Elasticsearch::Ext::APP, app_type: Datadog::Ext::AppTypes::DB, tracer: tracer ) pin.onto(self) initialize_without_datadog(*args, &block) 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(Datadog::Contrib::Elasticsearch::Ext::SPAN_QUERY) do |span| begin connection = transport.connections.first host = connection.host[:host] if connection port = connection.host[:port] if connection span.service = pin.service span.span_type = Datadog::Contrib::Elasticsearch::Ext::SPAN_TYPE_QUERY # 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) # Set analytics sample rate if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled]) Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate]) end span.set_tag(Datadog::Contrib::Elasticsearch::Ext::TAG_METHOD, method) span.set_tag(Datadog::Contrib::Elasticsearch::Ext::TAG_URL, url) span.set_tag(Datadog::Contrib::Elasticsearch::Ext::TAG_PARAMS, params) if params if body = datadog_configuration[:quantize] quantized_body = Datadog::Contrib::Elasticsearch::Quantize.format_body(body, ) span.set_tag(Datadog::Contrib::Elasticsearch::Ext::TAG_BODY, quantized_body) end span.set_tag(Datadog::Ext::NET::TARGET_HOST, host) if host span.set_tag(Datadog::Ext::NET::TARGET_PORT, port) if port quantized_url = Datadog::Contrib::Elasticsearch::Quantize.format_url(url) span.resource = "#{method} #{quantized_url}" rescue StandardError => e Datadog::Tracer.log.error(e.) ensure # the call is still executed response = perform_request_without_datadog(*args) span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, response.status) end end response end def datadog_configuration Datadog.configuration[:elasticsearch] end end end |
.patched? ⇒ Boolean
16 17 18 |
# File 'lib/ddtrace/contrib/elasticsearch/patcher.rb', line 16 def patched? done?(:elasticsearch) end |