Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/datadog/di/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason the approach we use in tracing doesn't work for DI?
In tracing, we always check if the process has forked when submitting data, and if it has, we restart the worker:

start if @worker.nil? || @pid != Process.pid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can certainly do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #4363


# 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
Expand Down
6 changes: 6 additions & 0 deletions lib/datadog/tracing/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a question similar to TODO's question: Is the current_component&.probe_notifier_worker needs to have safe-navigation? Could it be not defined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire DI component should be missing on Ruby 2.5 and JRuby. I am not sure (per the comment, and I did not investigate) why the DI module is defined but current_component is not defined sometimes. If current_component is defined and not nil, the rest of DI infrastructure should be loaded.

end

# Extract distributed tracing context before creating any spans,
# so that all spans will be added to the distributed trace.
if configuration[:distributed_tracing]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/datadog/di/integration/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading