diff --git a/lib/datadog/core/telemetry/logging.rb b/lib/datadog/core/telemetry/logging.rb index ed040c9de50..eba7e87719c 100644 --- a/lib/datadog/core/telemetry/logging.rb +++ b/lib/datadog/core/telemetry/logging.rb @@ -2,6 +2,8 @@ require_relative 'event' +require 'pathname' + module Datadog module Core module Telemetry @@ -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 @@ -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(',') diff --git a/sig/datadog/core/telemetry/logging.rbs b/sig/datadog/core/telemetry/logging.rbs index 324ac7112c4..57cd4ae8d37 100644 --- a/sig/datadog/core/telemetry/logging.rbs +++ b/sig/datadog/core/telemetry/logging.rbs @@ -3,7 +3,8 @@ module Datadog module Telemetry module Logging module DatadogStackTrace - REGEX: Regexp + GEM_ROOT: String + def self.from: (Exception exception) -> String? end diff --git a/spec/datadog/core/telemetry/logging_spec.rb b/spec/datadog/core/telemetry/logging_spec.rb index 880c7c90a71..d96668d0c8b 100644 --- a/spec/datadog/core/telemetry/logging_spec.rb +++ b/spec/datadog/core/telemetry/logging_spec.rb @@ -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`',