From 32d301c0be0cf378085f035650ac0e8cc64dc0e5 Mon Sep 17 00:00:00 2001 From: Sarah Chen Date: Mon, 7 Aug 2023 11:34:10 -0400 Subject: [PATCH] changed input intake method, added back SELF_DEPRECATION_ONLY_ONCE variable, passing rubocop and original unit tests. --- .../tracing/contrib/elasticsearch/patcher.rb | 62 +++++++++++-------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/datadog/tracing/contrib/elasticsearch/patcher.rb b/lib/datadog/tracing/contrib/elasticsearch/patcher.rb index b75d9f5d69c..f97f14828f9 100644 --- a/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +++ b/lib/datadog/tracing/contrib/elasticsearch/patcher.rb @@ -19,16 +19,6 @@ def target_version Integration.version end - # `Elasticsearch` namespace renamed to `Elastic` in version 8.0.0 of the transport gem: - # @see https://github.com/elastic/elastic-transport-ruby/commit/ef804cbbd284f2a82d825221f87124f8b5ff823c - def transport_module - if Integration.version >= Gem::Version.new('8.0.0') - ::Elastic::Transport - else - ::Elasticsearch::Transport - end - end - def patch require 'uri' require 'json' @@ -37,9 +27,13 @@ def patch transport_module::Client.prepend(Client) end + SELF_DEPRECATION_ONLY_ONCE = Core::Utils::OnlyOnce.new + # Patches Elasticsearch::Transport::Client module module Client - def perform_request(method, path, params = {}, body = nil) + # rubocop:disable Metrics/MethodLength + # rubocop:disable Metrics/AbcSize + def perform_request(*args) # DEV-2.0: Remove this access, as `Client#self` in this context is not exposed to the user # since `elasticsearch` v8.0.0. In contrast, `Client#transport` is always available across # all `elasticsearch` gem versions and should be used instead. @@ -54,7 +48,7 @@ def perform_request(method, path, params = {}, body = nil) ) end end - + # `Client#transport` is most convenient object both this integration and the library # user have shared access to across all `elasticsearch` versions. # @@ -62,6 +56,10 @@ def perform_request(method, path, params = {}, body = nil) # does not have access to since `elasticsearch` v8.0.0. service ||= Datadog.configuration_for(transport, :service_name) || datadog_configuration[:service_name] + method = args[0] + path = args[1] + params = args[2] + body = args[3] full_url = URI.parse(path) url = full_url.path response = nil @@ -80,10 +78,6 @@ def perform_request(method, path, params = {}, body = nil) span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, Ext::TAG_SYSTEM) - # 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) - # Tag as an external peer service if Contrib::SpanAttributeSchema.default_span_attribute_schema? span.set_tag(Tracing::Metadata::Ext::TAG_PEER_SERVICE, span.service) @@ -97,16 +91,9 @@ def perform_request(method, path, params = {}, body = nil) end span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_METHOD, method) + tag_params(params, span) + tag_body(body, span) span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_URL, url) - span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_PARAMS, params) if params - if body - quantize_options = datadog_configuration[:quantize] - quantized_body = Datadog::Tracing::Contrib::Elasticsearch::Quantize.format_body( - body, - quantize_options - ) - span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_BODY, quantized_body) - end span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, host) if host span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, port) if port @@ -116,18 +103,39 @@ def perform_request(method, path, params = {}, body = nil) Datadog.logger.error(e.message) ensure # the call is still executed - response = perform_request_without_datadog(*args) + response = super span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status) end end response end + def tag_params(params, span) + return unless params + + params = JSON.generate(params) unless params.is_a?(String) + span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_PARAMS, params) + end + + def tag_body(body, span) + return unless body + + body = JSON.generate(body) unless body.is_a?(String) + quantize_options = datadog_configuration[:quantize] + quantized_body = Datadog::Tracing::Contrib::Elasticsearch::Quantize.format_body( + body, + quantize_options + ) + span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_BODY, quantized_body) + end + def datadog_configuration Datadog.configuration.tracing[:elasticsearch] end end - + # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/AbcSize + # `Elasticsearch` namespace renamed to `Elastic` in version 8.0.0 of the transport gem: # @see https://github.com/elastic/elastic-transport-ruby/commit/ef804cbbd284f2a82d825221f87124f8b5ff823c def transport_module