Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to configure their asset-skipping pattern #1915

Merged
merged 2 commits into from
Oct 9, 2022
Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## Unreleased

### Features

- Allow users to configure their asset-skipping pattern [#1915](https://github.com/getsentry/sentry-ruby/pull/1915)

Users can now configure their own pattern to skip asset requests' transactions

```rb
Sentry.init do |config|
config.rails.assets_regexp = /my_regexp/
end
```

### Bug Fixes

- `Sentry::BackgroundWorker` will release `ActiveRecord` connection pool only when the `ActiveRecord` connection is established
Expand Down
8 changes: 4 additions & 4 deletions sentry-rails/lib/sentry/rails/capture_exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Sentry
module Rails
class CaptureExceptions < Sentry::Rack::CaptureExceptions
def initialize(app)
def initialize(_)
super

if defined?(::Sprockets::Rails)
@assets_regex = %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
if Sentry.initialized?
@assets_regexp = Sentry.configuration.rails.assets_regexp
end
end

Expand Down Expand Up @@ -36,7 +36,7 @@ def start_transaction(env, scope)

options = { name: scope.transaction_name, source: scope.transaction_source, op: transaction_op }

if @assets_regex && scope.transaction_name.match?(@assets_regex)
if @assets_regexp && scope.transaction_name.match?(@assets_regexp)
options.merge!(sampled: false)
end

Expand Down
18 changes: 18 additions & 0 deletions sentry-rails/lib/sentry/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@ class Configuration

attr_accessor :tracing_subscribers

# sentry-rails by default skips asset request' transactions by checking if the path matches
#
# ```rb
# %r(\A/{0,2}#{::Rails.application.config.assets.prefix})
# ```
#
# If you want to use a different pattern, you can configure the `assets_regexp` option like:
#
# ```rb
# Sentry.init do |config|
# config.rails.assets_regexp = /my_regexp/
# end
# ```
attr_accessor :assets_regexp

def initialize
@register_error_subscriber = false
@report_rescued_exceptions = true
@skippable_job_adapters = []
@assets_regexp = if defined?(::Sprockets::Rails)
%r(\A/{0,2}#{::Rails.application.config.assets.prefix})
end
@tracing_subscribers = Set.new([
Sentry::Rails::Tracing::ActionControllerSubscriber,
Sentry::Rails::Tracing::ActionViewSubscriber,
Expand Down
50 changes: 39 additions & 11 deletions sentry-rails/spec/sentry/rails/tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,50 @@
::Logger.new(string_io)
end

before do
require "sprockets/railtie"
context "with default setup" do
before do
require "sprockets/railtie"

make_basic_app do |config, app|
app.config.public_file_server.enabled = true
config.traces_sample_rate = 1.0
config.logger = logger
end
end

make_basic_app do |config, app|
app.config.public_file_server.enabled = true
config.traces_sample_rate = 1.0
config.logger = logger
it "doesn't record requests for asset files" do
get "/assets/application-ad022df6f1289ec07a560bb6c9a227ecf7bdd5a5cace5e9a8cdbd50b454931fb.css"

expect(response).to have_http_status(:not_found)
expect(transport.events).to be_empty
expect(string_io.string).not_to match(/\[Tracing\] Starting <rails\.request>/)
end
end

it "doesn't record requests for asset files" do
get "/assets/application-ad022df6f1289ec07a560bb6c9a227ecf7bdd5a5cace5e9a8cdbd50b454931fb.css"
context "with custom assets_regexp config" do
before do
require "sprockets/railtie"

expect(response).to have_http_status(:not_found)
expect(transport.events).to be_empty
expect(string_io.string).not_to match(/\[Tracing\] Starting <rails\.request>/)
make_basic_app do |config, app|
app.config.public_file_server.enabled = true
config.traces_sample_rate = 1.0
config.logger = logger
config.rails.assets_regexp = %r(/foo/)
end
end

it "accepts customized asset path patterns" do
get "/foo/application-ad022df6f1289ec07a560bb6c9a227ecf7bdd5a5cace5e9a8cdbd50b454931fb.css"

expect(response).to have_http_status(:not_found)
expect(transport.events).to be_empty
expect(string_io.string).not_to match(/\[Tracing\] Starting <rails\.request>/)

get "/assets/application-ad022df6f1289ec07a560bb6c9a227ecf7bdd5a5cace5e9a8cdbd50b454931fb.css"

expect(response).to have_http_status(:not_found)
expect(transport.events.count).to eq(1)
end
end
end

Expand Down