diff --git a/lib/datadog/di/probe.rb b/lib/datadog/di/probe.rb index 5dd5cf88a72..c709e3571eb 100644 --- a/lib/datadog/di/probe.rb +++ b/lib/datadog/di/probe.rb @@ -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 @@ -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. diff --git a/spec/datadog/di/probe_spec.rb b/spec/datadog/di/probe_spec.rb index 39561e87e8b..4aa41809452 100644 --- a/spec/datadog/di/probe_spec.rb +++ b/spec/datadog/di/probe_spec.rb @@ -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 @@ -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 @@ -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