Class: Upfluence::ErrorLogger::Sentry

Inherits:
Object
  • Object
show all
Defined in:
lib/upfluence/error_logger/sentry.rb

Constant Summary collapse

EXCLUDED_ERRORS =
(::Sentry::Configuration::IGNORE_DEFAULT + ['Identity::Thrift::Forbidden'])
MAX_TAG_SIZE =
8 * 1024

Instance Method Summary collapse

Constructor Details

#initializeSentry

Returns a new instance of Sentry.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/upfluence/error_logger/sentry.rb', line 9

def initialize
  @tag_extractors = []

  ::Sentry.init do |config|
    config.send_default_pii = true
    config.dsn = ENV.fetch('SENTRY_DSN', nil)
    config.environment = Upfluence.env
    config.excluded_exceptions = EXCLUDED_ERRORS
    config.logger = Upfluence.logger
    config.release = "#{ENV.fetch('PROJECT_NAME', nil)}-#{ENV.fetch('SEMVER_VERSION', nil)}"
    config.enable_tracing = false
    config.auto_session_tracking = false
  end

  ::Sentry.set_tags(
    { unit_name: unit_name, unit_type: unit_type }.select { |_, v| v }
  )

  ::Sentry.with_scope do |scope|
    scope.add_event_processor do |event, hint|
      tags = @tag_extractors.map(&:extract).compact.reduce({}, &:merge)

      exc = hint[:exception]

      tags.merge!(exc.tags) if exc.respond_to? :tags

      tx_name = transaction_name(tags)

      event.transaction = tx_name if tx_name
      event.extra.merge!(prepare_extra(tags))

      event
    end
  end
end

Instance Method Details

#append_tag_extractors(klass) ⇒ Object



45
46
47
# File 'lib/upfluence/error_logger/sentry.rb', line 45

def append_tag_extractors(klass)
  @tag_extractors << klass
end

#ignore_exception(*klss) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/upfluence/error_logger/sentry.rb', line 78

def ignore_exception(*klss)
  klss.each do |kls|
    case kls.class
    when Class
      ::Sentry.configuration.excluded_exceptions << kls.name
    when String
      ::Sentry.configuration.excluded_exceptions << kls
    else
      Upfluence.logger.warn "Unexcepted argument for ignore_exception #{kls}"
    end
  end
end

#middlewareObject



74
75
76
# File 'lib/upfluence/error_logger/sentry.rb', line 74

def middleware
  ::Sentry::Rack::CaptureExceptions
end

#notify(error, *args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/upfluence/error_logger/sentry.rb', line 49

def notify(error, *args)
  ::Sentry.with_scope do |scope|
    context = args.reduce({}) do |acc, arg|
      v = if arg.is_a?(Hash)
            arg
          else
            key = acc.empty? ? 'method' : "arg_#{acc.length}"
            { key => arg.inspect }
          end

      acc.merge(v)
    end

    scope.set_extras(prepare_extra(context))

    ::Sentry.capture_exception(error)
  end
rescue ::Sentry::Error => e
  Upfluence.logger.warning e.message
end

#user=(user) ⇒ Object



70
71
72
# File 'lib/upfluence/error_logger/sentry.rb', line 70

def user=(user)
  ::Sentry.set_user(id: user.id, email: user.email)
end