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 require file presence for line probe #4105

Merged
merged 1 commit into from
Nov 12, 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
9 changes: 8 additions & 1 deletion lib/datadog/di/probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def initialize(id:, type:,
raise ArgumentError, "Probe contains both line number and method name: #{id}"
end

if line_no && !file
raise ArgumentError, "Probe contains line number but not file: #{id}"
end

if type_name && !method_name || method_name && !type_name
raise ArgumentError, "Partial method probe definition: #{id}"
end
Expand Down Expand Up @@ -103,7 +107,10 @@ def capture_snapshot?
# method or for stack traversal purposes?), therefore we do not check
# for file name/path presence here and just consider the line number.
def line?
!line_no.nil?
# Constructor checks that file is given if line number is given,
# but for safety, check again here since we somehow got a probe with
# a line number but no file in the wild.
!!(file && line_no)
end

# Returns whether the probe is a method probe.
Expand Down
16 changes: 14 additions & 2 deletions spec/datadog/di/probe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
end
end

context 'line number given but file is not' do
let(:probe) do
described_class.new(id: "42", type: :log, line_no: 5)
end

it "raises ArgumentError" do
expect do
probe
end.to raise_error(ArgumentError, /Probe contains line number but not file/)
end
end

context 'unsupported type' do
let(:probe) do
# LOG_PROBE is a valid type in RC probe specification but not
Expand Down Expand Up @@ -151,7 +163,7 @@

describe "#line_no" do
context "one line number" do
let(:probe) { described_class.new(id: "x", type: :log, line_no: 5) }
let(:probe) { described_class.new(id: "x", type: :log, file: 'x', line_no: 5) }

it "returns the line number" do
expect(probe.line_no).to eq 5
Expand All @@ -169,7 +181,7 @@

describe "#line_no!" do
context "one line number" do
let(:probe) { described_class.new(id: "x", type: :log, line_no: 5) }
let(:probe) { described_class.new(id: "x", type: :log, file: 'x', line_no: 5) }

it "returns the line number" do
expect(probe.line_no!).to eq 5
Expand Down
Loading