Skip to content

Add new strip_backtrace_load_path boolean config (default true) to enable disabling load path stripping #2409

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

Merged
merged 1 commit into from
Sep 20, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Gemfile.lock
.ruby-gemset
.idea
*.rdb
.rgignore

examples/**/node_modules
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Support human readable intervals in `sidekiq-cron` ([#2387](https://github.com/getsentry/sentry-ruby/pull/2387))
- Set default app dirs pattern ([#2390](https://github.com/getsentry/sentry-ruby/pull/2390))
- Verifies presence of client before adding a breadcrumb ([#2394](https://github.com/getsentry/sentry-ruby/pull/2394))
- Add new `strip_backtrace_load_path` boolean config (default true) to enable disabling load path stripping ([#2409](https://github.com/getsentry/sentry-ruby/pull/2409))

### Bug Fixes

Expand Down
9 changes: 8 additions & 1 deletion sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ def capture_exception_frame_locals=(value)
# @return [String]
attr_accessor :project_root

# Whether to strip the load path while constructing the backtrace frame filename.
# Defaults to true.
# @return [Boolean]
attr_accessor :strip_backtrace_load_path

# Insert sentry-trace to outgoing requests' headers
# @return [Boolean]
attr_accessor :propagate_traces
Expand Down Expand Up @@ -359,6 +364,7 @@ def initialize
self.background_worker_threads = (processor_count / 2.0).ceil
self.background_worker_max_queue = BackgroundWorker::DEFAULT_MAX_QUEUE
self.backtrace_cleanup_callback = nil
self.strip_backtrace_load_path = true
self.max_breadcrumbs = BreadcrumbBuffer::DEFAULT_SIZE
self.breadcrumbs_logger = []
self.context_lines = 3
Expand Down Expand Up @@ -563,7 +569,8 @@ def stacktrace_builder
app_dirs_pattern: @app_dirs_pattern,
linecache: @linecache,
context_lines: @context_lines,
backtrace_cleanup_callback: @backtrace_cleanup_callback
backtrace_cleanup_callback: @backtrace_cleanup_callback,
strip_backtrace_load_path: @strip_backtrace_load_path
)
end

Expand Down
4 changes: 3 additions & 1 deletion sentry-ruby/lib/sentry/interfaces/stacktrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
attr_accessor :abs_path, :context_line, :function, :in_app, :filename,
:lineno, :module, :pre_context, :post_context, :vars

def initialize(project_root, line)
def initialize(project_root, line, strip_backtrace_load_path = true)
@project_root = project_root
@strip_backtrace_load_path = strip_backtrace_load_path

Check warning on line 32 in sentry-ruby/lib/sentry/interfaces/stacktrace.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/interfaces/stacktrace.rb#L32

Added line #L32 was not covered by tests

@abs_path = line.file
@function = line.method if line.method
Expand All @@ -44,6 +45,7 @@

def compute_filename
return if abs_path.nil?
return abs_path unless @strip_backtrace_load_path

Check warning on line 48 in sentry-ruby/lib/sentry/interfaces/stacktrace.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/interfaces/stacktrace.rb#L48

Added line #L48 was not covered by tests

prefix =
if under_project_root? && in_app
Expand Down
17 changes: 15 additions & 2 deletions sentry-ruby/lib/sentry/interfaces/stacktrace_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@
# @return [Proc, nil]
attr_reader :backtrace_cleanup_callback

# @return [Boolean]
attr_reader :strip_backtrace_load_path

# @param project_root [String]
# @param app_dirs_pattern [Regexp, nil]
# @param linecache [LineCache]
# @param context_lines [Integer, nil]
# @param backtrace_cleanup_callback [Proc, nil]
# @param strip_backtrace_load_path [Boolean]
# @see Configuration#project_root
# @see Configuration#app_dirs_pattern
# @see Configuration#linecache
# @see Configuration#context_lines
# @see Configuration#backtrace_cleanup_callback
def initialize(project_root:, app_dirs_pattern:, linecache:, context_lines:, backtrace_cleanup_callback: nil)
# @see Configuration#strip_backtrace_load_path
def initialize(
project_root:,
app_dirs_pattern:,
linecache:,
context_lines:,
backtrace_cleanup_callback: nil,
strip_backtrace_load_path: true
)
@project_root = project_root
@app_dirs_pattern = app_dirs_pattern
@linecache = linecache
@context_lines = context_lines
@backtrace_cleanup_callback = backtrace_cleanup_callback
@strip_backtrace_load_path = strip_backtrace_load_path
end

# Generates a StacktraceInterface with the given backtrace.
Expand Down Expand Up @@ -73,7 +86,7 @@
private

def convert_parsed_line_into_frame(line)
frame = StacktraceInterface::Frame.new(project_root, line)
frame = StacktraceInterface::Frame.new(project_root, line, strip_backtrace_load_path)

Check warning on line 89 in sentry-ruby/lib/sentry/interfaces/stacktrace_builder.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/interfaces/stacktrace_builder.rb#L89

Added line #L89 was not covered by tests
frame.set_context(linecache, context_lines) if context_lines
frame
end
Expand Down
15 changes: 15 additions & 0 deletions sentry-ruby/spec/sentry/interfaces/stacktrace_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@
expect(second_frame.post_context).to eq(["end\n", nil, nil])
end

context "when strip_backtrace_load_path is false" do
let(:configuration) do
Sentry::Configuration.new.tap do |config|
config.project_root = fixture_root
config.strip_backtrace_load_path = false
end
end

it "does not strip load paths for filenames" do
interface = subject.build(backtrace: backtrace)
expect(interface.frames.first.filename).to eq(fixture_file)
expect(interface.frames.last.filename).to eq(fixture_file)
end
end

context "with block argument" do
it "removes the frame if it's evaluated as nil" do
interface = subject.build(backtrace: backtrace) do |frame|
Expand Down
10 changes: 10 additions & 0 deletions sentry-ruby/spec/sentry/interfaces/stacktrace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@
expect(second_frame.function).to eq("save_user")
expect(second_frame.lineno).to eq(5)
end

it "does not strip load path when strip_backtrace_load_path is false" do
first_frame = Sentry::StacktraceInterface::Frame.new(configuration.project_root, lines.first, false)
expect(first_frame.filename).to eq(first_frame.abs_path)
expect(first_frame.filename).to eq(raw_lines.first.split(':').first)

second_frame = Sentry::StacktraceInterface::Frame.new(configuration.project_root, lines.last, false)
expect(second_frame.filename).to eq(second_frame.abs_path)
expect(second_frame.filename).to eq(raw_lines.last.split(':').first)
end
end
end
Loading