Skip to content

Commit

Permalink
Add new flag, suppress_not_found to use_all config method.
Browse files Browse the repository at this point in the history
Separate out failed to install from not found in use_all.
  • Loading branch information
cwjenkins committed Jan 23, 2023
1 parent 4cd4a52 commit eae47f5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
14 changes: 9 additions & 5 deletions registry/lib/opentelemetry/instrumentation/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def install(instrumentation_names, instrumentation_config_map = {})
if instrumentation.nil?
OpenTelemetry.logger.warn "Could not install #{instrumentation_name} because it was not found"
else
install_instrumentation(instrumentation, instrumentation_config_map[instrumentation.name])
install_instrumentation(instrumentation, instrumentation_config_map[instrumentation.name], false)
end
end
end
Expand All @@ -59,10 +59,12 @@ def install(instrumentation_names, instrumentation_config_map = {})
# @param [optional Hash<String, Hash>] instrumentation_config_map A map of
# instrumentation_name to config. This argument is optional and config can be
# passed for as many or as few instrumentations as desired.
def install_all(instrumentation_config_map = {})
# @param [optional boolean] suppress_not_found A flag that will suppress
# the warning log that a given instrumentation wasn't found.
def install_all(instrumentation_config_map = {}, suppress_not_found: false)
@lock.synchronize do
@instrumentation.map(&:instance).each do |instrumentation|
install_instrumentation(instrumentation, instrumentation_config_map[instrumentation.name])
install_instrumentation(instrumentation, instrumentation_config_map[instrumentation.name], suppress_not_found)
end
end
end
Expand All @@ -74,8 +76,10 @@ def find_instrumentation(instrumentation_name)
&.instance
end

def install_instrumentation(instrumentation, config)
if instrumentation.install(config)
def install_instrumentation(instrumentation, config, suppress_not_found)
if !instrumentation.present?
OpenTelemetry.logger.warn "Could not install #{instrumentation.name} because it was not found" unless suppress_not_found
elsif instrumentation.install(config)
OpenTelemetry.logger.info "Instrumentation: #{instrumentation.name} was successfully installed with the following options #{instrumentation.config}"
else
OpenTelemetry.logger.warn "Instrumentation: #{instrumentation.name} failed to install"
Expand Down
63 changes: 43 additions & 20 deletions registry/test/opentelemetry/instrumentation/registry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
class FakeInstrumentation
attr_reader :name, :version, :config

def initialize(name, version)
def initialize(name, version, present: true)
@name = name
@version = version
@install = false
@config = nil
@present = present
end

def instance
self
end

def present?
@present
end

def installed?
@install == true
end
Expand Down Expand Up @@ -67,33 +72,51 @@ def install(config)
end

describe '#install_all' do
before do
instrumentations.each { |i| registry.register(i) }
end
describe 'with existing instrumentation' do
before do
instrumentations.each { |i| registry.register(i) }
end

describe 'when using defaults arguments' do
it 'installs all registered instrumentations' do
registry.install_all
describe 'when using defaults arguments' do
it 'installs all registered instrumentations' do
registry.install_all

instrumentations.each do |i|
_(i).must_be :installed?
_(i.config).must_be_nil
end
end
end

instrumentations.each do |i|
_(i).must_be :installed?
_(i.config).must_be_nil
describe 'when using instrumentation specific configs' do
it 'installs all registered instrumentations' do
registry.install_all(
'TestInstrumentation1' => { a: 'a' },
'TestInstrumentation2' => { b: 'b' }
)

_(instrumentation1).must_be :installed?
_(instrumentation1.config).must_equal(a: 'a')

_(instrumentation2).must_be :installed?
_(instrumentation2.config).must_equal(b: 'b')
end
end
end

describe 'when using instrumentation specific configs' do
it 'installs all registered instrumentations' do
registry.install_all(
'TestInstrumentation1' => { a: 'a' },
'TestInstrumentation2' => { b: 'b' }
)
describe 'with non existent instrumentation' do
describe 'suppress not found' do
before do
@log_stream = StringIO.new
OpenTelemetry.logger = ::Logger.new(@log_stream)
end

_(instrumentation1).must_be :installed?
_(instrumentation1.config).must_equal(a: 'a')
it 'suppresses not found in logs' do
registry.register(FakeInstrumentation.new('Not installed', '1.0.0', present: false))
registry.install_all({}, suppress_not_found: true)

_(instrumentation2).must_be :installed?
_(instrumentation2.config).must_equal(b: 'b')
_(@log_stream.string).must_be_empty
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions sdk/lib/opentelemetry/sdk/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ def use(instrumentation_name, config = nil)
#
# @param [optional Hash<String,Hash>] instrumentation_config_map A map with string keys
# representing the instrumentation name and values specifying the instrumentation config
def use_all(instrumentation_config_map = {})
# @param [optional boolean] suppress_not_found A flag that will suppress
# the warning log that a given instrumentation wasn't found.
def use_all(instrumentation_config_map = {}, suppress_not_found = false)
check_use_mode!(USE_MODE_ALL)
@instrumentation_config_map = instrumentation_config_map
@suppress_not_found = suppress_not_found
end

# Add a span processor to the export pipeline
Expand Down Expand Up @@ -163,7 +166,7 @@ def install_instrumentation
when USE_MODE_ONE
OpenTelemetry::Instrumentation.registry.install(@instrumentation_names, @instrumentation_config_map)
when USE_MODE_ALL
OpenTelemetry::Instrumentation.registry.install_all(@instrumentation_config_map)
OpenTelemetry::Instrumentation.registry.install_all(@instrumentation_config_map, suppress_not_found: @suppress_not_found)
end
end

Expand Down

0 comments on commit eae47f5

Please sign in to comment.