Skip to content

Commit a693370

Browse files
committed
Use telemetry.report for reporting route extraction errors
1 parent 11a7c86 commit a693370

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/datadog/appsec/api_security/route_extractor.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def self.route_pattern(request)
6464
# it does not have all the methods that ActionDispatch::Request has.
6565
# Before trying to use the router to recognize the route, we need to
6666
# create a new ActionDispatch::Request from the request env
67+
#
68+
# NOTE: Rails mutates HEAD request by changing the method to GET
69+
# and uses it for route recognition to check if the route is defined
6770
request = request.env[RAILS_ROUTES_KEY].request_class.new(request.env)
6871

6972
pattern = request.env[RAILS_ROUTES_KEY].router
@@ -78,8 +81,8 @@ def self.route_pattern(request)
7881
else
7982
Tracing::Contrib::Rack::RouteInference.read_or_infer(request.env)
8083
end
81-
rescue
82-
AppSec.telemetry&.error('AppSec: Could not extract route pattern for APISecurity sampler')
84+
rescue => e
85+
AppSec.telemetry&.report(e, description: 'AppSec: Could not extract route pattern')
8386

8487
nil
8588
end

spec/datadog/appsec/api_security/route_extractor_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@
219219
it { expect(described_class.route_pattern(request)).to be_nil }
220220

221221
it 'reports the error via telemetry' do
222-
expect(telemetry).to receive(:error).with('AppSec: Could not extract route pattern for APISecurity sampler')
222+
expect(telemetry).to receive(:report)
223+
.with(an_instance_of(StandardError), description: 'AppSec: Could not extract route pattern')
223224

224225
described_class.route_pattern(request)
225226
end

0 commit comments

Comments
 (0)