Skip to content
Open
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
28 changes: 16 additions & 12 deletions lib/datadog/di/probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions spec/datadog/di/probe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down