Skip to content

Commit

Permalink
changed input intake method, added back SELF_DEPRECATION_ONLY_ONCE va…
Browse files Browse the repository at this point in the history
…riable, passing rubocop and original unit tests.
  • Loading branch information
sarahchen6 committed Aug 8, 2023
1 parent c7409af commit 93a4e9b
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions lib/datadog/tracing/contrib/elasticsearch/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.
Expand All @@ -54,14 +48,18 @@ 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.
#
# `Client#self` in this context is an internal object that the library user
# 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
Expand All @@ -87,10 +85,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)

span.set_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME, host) if host

# Set analytics sample rate
Expand All @@ -99,16 +93,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

Expand All @@ -119,18 +106,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
Expand Down

0 comments on commit 93a4e9b

Please sign in to comment.