From eae47f52130d29df35c7348b34628d1820fd1e1d Mon Sep 17 00:00:00 2001 From: Colton Jenkins Date: Sun, 27 Nov 2022 17:11:21 -0500 Subject: [PATCH] Add new flag, suppress_not_found to use_all config method. Separate out failed to install from not found in use_all. --- .../opentelemetry/instrumentation/registry.rb | 14 +++-- .../instrumentation/registry_test.rb | 63 +++++++++++++------ sdk/lib/opentelemetry/sdk/configurator.rb | 7 ++- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/registry/lib/opentelemetry/instrumentation/registry.rb b/registry/lib/opentelemetry/instrumentation/registry.rb index d4bcba91ee..e794da66ec 100644 --- a/registry/lib/opentelemetry/instrumentation/registry.rb +++ b/registry/lib/opentelemetry/instrumentation/registry.rb @@ -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 @@ -59,10 +59,12 @@ def install(instrumentation_names, instrumentation_config_map = {}) # @param [optional 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 @@ -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" diff --git a/registry/test/opentelemetry/instrumentation/registry_test.rb b/registry/test/opentelemetry/instrumentation/registry_test.rb index 4fb8220664..bff349ed0c 100644 --- a/registry/test/opentelemetry/instrumentation/registry_test.rb +++ b/registry/test/opentelemetry/instrumentation/registry_test.rb @@ -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 @@ -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 diff --git a/sdk/lib/opentelemetry/sdk/configurator.rb b/sdk/lib/opentelemetry/sdk/configurator.rb index 1aa918a4c2..68ad1a76a1 100644 --- a/sdk/lib/opentelemetry/sdk/configurator.rb +++ b/sdk/lib/opentelemetry/sdk/configurator.rb @@ -112,9 +112,12 @@ def use(instrumentation_name, config = nil) # # @param [optional 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 @@ -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