Skip to content

Commit

Permalink
Pin to transport
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Jan 26, 2024
1 parent 3f16639 commit 3ac27d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 20 additions & 0 deletions lib/datadog/tracing/contrib/elasticsearch/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def patch
require 'json'
require_relative 'quantize'

transport_module::Client.prepend(DatadogPin)
transport_module::Client.prepend(Client)
end

Expand Down Expand Up @@ -126,6 +127,25 @@ def datadog_configuration
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

# Patch to support both `elasticsearch` and `elastic-transport` versions
module DatadogPin
def datadog_pin=(pin)
pin.onto(pin_candidate)
end

def datadog_pin
Datadog.configuration_for(pin_candidate)
end

def pin_candidate(candidate = self)
if candidate.respond_to?(:transport)
pin_candidate(candidate.transport)
else
candidate
end
end
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
Expand Down
9 changes: 1 addition & 8 deletions spec/datadog/tracing/contrib/elasticsearch/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,9 @@ def call(env)

describe 'transport configuration override' do
context 'when #service is overridden' do
let(:version_greater_than_8) { Gem::Version.new(::Elasticsearch::VERSION) >= Gem::Version.new('8.0.0') }
let(:service_name) { 'bar' }

before do
if version_greater_than_8
Datadog.configure_onto(client.transport, service_name: service_name)
else
Datadog.configure_onto(client.transport.transport, service_name: service_name)
end
end
before { Datadog.configure_onto(client.transport, service_name: service_name) }

describe 'then a GET request' do
subject(:response) { client.perform_request(method, path) }
Expand Down

0 comments on commit 3ac27d8

Please sign in to comment.