Skip to content

Commit

Permalink
Migrate from sentry-raven to sentry-ruby (dependabot#8878)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Jan 25, 2024
1 parent 8365b87 commit b2703ab
Show file tree
Hide file tree
Showing 26 changed files with 303 additions and 165 deletions.
2 changes: 1 addition & 1 deletion common/lib/dependabot/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def initialize(message:, error_context:, error_class: nil, trace: nil)
end

sig { returns(T::Hash[Symbol, T.untyped]) }
def raven_context
def sentry_context
{ fingerprint: [@fingerprint], extra: @error_context.except(:stderr_output, :fingerprint) }
end
end
Expand Down
2 changes: 1 addition & 1 deletion npm_and_yarn/lib/dependabot/npm_and_yarn/file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(message:, error_context:)
@error_context = error_context
end

def raven_context
def sentry_context
{ extra: @error_context }
end
end
Expand Down
23 changes: 0 additions & 23 deletions sorbet/rbi/shims/sentry-raven.rbi

This file was deleted.

55 changes: 55 additions & 0 deletions sorbet/rbi/shims/sentry-ruby.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# typed: strong
# frozen_string_literal: true

module Sentry
class << self
sig { params(_blk: T.proc.params(arg0: Sentry::Configuration).void).void }
def init(&_blk); end

sig { params(exception: Exception, options: T.untyped).void }
def capture_exception(exception, **options); end
end

class Configuration
sig { returns(T.nilable(::Logger)) }
attr_accessor :logger

sig { returns(T.nilable(String)) }
attr_accessor :project_root

sig { returns(T.nilable(::Regexp)) }
attr_accessor :app_dirs_pattern

sig { returns(T::Boolean) }
attr_accessor :propagate_traces

sig do
params(
value: T.proc
.params(
event: ::Sentry::Event,
hint: T::Hash[Symbol, T.untyped]
)
.returns(::Sentry::Event)
).void
end
def before_send=(value); end
end

class Event; end

class ErrorEvent < ::Sentry::Event
sig { returns(::Sentry::ExceptionInterface) }
attr_reader :exception
end

class ExceptionInterface
sig { returns(T::Array[::Sentry::SingleExceptionInterface]) }
attr_reader :values
end

class SingleExceptionInterface
sig { returns(String) }
attr_accessor :value
end
end
2 changes: 0 additions & 2 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
# typed: false

module ::Azure::Error::NotFound; end
module ::Raven; end
class ::Raven::Processor; end
module Bundler::CompactIndexClient::Updater; end
module Bundler::SolveFailure; end
module Dependabot::NpmAndYarn::FileFetcher::Pysch::SyntaxError; end
Expand Down
2 changes: 1 addition & 1 deletion updater/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ gem "opentelemetry-instrumentation-faraday", "~> 0.23"
gem "opentelemetry-instrumentation-http", "~> 0.23"
gem "opentelemetry-instrumentation-net_http", "~> 0.22"
gem "opentelemetry-sdk", "~> 1.3"
gem "sentry-raven", "~> 3.1"
gem "sentry-ruby", "~> 5.1"
gem "terminal-table", "~> 3.0.2"

group :test do
Expand Down
7 changes: 4 additions & 3 deletions updater/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ GEM
base64 (0.1.1)
citrus (3.0.2)
commonmarker (0.23.10)
concurrent-ruby (1.2.3)
crack (0.4.5)
rexml
debug (1.8.0)
Expand Down Expand Up @@ -321,8 +322,8 @@ GEM
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (>= 0.17.3, < 3)
sentry-raven (3.1.2)
faraday (>= 1.0)
sentry-ruby (5.16.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
sorbet-runtime (0.5.11193)
stackprof (0.2.25)
stringio (3.0.8)
Expand Down Expand Up @@ -384,7 +385,7 @@ DEPENDENCIES
rubocop (~> 1.58.0)
rubocop-performance (~> 1.19.0)
rubocop-sorbet (~> 0.7.3)
sentry-raven (~> 3.1)
sentry-ruby (~> 5.1)
stackprof (~> 0.2.16)
terminal-table (~> 3.0.2)
turbo_tests (~> 2.2.0)
Expand Down
10 changes: 6 additions & 4 deletions updater/bin/fetch_files.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# typed: false
# typed: strict
# frozen_string_literal: true

$LOAD_PATH.unshift(__dir__ + "/../lib")
require "sorbet-runtime"

$LOAD_PATH.unshift(T.must(__dir__) + "/../lib")

$stdout.sync = true

require "raven"
require "sentry-ruby"
require "dependabot/setup"
require "dependabot/file_fetcher_command"
require "debug" if ENV["DEBUG"]
Expand All @@ -16,7 +18,7 @@ class UpdaterKilledError < StandardError; end
puts "Received SIGTERM"
error = UpdaterKilledError.new("Updater process killed with SIGTERM")
tags = { "gh.dependabot_api.update_job.id": ENV.fetch("DEPENDABOT_JOB_ID", nil) }
Raven.capture_exception(error, tags: tags)
Sentry.capture_exception(error, tags: tags)
exit
end

Expand Down
4 changes: 2 additions & 2 deletions updater/bin/update_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

$stdout.sync = true

require "raven"
require "sentry-ruby"
require "dependabot/setup"
require "dependabot/update_files_command"
require "debug" if ENV["DEBUG"]
Expand All @@ -16,7 +16,7 @@ class UpdaterKilledError < StandardError; end
puts "Received SIGTERM"
error = UpdaterKilledError.new("Updater process killed with SIGTERM")
tags = { "gh.dependabot_api.update_job.id": ENV.fetch("DEPENDABOT_JOB_ID", nil) }
Raven.capture_exception(error, tags: tags)
Sentry.capture_exception(error, tags: tags)
exit
end

Expand Down
1 change: 0 additions & 1 deletion updater/lib/dependabot/base_command.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# typed: true
# frozen_string_literal: true

require "raven"
require "dependabot/api_client"
require "dependabot/service"
require "dependabot/logger"
Expand Down
39 changes: 11 additions & 28 deletions updater/lib/dependabot/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
# typed: strong
# frozen_string_literal: true

require "raven"
require "sorbet-runtime"
require "dependabot/sentry/exception_sanitizer_processor"
require "dependabot/sentry/sentry_context_processor"

# ExceptionSanitizer filters potential secrets/PII from exception payloads
class ExceptionSanitizer < Raven::Processor
extend T::Sig
module Dependabot
module Sentry
extend T::Sig

REPO = %r{[\w.\-]+/([\w.\-]+)}
PATTERNS = T.let(
{
auth_token: /(?:authorization|bearer):? (\w+)/i,
repo: %r{https://api\.github\.com/repos/#{REPO}|https://github\.com/#{REPO}|git@github\.com:#{REPO}}
}.freeze,
T::Hash[Symbol, Regexp]
)

sig do
params(data: T::Hash[Symbol, T.nilable(T::Hash[Symbol, T::Array[T::Hash[Symbol, String]]])])
.returns(T::Hash[Symbol, T.untyped])
end
def process(data)
return data unless data.dig(:exception, :values)

T.must(data[:exception])[:values] = T.must(data.dig(:exception, :values)).map do |e|
PATTERNS.each do |key, regex|
e[:value] = T.must(e[:value]).gsub(regex) do |match|
match.sub(/#{T.must(Regexp.last_match).captures.compact.first}\z/, "[FILTERED_#{key.to_s.upcase}]")
end
# The default processor chain.
# This chain is applied in the order of the array.
sig { params(event: ::Sentry::Event, hint: T::Hash[Symbol, T.untyped]).returns(::Sentry::Event) }
def self.process_chain(event, hint)
[ExceptionSanitizer, SentryContext].each(&:new).reduce(event) do |acc, processor|
processor.new.process(acc, hint)
end
e
end

data
end
end
43 changes: 43 additions & 0 deletions updater/lib/dependabot/sentry/exception_sanitizer_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# typed: strong
# frozen_string_literal: true

require "sentry-ruby"
require "sorbet-runtime"

require "dependabot/sentry/processor"

# ExceptionSanitizer filters potential secrets/PII from exception payloads
class ExceptionSanitizer < ::Dependabot::Sentry::Processor
extend T::Sig

REPO = %r{[\w.\-]+/([\w.\-]+)}
PATTERNS = T.let(
{
auth_token: /(?:authorization|bearer):? (\w+)/i,
repo: %r{https://api\.github\.com/repos/#{REPO}|https://github\.com/#{REPO}|git@github\.com:#{REPO}}
}.freeze,
T::Hash[Symbol, Regexp]
)

sig do
override
.params(
event: ::Sentry::Event,
_hint: T::Hash[Symbol, T.untyped]
)
.returns(::Sentry::Event)
end
def process(event, _hint)
return event unless event.is_a?(::Sentry::ErrorEvent)

event.exception.values.each do |e|
PATTERNS.each do |key, regex|
e.value = e.value.gsub(regex) do |match|
match.sub(/#{T.must(Regexp.last_match).captures.compact.first}\z/, "[FILTERED_#{key.to_s.upcase}]")
end
end
end

event
end
end
26 changes: 26 additions & 0 deletions updater/lib/dependabot/sentry/processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# typed: strong
# frozen_string_literal: true

require "sorbet-runtime"

module Dependabot
module Sentry
class Processor
extend T::Sig
extend T::Helpers

abstract!

# Process an event before it is sent to Sentry
sig do
abstract
.params(
event: ::Sentry::Event,
hint: T::Hash[Symbol, T.untyped]
)
.returns(::Sentry::Event)
end
def process(event, hint); end
end
end
end
26 changes: 26 additions & 0 deletions updater/lib/dependabot/sentry/sentry_context_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# typed: strict
# frozen_string_literal: true

require "sentry-ruby"
require "sorbet-runtime"

require "dependabot/sentry/processor"

class SentryContext < ::Dependabot::Sentry::Processor
sig do
override
.params(
event: ::Sentry::Event,
hint: T::Hash[Symbol, T.untyped]
)
.returns(::Sentry::Event)
end
def process(event, hint)
if (exception = hint[:exception]) && exception.respond_to?(:sentry_context)
exception.sentry_context&.each do |key, value|
event.send("#{key}=", value)
end
end
event
end
end
Loading

0 comments on commit b2703ab

Please sign in to comment.