diff --git a/lib/datadog/di/probe.rb b/lib/datadog/di/probe.rb index ca4088a60da..c04e7a59d2d 100644 --- a/lib/datadog/di/probe.rb +++ b/lib/datadog/di/probe.rb @@ -46,9 +46,17 @@ def initialize(id:, type:, raise ArgumentError, "Unknown probe type: #{type}" end - if line_no && method_name - raise ArgumentError, "Probe contains both line number and method name: #{id}" - end + # Probe should be inferred to be a line probe if the specification + # contains a line number. This how Java tracer works and Go tracer + # is implementing the same behavior, and Go will have all 3 fields + # (file path, line number and method name) for line probes. + # Do not raise if line number and method name both exist - instead + # treat the probe as a line probe. + # + # In the future we want to provide type name and method name to line + # probes, so that the library can verify that the instrumented line + # is in the method that the frontend showed to the user when the + # user created the probe. if line_no && !file raise ArgumentError, "Probe contains line number but not file: #{id}" @@ -58,6 +66,10 @@ def initialize(id:, type:, raise ArgumentError, "Partial method probe definition: #{id}" end + if line_no.nil? && method_name.nil? + raise ArgumentError, "Unhandled probe type: neither method nor line probe: #{id}" + end + @id = id @type = type @file = file @@ -71,14 +83,6 @@ def initialize(id:, type:, @max_capture_attribute_count = max_capture_attribute_count @condition = condition - # These checks use instance methods that have more complex logic - # than checking a single argument value. To avoid duplicating - # the logic here, use the methods and perform these checks after - # instance variable assignment. - unless method? || line? - raise ArgumentError, "Unhandled probe type: neither method nor line probe: #{id}" - end - @rate_limit = rate_limit || (@capture_snapshot ? 1 : 5000) @rate_limiter = Datadog::Core::TokenBucket.new(@rate_limit) @@ -129,7 +133,7 @@ def line? # Returns whether the probe is a method probe. def method? - !!(type_name && method_name) + line_no.nil? end # Returns the line number associated with the probe, raising diff --git a/spec/datadog/di/probe_spec.rb b/spec/datadog/di/probe_spec.rb index 4aa41809452..88403759bfe 100644 --- a/spec/datadog/di/probe_spec.rb +++ b/spec/datadog/di/probe_spec.rb @@ -89,10 +89,9 @@ type_name: "foo", method_name: "bar", file: "baz", line_no: 4) end - it "raises ArgumentError" do - expect do - probe - end.to raise_error(ArgumentError, /both line number and method name/) + it "creates a line probe" do + expect(probe.line?).to be true + expect(probe.method?).to be false end end end