Skip to content

Commit

Permalink
feat: Add compatible? method to instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Mar 1, 2023
1 parent 9855ca7 commit a6358b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Instrumentation
module Elasticsearch
# The Instrumentation class contains logic to detect and install the Elasticsearch instrumentation
class Instrumentation < OpenTelemetry::Instrumentation::Base
MINIMUM_VERSION = Gem::Version.new('8.0.0')

install do |_config|
require_dependencies
Expand All @@ -19,6 +20,13 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
!defined?(::Elastic::Transport::Client).nil?
end

compatible do
# Versions < 8.0 of the elasticsearch client don't have the
# Elastic::Transport namespace so we have to check that it's
# present first.
present? && gem_version >= MINIMUM_VERSION
end

def patch
::Elastic::Transport::Client.prepend(Patches::Client)
end
Expand All @@ -29,6 +37,10 @@ def patch

private

def gem_version
Gem::Version.new(::Elastic::Transport::VERSION)
end

def require_dependencies
require_relative 'patches/client'
require_relative 'patches/deep_dup'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
end
end

describe 'compatible' do
it 'when older gem version installed' do
stub_const('::Elastic::Transport::VERSION', '7.17.7')
_(instrumentation.compatible?).must_equal false
end

it 'when future gem version installed' do
_(instrumentation.compatible?).must_equal true
end
end

describe '#install' do
it 'accepts argument' do
_(instrumentation.install({})).must_equal(true)
Expand Down

0 comments on commit a6358b0

Please sign in to comment.