Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEBUG-2334 do not try to install pending probes on eval'd code #4166

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions lib/datadog/di/code_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ def start
registry_lock.synchronize do
registry[path] = tp.instruction_sequence
end
end

DI.current_component&.probe_manager&.install_pending_line_probes(path)

# Also, pending line probes should only be installed for
# non-eval'd code.
DI.current_component&.probe_manager&.install_pending_line_probes(path)
end
# Since this method normally is called from customer applications,
# rescue any exceptions that might not be handled to not break said
# customer applications.
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/di/probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def location
# If file is not an absolute path, the path matches if the file is its suffix,
# at a path component boundary.
def file_matches?(path)
if path.nil?
raise ArgumentError, "Cannot match against a nil path"
end
unless file
raise ArgumentError, "Probe does not have a file to match against"
end
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/di/probe_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ def remove_other_probes(probe_ids)
# point, which is invoked for each required or loaded file
# (and also for eval'd code, but those invocations are filtered out).
def install_pending_line_probes(path)
if path.nil?
raise ArgumentError, "path must not be nil"
end
@lock.synchronize do
@pending_probes.values.each do |probe|
if probe.line?
Expand Down
7 changes: 7 additions & 0 deletions lib/datadog/di/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ module Utils
# If suffix is not an absolute path, the path matches if its suffix is
# the provided suffix, at a path component boundary.
module_function def path_matches_suffix?(path, suffix)
if path.nil?
raise ArgumentError, "nil path passed"
end
if suffix.nil?
raise ArgumentError, "nil suffix passed"
end

if suffix.start_with?('/')
path == suffix
else
Expand Down
1 change: 1 addition & 0 deletions spec/datadog/di/code_tracker_pending_load.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nothing
1 change: 1 addition & 0 deletions spec/datadog/di/code_tracker_pending_require.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Nothing
73 changes: 66 additions & 7 deletions spec/datadog/di/code_tracker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@
RSpec.describe Datadog::DI::CodeTracker do
di_test

before(:all) do
Datadog::DI.deactivate_tracking!
end

let(:tracker) do
described_class.new
end

shared_context 'when code tracker is running' do
before do
tracker.start
end

after do
tracker.stop
end
end

describe "#start" do
after do
tracker.stop
Expand Down Expand Up @@ -90,13 +104,7 @@

describe "#active?" do
context "when started" do
before do
tracker.start
end

after do
tracker.stop
end
include_context 'when code tracker is running'

it "is true" do
expect(tracker.active?).to be true
Expand All @@ -115,6 +123,57 @@
end
end

describe 'line probe installation' do
let(:component) do
instance_double(Datadog::DI::Component).tap do |component|
expect(component).to receive(:probe_manager).and_return(probe_manager)
end
end

let(:probe_manager) do
instance_double(Datadog::DI::ProbeManager)
end

context 'when started' do
include_context 'when code tracker is running'

context 'when a file is required' do
it 'requests to install pending line probes' do
expect(Datadog::DI).to receive(:current_component).and_return(component)
expect(probe_manager).to receive(:install_pending_line_probes) do |path|
# Should be an absolute path
expect(path).to start_with('/')
expect(File.basename(path)).to eq('code_tracker_pending_require.rb')
end
require_relative 'code_tracker_pending_require'
end
end

context 'when a file is loaded' do
it 'requests to install pending line probes' do
expect(Datadog::DI).to receive(:current_component).and_return(component)
expect(probe_manager).to receive(:install_pending_line_probes) do |path|
# Should be an absolute path
expect(path).to start_with('/')
expect(File.basename(path)).to eq('code_tracker_pending_load.rb')
end
load File.join(File.dirname(__FILE__), 'code_tracker_pending_load.rb')
end
end

context "when Ruby code is eval'd" do
it 'requests to install pending line probes' do
# Matchers can be lazily loaded, force all code to be loaded here.
expect(4).to eq(4)

expect(Datadog::DI).not_to receive(:current_component)
expect(probe_manager).not_to receive(:install_pending_line_probes)
expect(eval('2 + 2')).to eq(4) # rubocop:disable Style/EvalWithLocation
end
end
end
end

describe "#iseqs_for_path_suffix" do
around do |example|
tracker.start
Expand Down
Loading