Skip to content

Commit cb4cd77

Browse files
committed
feat(performance): don't record HTTP OPTIONS and HEAD transactions
1 parent 168b43a commit cb4cd77

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
```rb
2020
config.enabled_patches += [:sidekiq_scheduler]
2121
```
22+
- Sentry will not record traces of HTTP OPTIONS and HEAD requests in Rack and Rails apps [#2181](https://github.com/getsentry/sentry-ruby/pull/2181)
2223

2324
### Bug Fixes
2425

sentry-ruby/lib/sentry/rack/capture_exceptions.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Sentry
44
module Rack
55
class CaptureExceptions
66
ERROR_EVENT_ID_KEY = "sentry.error_event_id"
7+
IGNORED_HTTP_METHODS = ["HEAD", "OPTIONS"].freeze
78

89
def initialize(app)
910
@app = app
@@ -62,7 +63,17 @@ def capture_exception(exception, env)
6263
end
6364

6465
def start_transaction(env, scope)
65-
options = { name: scope.transaction_name, source: scope.transaction_source, op: transaction_op }
66+
options = {
67+
name: scope.transaction_name,
68+
source: scope.transaction_source,
69+
op: transaction_op
70+
}
71+
72+
# Tell Sentry to not sample this transaction if this is an HTTP OPTIONS or HEAD request.
73+
if IGNORED_HTTP_METHODS.include?(env["REQUEST_METHOD"])
74+
options.merge!(sampled: false)
75+
end
76+
6677
transaction = Sentry.continue_trace(env, **options)
6778
Sentry.start_transaction(transaction: transaction, custom_sampling_context: { env: env }, **options)
6879
end

sentry-ruby/lib/sentry/transaction.rb

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def set_initial_sample_decision(sampling_context:)
188188
return
189189
end
190190

191+
# This block would return either true / false (if parent was sampled or not)
192+
# or the sample rate from the configuration, presumably between 0 and 1.
191193
sample_rate =
192194
if @traces_sampler.is_a?(Proc)
193195
@traces_sampler.call(sampling_context)

sentry-ruby/spec/sentry/rack/capture_exceptions_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def inspect
178178
expect(event.breadcrumbs.count).to eq(1)
179179
expect(event.breadcrumbs.peek.message).to eq("request breadcrumb")
180180
end
181+
181182
it "doesn't pollute the top-level scope" do
182183
request_1 = lambda do |e|
183184
Sentry.configure_scope { |s| s.set_tags({tag_1: "foo"}) }
@@ -192,6 +193,7 @@ def inspect
192193
expect(event.tags).to eq(tag_1: "foo")
193194
expect(Sentry.get_current_scope.tags).to eq(tag_1: "don't change me")
194195
end
196+
195197
it "doesn't pollute other request's scope" do
196198
request_1 = lambda do |e|
197199
Sentry.configure_scope { |s| s.set_tags({tag_1: "foo"}) }

0 commit comments

Comments
 (0)