sentry.rb 1.83 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
module Gitlab
  module Sentry
    def self.enabled?
6 7
      (Rails.env.production? || Rails.env.development?) &&
        Gitlab::CurrentSettings.sentry_enabled?
8 9 10
    end

    def self.context(current_user = nil)
11
      return unless enabled?
12

13
      Raven.tags_context(default_tags)
14

15 16 17 18
      if current_user
        Raven.user_context(
          id: current_user.id,
          email: current_user.email,
19
          username: current_user.username
20 21 22 23
        )
      end
    end

24 25 26 27 28 29 30 31 32
    # This can be used for investigating exceptions that can be recovered from in
    # code. The exception will still be raised in development and test
    # environments.
    #
    # That way we can track down these exceptions with as much information as we
    # need to resolve them.
    #
    # Provide an issue URL for follow up.
    def self.track_exception(exception, issue_url: nil, extra: {})
33 34
      track_acceptable_exception(exception, issue_url: issue_url, extra: extra)

35
      raise exception if should_raise_for_dev?
36 37 38 39 40 41 42
    end

    # This should be used when you do not want to raise an exception in
    # development and test. If you need development and test to behave
    # just the same as production you can use this instead of
    # track_exception.
    def self.track_acceptable_exception(exception, issue_url: nil, extra: {})
43 44 45 46
      if enabled?
        extra[:issue_url] = issue_url if issue_url
        context # Make sure we've set everything we know in the context

47
        Raven.capture_exception(exception, tags: default_tags, extra: extra)
48 49 50
      end
    end

51
    def self.should_raise_for_dev?
52 53
      Rails.env.development? || Rails.env.test?
    end
54 55 56 57 58 59 60

    def self.default_tags
      {
        Labkit::Correlation::CorrelationId::LOG_KEY.to_sym => Labkit::Correlation::CorrelationId.current_id,
        locale: I18n.locale
      }
    end
61 62
  end
end