Module: SentryLogging

Extended by:
SentryLogging
Included in:
Accountable, ApplicationController, Apps::Client, BB::Client, BB::GenerateReportRequestForm, BGS::AwardsService, BGS::BenefitClaim, BGS::DependencyVerificationService, BGS::DependentService, BGS::Exceptions::BGSErrors, BGS::FlashUpdater, BGS::Form674, BGS::Form686c, BGS::PaymentService, BGS::People::Service, BGS::Service, BGS::ServiceException, BGS::SubmitForm674Job, BGS::SubmitForm686cJob, BGS::UploadedDocumentService, BGS::VnpVeteran, BID::Service, BenefitsClaims::ServiceException, BenefitsEducation::Response, BenefitsEducation::Service, BenefitsIntakeService::Service, BenefitsReferenceData::Service, BenefitsReferenceData::ServiceException, Caseflow::Service, CentralMail::SubmitCentralForm686cJob, Chip::Service, Chip::ServiceException, ClaimFastTracking::MaxCfiMetrics, Common::Client::Base, Common::Client::Concerns::MHVSessionBasedClient, Common::Client::Concerns::MhvLockedSessionClient, Common::Client::Middleware::Response::MHVXmlHtmlErrors, Common::PdfHelpers, CopayNotifications::NewStatementNotificationJob, CypressViewportUpdater::GithubService, CypressViewportUpdater::GoogleAnalyticsReports, DebtManagementCenter::DebtLetterDownloader, DebtManagementCenter::PaymentsService, DebtManagementCenter::StatementIdentifierService, DebtManagementCenter::VANotifyEmailJob, DecisionReview::PdfValidation::Service, DecisionReview::Service, DecisionReview::ServiceException, DecisionReviewV1::Service, DecisionReviewV1::ServiceException, DeleteOldTransactionsJob, EVSS::BaseService, EVSS::DisabilityCompensationForm::ServiceException, EVSS::DocumentUpload, EVSSClaimDocument, EVSSClaimService, EVSSClaimServiceAsync, EducationForm::Create10203ApplicantDecisionLetters, EducationForm::CreateDailySpoolFiles, EducationForm::Process10203Submissions, Form1010Ezr::Service, Form1010cg::Service, Form1010cg::SubmissionJob, Form526ConfirmationEmailJob, Form526Submission, Form526SubmissionFailedEmailJob, FormAttachment, FormAttachmentCreate, FormProfile, Forms::Client, HCA::EzrSubmissionJob, HCA::SOAPParser, HealthCareApplication, IAMUserIdentity, LGY::Service, Lighthouse::DocumentUpload, Lighthouse::PollForm526Pdf, Lighthouse::ServiceException, Lighthouse::SubmitBenefitsIntakeClaim, LighthouseDocument, MDOT::Exceptions::ServiceException, MPI::Responses::AddParser, MPI::Responses::ProfileParser, MPI::Services::AddPersonResponseCreator, MPI::Services::FindProfileResponseCreator, MPIData, PagerDuty::CacheGlobalDowntime, PagerDuty::PollMaintenanceWindows, Preneeds::Middleware::Response::EoasXmlErrors, RES::Ch31Form, Rx::Middleware::Response::RxFailedStation, SAML::PostURLService, SAML::User, SAML::UserAttributes::SSOe, SavedClaim::DependencyVerificationClaim, SavedClaim::EducationBenefits::VA10203, SavedClaim::EducationCareerCounselingClaim, SavedClaim::VeteranReadinessEmploymentClaim, SentryLogging, Session, Sidekiq::Form526BackupSubmissionProcess::Submit, Sidekiq::Form526JobStatusTracker::JobTracker, SignIn::ApplicationController, SignIn::ServiceAccountApplicationController, UserSessionForm, V0::Preneeds::BurialFormsController, V1::Post911GIBillStatusesController, VANotifyDdEmailJob, VANotifyEmailJob, VAProfile::ContactInformation::TransactionResponse, VAProfile::V2::ContactInformation::TransactionResponse, VAProfileRedis::Cache, VAProfileRedis::V2::Cache, VBMS::SubmitDependentsPdfJob, VRE::Ch31Form, VRE::CreateCh31SubmissionsReportJob, VRE::Submit1900Job
Defined in:
lib/sentry_logging.rb

Instance Method Summary collapse

Instance Method Details

#log_exception_to_sentry(exception, extra_context = {}, tags_context = {}, level = 'error') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sentry_logging.rb', line 19

def log_exception_to_sentry(exception, extra_context = {}, tags_context = {}, level = 'error')
  level = normalize_level(level, exception)

  if Settings.sentry.dsn.present?
    (extra_context, tags_context)
    Sentry.capture_exception(exception.cause.presence || exception, level:)
  end

  if exception.is_a? Common::Exceptions::BackendServiceException
    rails_logger(level, exception.message, exception.errors, exception.backtrace)
  else
    rails_logger(level, "#{exception.message}.")
  end
  rails_logger(level, exception.backtrace.join("\n")) unless exception.backtrace.nil?
end

#log_message_to_sentry(message, level, extra_context = {}, tags_context = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/sentry_logging.rb', line 8

def log_message_to_sentry(message, level, extra_context = {}, tags_context = {})
  level = normalize_level(level, nil)
  formatted_message = extra_context.empty? ? message : "#{message} : #{extra_context}"
  rails_logger(level, formatted_message)

  if Settings.sentry.dsn.present?
    (extra_context, tags_context)
    Sentry.capture_message(message, level:)
  end
end

#non_nil_hash?(h) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/sentry_logging.rb', line 63

def non_nil_hash?(h)
  h.is_a?(Hash) && !h.empty?
end

#normalize_level(level, exception) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sentry_logging.rb', line 35

def normalize_level(level, exception)
  # https://docs.sentry.io/platforms/ruby/usage/set-level/
  # valid sentry levels: log, debug, info, warning, error, fatal
  level = case exception
          when Pundit::NotAuthorizedError
            'info'
          when Common::Exceptions::BaseError
            exception.sentry_type.to_s
          else
            level.to_s
          end

  return 'warning' if level == 'warn'

  level
end

#rails_logger(level, message, errors = nil, backtrace = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/sentry_logging.rb', line 52

def rails_logger(level, message, errors = nil, backtrace = nil)
  # rails logger uses 'warn' instead of 'warning'
  level = 'warn' if level == 'warning'
  if errors.present?
    error_details = errors.first.attributes.compact.reject { |_k, v| v.try(:empty?) }
    Rails.logger.send(level, message, error_details.merge(backtrace:))
  else
    Rails.logger.send(level, message)
  end
end

#set_sentry_metadata(extra_context, tags_context) ⇒ Object (private)



69
70
71
72
# File 'lib/sentry_logging.rb', line 69

def (extra_context, tags_context)
  Sentry.set_extras(extra_context) if non_nil_hash?(extra_context)
  Sentry.set_tags(tags_context) if non_nil_hash?(tags_context)
end