Skip to content

Commit

Permalink
Add new strip_backtrace_load_path boolean config (default true) to …
Browse files Browse the repository at this point in the history
…enable disabling load path stripping (#2409)
  • Loading branch information
sl0thentr0py authored Sep 20, 2024
1 parent 05c4675 commit ab8361f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
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 @@ class Frame < Interface
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

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

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

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 @@ class StacktraceBuilder
# @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 @@ def metrics_code_location(unparsed_line)
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)
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

0 comments on commit ab8361f

Please sign in to comment.