diff --git a/lib/datadog/di/component.rb b/lib/datadog/di/component.rb index c91eed45c63..8cbbf275201 100644 --- a/lib/datadog/di/component.rb +++ b/lib/datadog/di/component.rb @@ -84,7 +84,18 @@ def initialize(settings, agent_settings, logger, code_tracker: nil, telemetry: n @probe_notifier_worker = ProbeNotifierWorker.new(settings, transport, logger, telemetry: telemetry) @probe_notification_builder = ProbeNotificationBuilder.new(settings, serializer) @probe_manager = ProbeManager.new(settings, instrumenter, probe_notification_builder, probe_notifier_worker, logger, telemetry: telemetry) - probe_notifier_worker.start + + # If the worker is started here, it will be in the parent process + # of forking web servers like puma rather than in the worker processes + # that actually process HTTP requests, and thus DI will end up not + # sending any events to the agent. + # + # There is currently no "nice" way to handle this situation correctly - + # remote config has the same issue and the tracing Rack middleware + # presently starts the remote config worker. + # + # DI is hacked into that middleware the same way. + # probe_notifier_worker.start end attr_reader :settings diff --git a/lib/datadog/tracing/contrib/rack/middlewares.rb b/lib/datadog/tracing/contrib/rack/middlewares.rb index fc2662c95ca..ad4380f21b7 100644 --- a/lib/datadog/tracing/contrib/rack/middlewares.rb +++ b/lib/datadog/tracing/contrib/rack/middlewares.rb @@ -39,6 +39,12 @@ def call(env) boot = Datadog::Core::Remote::Tie.boot + if defined?(Datadog::DI.current_component) + # TODO: when would Datadog::DI be defined but + # Datadog::DI.current_component not be defined? + Datadog::DI.current_component&.probe_notifier_worker&.start + end + # Extract distributed tracing context before creating any spans, # so that all spans will be added to the distributed trace. if configuration[:distributed_tracing] 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 551c53eddd9..2b3dfaead78 100644 --- a/spec/datadog/di/integration/everything_from_remote_config_spec.rb +++ b/spec/datadog/di/integration/everything_from_remote_config_spec.rb @@ -106,6 +106,9 @@ def target_method before do expect(Datadog::DI).to receive(:component).at_least(:once).and_return(component) + + # This is done by tracing Rack middleware in actual applications + component.probe_notifier_worker.start end let(:mock_response) do diff --git a/spec/datadog/di/integration/instrumentation_spec.rb b/spec/datadog/di/integration/instrumentation_spec.rb index 907df2bc9e8..70a2f39e0f7 100644 --- a/spec/datadog/di/integration/instrumentation_spec.rb +++ b/spec/datadog/di/integration/instrumentation_spec.rb @@ -107,6 +107,9 @@ def mutating_method(greeting) allow(agent_settings).to receive(:ssl) allow(Datadog::DI).to receive(:current_component).and_return(component) + + # This is done by tracing Rack middleware in actual applications + component.probe_notifier_worker.start end context 'method probe' do