diff --git a/lib/datadog/core/configuration/components.rb b/lib/datadog/core/configuration/components.rb index bf154311b43..c4f8301541d 100644 --- a/lib/datadog/core/configuration/components.rb +++ b/lib/datadog/core/configuration/components.rb @@ -113,7 +113,7 @@ def initialize(settings) @runtime_metrics = self.class.build_runtime_metrics_worker(settings) @health_metrics = self.class.build_health_metrics(settings) @appsec = Datadog::AppSec::Component.build_appsec_component(settings, telemetry: telemetry) - @dynamic_instrumentation = Datadog::DI::Component.build(settings, agent_settings, telemetry: telemetry) + @dynamic_instrumentation = Datadog::DI::Component.build(settings, agent_settings, @logger, telemetry: telemetry) self.class.configure_tracing(settings) end diff --git a/lib/datadog/di/component.rb b/lib/datadog/di/component.rb index 8ef93e626d4..797449c077d 100644 --- a/lib/datadog/di/component.rb +++ b/lib/datadog/di/component.rb @@ -16,22 +16,22 @@ module DI # resources and installed tracepoints upon shutdown. class Component class << self - def build(settings, agent_settings, telemetry: nil) + def build(settings, agent_settings, logger, telemetry: nil) return unless settings.respond_to?(:dynamic_instrumentation) && settings.dynamic_instrumentation.enabled unless settings.respond_to?(:remote) && settings.remote.enabled - Datadog.logger.debug("Dynamic Instrumentation could not be enabled because Remote Configuration Management is not available. To enable Remote Configuration, see https://docs.datadoghq.com/agent/remote_config") + logger.debug("Dynamic Instrumentation could not be enabled because Remote Configuration Management is not available. To enable Remote Configuration, see https://docs.datadoghq.com/agent/remote_config") return end - return unless environment_supported?(settings) + return unless environment_supported?(settings, logger) - new(settings, agent_settings, Datadog.logger, code_tracker: DI.code_tracker, telemetry: telemetry).tap do |component| + new(settings, agent_settings, logger, code_tracker: DI.code_tracker, telemetry: telemetry).tap do |component| DI.add_current_component(component) end end - def build!(settings, agent_settings, telemetry: nil) + def build!(settings, agent_settings, logger, telemetry: nil) unless settings.respond_to?(:dynamic_instrumentation) && settings.dynamic_instrumentation.enabled raise "Requested DI component but DI is not enabled in settings" end @@ -40,27 +40,27 @@ def build!(settings, agent_settings, telemetry: nil) raise "Requested DI component but remote config is not enabled in settings" end - unless environment_supported?(settings) + unless environment_supported?(settings, logger) raise "DI does not support the environment (development or Ruby version too low or not MRI)" end - new(settings, agent_settings, Datadog.logger, code_tracker: DI.code_tracker, telemetry: telemetry) + new(settings, agent_settings, logger, code_tracker: DI.code_tracker, telemetry: telemetry) end # Checks whether the runtime environment is supported by # dynamic instrumentation. Currently we only require that, if Rails # is used, that Rails environment is not development because # DI does not currently support code unloading and reloading. - def environment_supported?(settings) + def environment_supported?(settings, logger) # TODO add tests? unless settings.dynamic_instrumentation.internal.development if Datadog::Core::Environment::Execution.development? - Datadog.logger.debug("Not enabling dynamic instrumentation because we are in development environment") + logger.debug("Not enabling dynamic instrumentation because we are in development environment") return false end end if RUBY_ENGINE != 'ruby' || RUBY_VERSION < '2.6' - Datadog.logger.debug("Not enabling dynamic instrumentation because of unsupported Ruby version") + logger.debug("Not enabling dynamic instrumentation because of unsupported Ruby version") return false end true diff --git a/sig/datadog/di/component.rbs b/sig/datadog/di/component.rbs index 27794388c27..eb64ddab7cf 100644 --- a/sig/datadog/di/component.rbs +++ b/sig/datadog/di/component.rbs @@ -23,12 +23,12 @@ module Datadog @probe_manager: untyped - def self.build: (untyped settings, untyped agent_settings, ?telemetry: untyped?) -> (nil | untyped) + def self.build: (untyped settings, untyped agent_settings, Core::Logger logger, ?telemetry: untyped?) -> (nil | untyped) - def self.build!: (untyped settings, untyped agent_settings, ?telemetry: untyped?) -> untyped - def self.environment_supported?: (untyped settings) -> (false | true) + def self.build!: (untyped settings, untyped agent_settings, Core::Logger logger, ?telemetry: untyped?) -> untyped + def self.environment_supported?: (untyped settings, Core::Logger logger) -> (false | true) - def initialize: (untyped settings, untyped agent_settings, untyped logger, ?code_tracker: untyped?, ?telemetry: untyped?) -> void + def initialize: (untyped settings, untyped agent_settings, Core::Logger logger, ?code_tracker: untyped?, ?telemetry: untyped?) -> void attr_reader settings: untyped diff --git a/spec/datadog/di/component_spec.rb b/spec/datadog/di/component_spec.rb index 49620913aa8..0453fc8e4a8 100644 --- a/spec/datadog/di/component_spec.rb +++ b/spec/datadog/di/component_spec.rb @@ -16,6 +16,10 @@ instance_double_agent_settings end + let(:logger) do + instance_double(Logger) + end + context 'when dynamic instrumentation is enabled' do let(:dynamic_instrumentation_enabled) { true } @@ -32,7 +36,7 @@ end it 'returns a Datadog::DI::Component instance' do - component = described_class.build(settings, agent_settings) + component = described_class.build(settings, agent_settings, logger) expect(component).to be_a(described_class) component.shutdown! end @@ -44,7 +48,8 @@ end it 'returns nil' do - component = described_class.build(settings, agent_settings) + expect(logger).to receive(:debug).with(/Dynamic Instrumentation could not be enabled because Remote Configuration Management is not available/) + component = described_class.build(settings, agent_settings, logger) expect(component).to be nil end end @@ -54,7 +59,7 @@ let(:dynamic_instrumentation_enabled) { false } it 'returns nil' do - component = described_class.build(settings, agent_settings) + component = described_class.build(settings, agent_settings, logger) expect(component).to be nil end end diff --git a/spec/datadog/di/integration/everything_from_remote_config_spec.rb b/spec/datadog/di/integration/everything_from_remote_config_spec.rb index faf3570fcf9..499793a286e 100644 --- a/spec/datadog/di/integration/everything_from_remote_config_spec.rb +++ b/spec/datadog/di/integration/everything_from_remote_config_spec.rb @@ -23,6 +23,8 @@ def target_method let(:telemetry) { instance_double(Datadog::Core::Telemetry::Component) } + let(:logger) { instance_double(Logger) } + let(:repository) { Datadog::Core::Remote::Configuration::Repository.new } let(:transaction) do @@ -59,7 +61,7 @@ def target_method let(:receiver) { remote.receivers(telemetry)[0] } let(:component) do - Datadog::DI::Component.build!(settings, agent_settings) + Datadog::DI::Component.build!(settings, agent_settings, logger) end let(:propagate_all_exceptions) { true } @@ -239,6 +241,8 @@ def do_rc end it 'adds a probe to pending list' do + expect(logger).to receive(:info).with(/Received probe from RC:/) + do_rc expect(payloads).to be_a(Array) @@ -268,6 +272,8 @@ def assert_received_and_installed end it 'instruments code and adds probe to installed list' do + expect(logger).to receive(:info).with(/Received probe from RC:/) + do_rc assert_received_and_installed @@ -276,6 +282,8 @@ def assert_received_and_installed context 'and target method is invoked' do it 'notifies about execution' do + expect(logger).to receive(:info).with(/Received probe from RC:/) + do_rc assert_received_and_installed @@ -317,6 +325,9 @@ def assert_received_and_installed end it 'installs the second, known, probe' do + expect(logger).to receive(:warn).with(/Unrecognized probe type:/) + expect(logger).to receive(:info).with(/Received probe from RC:/) + do_rc assert_received_and_installed diff --git a/spec/datadog/di/integration/instrumentation_spec.rb b/spec/datadog/di/integration/instrumentation_spec.rb index 4f4a8b60d53..28720fd1995 100644 --- a/spec/datadog/di/integration/instrumentation_spec.rb +++ b/spec/datadog/di/integration/instrumentation_spec.rb @@ -59,8 +59,12 @@ def mutating_method(greeting) instance_double_agent_settings end + let(:logger) do + instance_double(Logger) + end + let(:component) do - Datadog::DI::Component.build!(settings, agent_settings) + Datadog::DI::Component.build!(settings, agent_settings, logger) end let(:expected_installed_payload) do