Skip to content

Commit

Permalink
Change path matching with gem root
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCTHsu committed Aug 29, 2024
1 parent 9f863d7 commit c67c666
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/datadog/core/telemetry/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require_relative 'event'

require 'pathname'

module Datadog
module Core
module Telemetry
Expand All @@ -23,9 +25,7 @@ module Logging

# Extract datadog stack trace from the exception
module DatadogStackTrace
# Typically, `lib` is under `#{gem_name}-#{version}/`
# but not the case when providing a bundler custom path in `Gemfile`
REGEX = %r{/lib/datadog/}.freeze
GEM_ROOT = Pathname.new("#{__dir__}/../../../..").cleanpath.to_s

def self.from(exception)
backtrace = exception.backtrace
Expand All @@ -35,12 +35,12 @@ def self.from(exception)

stack_trace = +''
backtrace.each do |line|
stack_trace << if line.match?(REGEX)
# Removing host related information
line.sub(/^.*?(#{REGEX})/o, '\1') << ','
stack_trace << if line.start_with?(GEM_ROOT)
line[GEM_ROOT.length..-1] || ''
else
'REDACTED,'
'REDACTED'
end
stack_trace << ','
end

stack_trace.chomp(',')
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/core/telemetry/logging.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Datadog
module Telemetry
module Logging
module DatadogStackTrace
REGEX: Regexp
GEM_ROOT: String

def self.from: (Exception exception) -> String?
end

Expand Down
4 changes: 3 additions & 1 deletion spec/datadog/core/telemetry/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@
end

it 'returns redacted stack trace' do
gem_root = Datadog::Core::Telemetry::Logging::DatadogStackTrace::GEM_ROOT

exception = StandardError.new('Yo!')
exception.set_backtrace(
[
'/usr/local/bundle/gems/datadog-2.3.0.beta1/lib/datadog/core/telemetry/logging.rb:1 in `report`',
"#{gem_root}/lib/datadog/core/telemetry/logging.rb:1 in `report`",
'/foo/bar/baz.rb:1 in `baz`',
'/foo/bar.rb:1 in `bar`',
'/foo.rb:1 in `foo`',
Expand Down

0 comments on commit c67c666

Please sign in to comment.