Class: Upfluence::ErrorLogger::Sentry

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

Defined Under Namespace

Classes: RackMiddleware

Constant Summary collapse

EXCLUDED_ERRORS =
(
  ::Sentry::Configuration::IGNORE_DEFAULT + ['ActiveRecord::RecordNotFound']
)
MAX_TAG_SIZE =
8 * 1024

Instance Method Summary collapse

Constructor Details

#initializeSentry

Returns a new instance of Sentry.



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
44
45
# File 'lib/upfluence/error_logger/sentry.rb', line 11

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



47
48
49
# File 'lib/upfluence/error_logger/sentry.rb', line 47

def append_tag_extractors(klass)
  @tag_extractors << klass
end

#ignore_exception(*klss) ⇒ Object



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

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



76
77
78
# File 'lib/upfluence/error_logger/sentry.rb', line 76

def middleware
  RackMiddleware
end

#notify(error, *args) ⇒ Object



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

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



72
73
74
# File 'lib/upfluence/error_logger/sentry.rb', line 72

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