From 4057dd53c5bf5000fe09936e2f7f26822b9225f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Wed, 28 Nov 2018 18:02:23 +0100 Subject: [PATCH 1/7] Refactor Sentry handling Rename methods of Sentry class: - `track_acceptable_exception` => `track_exception`: we just want to capture exception - `track_exception` => `track_and_raise_for_dev_exception`: as said, - `track_and_raise_exception` => we want to capture and re-raise exception Update exception tracking - Remove `extra:` and instead accept hash, - Update documentation to include the best practices, - Remove manual logging of exceptions --- app/controllers/application_controller.rb | 8 +- app/controllers/concerns/issuable_actions.rb | 10 +- app/helpers/icons_helper.rb | 8 +- app/helpers/users_helper.rb | 2 +- app/models/ci/build.rb | 2 +- app/models/ci/persistent_ref.rb | 4 +- .../clusters/applications/elastic_stack.rb | 2 + app/models/clusters/cluster.rb | 2 +- .../clusters/concerns/application_core.rb | 2 +- app/models/concerns/group_descendant.rb | 6 +- .../concerns/storage/legacy_namespace.rb | 6 +- app/models/merge_request.rb | 2 +- app/models/upload.rb | 6 +- app/models/uploads/local.rb | 3 +- app/services/ci/archive_trace_service.rb | 4 +- ...nerate_exposed_artifacts_report_service.rb | 2 +- app/services/ci/prepare_build_service.rb | 2 +- app/services/ci/register_job_service.rb | 4 +- .../applications/base_helm_service.rb | 9 +- .../delete_tags_service.rb | 2 +- app/services/projects/import_service.rb | 4 +- .../proxy_variable_substitution_service.rb | 2 +- app/workers/delete_stored_files_worker.rb | 2 +- .../pages_domain_removal_cron_worker.rb | 2 +- app/workers/run_pipeline_schedule_worker.rb | 4 +- app/workers/stuck_ci_jobs_worker.rb | 4 +- .../forbid_sidekiq_in_transactions.rb | 2 +- config/initializers/sentry.rb | 33 +-- doc/development/logging.md | 62 ++++++ .../ee/clusters/platforms/kubernetes.rb | 2 +- .../jenkins_deprecated_service.rb | 2 +- ee/lib/api/conan_packages.rb | 2 +- ee/lib/ee/gitlab/ci/limit.rb | 2 +- .../license_compliance/license_scanning.rb | 2 +- ee/lib/gitlab/ci/parsers/metrics/generic.rb | 2 +- ee/lib/gitlab/ci/parsers/security/common.rb | 2 +- .../ci/pipeline/chain/limit/activity_spec.rb | 6 +- .../pipeline/chain/limit/job_activity_spec.rb | 6 +- .../ci/pipeline/chain/limit/size_spec.rb | 6 +- lib/api/helpers.rb | 5 +- lib/gitlab/bitbucket_import/importer.rb | 9 +- .../bitbucket_server_import/importer.rb | 33 ++- lib/gitlab/ci/config.rb | 8 +- .../ci/pipeline/chain/config/process.rb | 4 +- lib/gitlab/diff/highlight.rb | 2 +- lib/gitlab/gitaly_client.rb | 3 +- .../importer/pull_request_importer.rb | 10 +- lib/gitlab/gpg.rb | 4 +- .../graphql/calls_gitaly/instrumentation.rb | 2 +- .../query_analyzers/logger_analyzer.rb | 4 +- lib/gitlab/highlight.rb | 2 +- .../import_export/relation_tree_restorer.rb | 7 +- lib/gitlab/import_export/shared.rb | 6 +- lib/gitlab/profiler.rb | 2 + lib/gitlab/sentry.rb | 192 ++++++++++++------ lib/gitlab/sentry/logger.rb | 11 + lib/gitlab/shell.rb | 6 +- .../usage_data_counters/base_counter.rb | 2 +- lib/sentry/client.rb | 6 +- locale/gitlab.pot | 3 - .../projects/issues_controller_spec.rb | 4 +- .../merge_requests_controller_spec.rb | 4 +- spec/lib/gitlab/ci/config_spec.rb | 4 +- spec/lib/gitlab/diff/highlight_spec.rb | 4 +- spec/lib/gitlab/gitaly_client_spec.rb | 5 +- .../importer/pull_request_importer_spec.rb | 2 +- spec/lib/gitlab/gpg_spec.rb | 6 +- spec/lib/gitlab/import_export/shared_spec.rb | 21 +- .../import_export/version_checker_spec.rb | 12 +- spec/lib/gitlab/sentry_spec.rb | 41 ++-- spec/models/ci/build_spec.rb | 2 +- spec/models/clusters/cluster_spec.rb | 4 +- spec/models/concerns/group_descendant_spec.rb | 4 +- spec/models/upload_spec.rb | 3 +- spec/requests/api/graphql_spec.rb | 4 +- spec/requests/api/helpers_spec.rb | 4 +- .../services/ci/archive_trace_service_spec.rb | 4 +- .../ci/create_pipeline_service_spec.rb | 4 +- .../services/ci/prepare_build_service_spec.rb | 4 +- spec/services/ci/register_job_service_spec.rb | 8 +- .../base_helm_service_shared_examples.rb | 14 +- .../ci/archive_traces_cron_worker_spec.rb | 2 +- .../run_pipeline_schedule_worker_spec.rb | 4 +- spec/workers/stuck_ci_jobs_worker_spec.rb | 4 +- 84 files changed, 406 insertions(+), 316 deletions(-) create mode 100644 lib/gitlab/sentry/logger.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1ed8da57927e..ba986c495e21 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,7 +23,7 @@ class ApplicationController < ActionController::Base before_action :validate_user_service_ticket! before_action :check_password_expiration, if: :html_request? before_action :ldap_security_check - before_action :sentry_context + around_action :sentry_context before_action :default_headers before_action :add_gon_variables, if: :html_request? before_action :configure_permitted_parameters, if: :devise_controller? @@ -165,7 +165,7 @@ def auth_user end def log_exception(exception) - Gitlab::Sentry.track_acceptable_exception(exception) + Gitlab::Sentry.track_exception(exception) backtrace_cleaner = request.env["action_dispatch.backtrace_cleaner"] application_trace = ActionDispatch::ExceptionWrapper.new(backtrace_cleaner, exception).application_trace @@ -532,8 +532,8 @@ def impersonator @impersonator ||= User.find(session[:impersonator_id]) if session[:impersonator_id] end - def sentry_context - Gitlab::Sentry.context(current_user) + def sentry_context(&block) + Gitlab::Sentry.with_context(current_user, &block) end def allow_gitaly_ref_name_caching diff --git a/app/controllers/concerns/issuable_actions.rb b/app/controllers/concerns/issuable_actions.rb index 7b7cfa7a5d37..4a1b06bf01ff 100644 --- a/app/controllers/concerns/issuable_actions.rb +++ b/app/controllers/concerns/issuable_actions.rb @@ -98,13 +98,11 @@ def check_destroy_confirmation! error_message = "Destroy confirmation not provided for #{issuable.human_class_name}" exception = RuntimeError.new(error_message) - Gitlab::Sentry.track_acceptable_exception( + Gitlab::Sentry.track_exception( exception, - extra: { - project_path: issuable.project.full_path, - issuable_type: issuable.class.name, - issuable_id: issuable.id - } + project_path: issuable.project.full_path, + issuable_type: issuable.class.name, + issuable_id: issuable.id ) index_path = polymorphic_path([parent, issuable.class]) diff --git a/app/helpers/icons_helper.rb b/app/helpers/icons_helper.rb index 4f73270577fc..b79766e3a654 100644 --- a/app/helpers/icons_helper.rb +++ b/app/helpers/icons_helper.rb @@ -42,11 +42,9 @@ def sprite_file_icons_path end def sprite_icon(icon_name, size: nil, css_class: nil) - if Gitlab::Sentry.should_raise_for_dev? - unless known_sprites.include?(icon_name) - exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg") - raise exception - end + unless known_sprites.include?(icon_name) + exception = ArgumentError.new("#{icon_name} is not a known icon in @gitlab-org/gitlab-svg") + Gitlab::Sentry.track_and_raise_for_dev_exception(exception) end css_classes = [] diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index ef0cb8b4bcb5..ee3c03905efd 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -57,7 +57,7 @@ def user_status(user) unless user.association(:status).loaded? exception = RuntimeError.new("Status was not preloaded") - Gitlab::Sentry.track_exception(exception, extra: { user: user.inspect }) + Gitlab::Sentry.track_and_raise_for_dev_exception(exception, user: user.inspect) end return unless user.status diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 21dbbae17474..2e6b5d68747b 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -289,7 +289,7 @@ def retry(build, current_user) begin build.deployment.drop! rescue => e - Gitlab::Sentry.track_exception(e, extra: { build_id: build.id }) + Gitlab::Sentry.track_and_raise_for_dev_exception(e, build_id: build.id) end true diff --git a/app/models/ci/persistent_ref.rb b/app/models/ci/persistent_ref.rb index 10d7795be2ba..634c03e03262 100644 --- a/app/models/ci/persistent_ref.rb +++ b/app/models/ci/persistent_ref.rb @@ -27,7 +27,7 @@ def create create_ref(sha, path) rescue => e Gitlab::Sentry - .track_acceptable_exception(e, extra: { pipeline_id: pipeline.id }) + .track_exception(e, pipeline_id: pipeline.id) end def delete @@ -38,7 +38,7 @@ def delete # no-op rescue => e Gitlab::Sentry - .track_acceptable_exception(e, extra: { pipeline_id: pipeline.id }) + .track_exception(e, pipeline_id: pipeline.id) end def path diff --git a/app/models/clusters/applications/elastic_stack.rb b/app/models/clusters/applications/elastic_stack.rb index 8589f8c00cbc..9854ad2ea3ea 100644 --- a/app/models/clusters/applications/elastic_stack.rb +++ b/app/models/clusters/applications/elastic_stack.rb @@ -71,6 +71,8 @@ def elasticsearch_client # `proxy_url` could raise an exception because gitlab can not communicate with the cluster. # We check for a nil client in downstream use and behaviour is equivalent to an empty state log_exception(error, :failed_to_create_elasticsearch_client) + + nil end end diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb index 3742bbd84462..2d5b4905bf53 100644 --- a/app/models/clusters/cluster.rb +++ b/app/models/clusters/cluster.rb @@ -335,7 +335,7 @@ def retrieve_connection_status rescue Kubeclient::HttpError => e kubeclient_error_status(e.message) rescue => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { cluster_id: id }) + Gitlab::Sentry.track_exception(e, cluster_id: id) :unknown_failure else diff --git a/app/models/clusters/concerns/application_core.rb b/app/models/clusters/concerns/application_core.rb index 21b98534808b..cc9eafbcdd6b 100644 --- a/app/models/clusters/concerns/application_core.rb +++ b/app/models/clusters/concerns/application_core.rb @@ -76,7 +76,7 @@ def log_exception(error, event) message: error.message }) - Gitlab::Sentry.track_acceptable_exception(error, extra: { cluster_id: cluster&.id, application_id: id }) + Gitlab::Sentry.track_exception(error, cluster_id: cluster&.id, application_id: id) end end end diff --git a/app/models/concerns/group_descendant.rb b/app/models/concerns/group_descendant.rb index 7e6a20c27e84..18f6a7a9c58d 100644 --- a/app/models/concerns/group_descendant.rb +++ b/app/models/concerns/group_descendant.rb @@ -48,11 +48,11 @@ def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded) extras = { parent: parent.inspect, child: child.inspect, - preloaded: preloaded.map(&:full_path) + preloaded: preloaded.map(&:full_path), + issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/49404' } - issue_url = 'https://gitlab.com/gitlab-org/gitlab-foss/issues/49404' - Gitlab::Sentry.track_exception(exception, issue_url: issue_url, extra: extras) + Gitlab::Sentry.track_and_raise_for_dev_exception(exception, extras) end if parent.nil? && hierarchy_top.present? diff --git a/app/models/concerns/storage/legacy_namespace.rb b/app/models/concerns/storage/legacy_namespace.rb index 9c2b0372d54d..b9081f9bbf25 100644 --- a/app/models/concerns/storage/legacy_namespace.rb +++ b/app/models/concerns/storage/legacy_namespace.rb @@ -37,8 +37,10 @@ def move_dir send_update_instructions write_projects_repository_config rescue => e - # Raise if development/test environment, else just notify Sentry - Gitlab::Sentry.track_exception(e, extra: { full_path_before_last_save: full_path_before_last_save, full_path: full_path, action: 'move_dir' }) + Gitlab::Sentry.track_and_raise_for_dev_exception(e, + full_path_before_last_save: full_path_before_last_save, + full_path: full_path, + action: 'move_dir') end true # false would cancel later callbacks but not rollback diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index bbc54987407e..1671b1e2d55b 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1514,7 +1514,7 @@ def with_retried_nowait_lock end end rescue ActiveRecord::LockWaitTimeout => e - Gitlab::Sentry.track_acceptable_exception(e) + Gitlab::Sentry.track_exception(e) raise RebaseLockTimeout, REBASE_LOCK_MESSAGE end diff --git a/app/models/upload.rb b/app/models/upload.rb index 650321e2793b..12917f854315 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -103,10 +103,8 @@ def exist? # Help sysadmins find missing upload files if persisted? && !exist - if Gitlab::Sentry.enabled? - Raven.capture_message(_("Upload file does not exist"), extra: self.attributes) - end - + exception = RuntimeError.new("Uploaded file does not exist") + Gitlab::Sentry.track_exception(exception, self.attributes) Gitlab::Metrics.counter(:upload_file_does_not_exist_total, _('The number of times an upload record could not find its file')).increment end diff --git a/app/models/uploads/local.rb b/app/models/uploads/local.rb index 2901c33c3592..f1f25dfb5848 100644 --- a/app/models/uploads/local.rb +++ b/app/models/uploads/local.rb @@ -23,7 +23,8 @@ def delete_file(path) unless in_uploads?(path) message = "Path '#{path}' is not in uploads dir, skipping" logger.warn(message) - Gitlab::Sentry.track_exception(RuntimeError.new(message), extra: { uploads_dir: storage_dir }) + Gitlab::Sentry.track_and_raise_for_dev_exception( + RuntimeError.new(message), uploads_dir: storage_dir) return end diff --git a/app/services/ci/archive_trace_service.rb b/app/services/ci/archive_trace_service.rb index 8fad9e9c8699..75c7eee2f720 100644 --- a/app/services/ci/archive_trace_service.rb +++ b/app/services/ci/archive_trace_service.rb @@ -47,9 +47,9 @@ def archive_error(error, job, worker_name) job_id: job.id) Gitlab::Sentry - .track_exception(error, + .track_and_raise_for_dev_exception(error, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/51502', - extra: { job_id: job.id }) + job_id: job.id ) end end end diff --git a/app/services/ci/generate_exposed_artifacts_report_service.rb b/app/services/ci/generate_exposed_artifacts_report_service.rb index b9bf580bcbc4..af6331341fff 100644 --- a/app/services/ci/generate_exposed_artifacts_report_service.rb +++ b/app/services/ci/generate_exposed_artifacts_report_service.rb @@ -15,7 +15,7 @@ def execute(base_pipeline, head_pipeline) data: data } rescue => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { project_id: project.id }) + Gitlab::Sentry.track_exception(e, project_id: project.id) { status: :error, key: key(base_pipeline, head_pipeline), diff --git a/app/services/ci/prepare_build_service.rb b/app/services/ci/prepare_build_service.rb index 3722faeb020b..8ace7914f8ed 100644 --- a/app/services/ci/prepare_build_service.rb +++ b/app/services/ci/prepare_build_service.rb @@ -13,7 +13,7 @@ def execute build.enqueue! rescue => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { build_id: build.id }) + Gitlab::Sentry.track_exception(e, build_id: build.id) build.drop(:unmet_prerequisites) end diff --git a/app/services/ci/register_job_service.rb b/app/services/ci/register_job_service.rb index 30e2a66e04a4..24597579d9ef 100644 --- a/app/services/ci/register_job_service.rb +++ b/app/services/ci/register_job_service.rb @@ -128,13 +128,13 @@ def scheduler_failure!(build) end def track_exception_for_build(ex, build) - Gitlab::Sentry.track_acceptable_exception(ex, extra: { + Gitlab::Sentry.track_exception(ex, build_id: build.id, build_name: build.name, build_stage: build.stage, pipeline_id: build.pipeline_id, project_id: build.project_id - }) + ) end # rubocop: disable CodeReuse/ActiveRecord diff --git a/app/services/clusters/applications/base_helm_service.rb b/app/services/clusters/applications/base_helm_service.rb index 3e7f55f0c635..2b51de719348 100644 --- a/app/services/clusters/applications/base_helm_service.rb +++ b/app/services/clusters/applications/base_helm_service.rb @@ -21,14 +21,7 @@ def log_error(error) group_ids: app.cluster.group_ids } - logger_meta = meta.merge( - exception: error.class.name, - message: error.message, - backtrace: Gitlab::Profiler.clean_backtrace(error.backtrace) - ) - - logger.error(logger_meta) - Gitlab::Sentry.track_acceptable_exception(error, extra: meta) + Gitlab::Sentry.track_exception(error, meta) end def log_event(event) diff --git a/app/services/projects/container_repository/delete_tags_service.rb b/app/services/projects/container_repository/delete_tags_service.rb index af58d3780b08..0da3ddea9f72 100644 --- a/app/services/projects/container_repository/delete_tags_service.rb +++ b/app/services/projects/container_repository/delete_tags_service.rb @@ -51,7 +51,7 @@ def replace_tag_manifests(container_repository, dummy_manifest, tag_names) digests = deleted_tags.values.uniq # rubocop: disable CodeReuse/ActiveRecord - Gitlab::Sentry.track_exception(ArgumentError.new('multiple tag digests')) if digests.many? + Gitlab::Sentry.track_and_raise_for_dev_exception(ArgumentError.new('multiple tag digests')) if digests.many? deleted_tags end diff --git a/app/services/projects/import_service.rb b/app/services/projects/import_service.rb index 073c14040cea..bef4897baec6 100644 --- a/app/services/projects/import_service.rb +++ b/app/services/projects/import_service.rb @@ -25,13 +25,13 @@ def execute success rescue Gitlab::UrlBlocker::BlockedUrlError => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { project_path: project.full_path, importer: project.import_type }) + Gitlab::Sentry.track_exception(e, project_path: project.full_path, importer: project.import_type) error(s_("ImportProjects|Error importing repository %{project_safe_import_url} into %{project_full_path} - %{message}") % { project_safe_import_url: project.safe_import_url, project_full_path: project.full_path, message: e.message }) rescue => e message = Projects::ImportErrorFilter.filter_message(e.message) - Gitlab::Sentry.track_acceptable_exception(e, extra: { project_path: project.full_path, importer: project.import_type }) + Gitlab::Sentry.track_exception(e, project_path: project.full_path, importer: project.import_type) error(s_("ImportProjects|Error importing repository %{project_safe_import_url} into %{project_full_path} - %{message}") % { project_safe_import_url: project.safe_import_url, project_full_path: project.full_path, message: message }) end diff --git a/app/services/prometheus/proxy_variable_substitution_service.rb b/app/services/prometheus/proxy_variable_substitution_service.rb index d3d56987f075..4a6678ec2376 100644 --- a/app/services/prometheus/proxy_variable_substitution_service.rb +++ b/app/services/prometheus/proxy_variable_substitution_service.rb @@ -32,7 +32,7 @@ def substitute_ruby_variables(result) success(result) rescue TypeError, ArgumentError => exception log_error(exception.message) - Gitlab::Sentry.track_acceptable_exception(exception, extra: { + Gitlab::Sentry.track_exception(exception, extra: { template_string: query, variables: predefined_context }) diff --git a/app/workers/delete_stored_files_worker.rb b/app/workers/delete_stored_files_worker.rb index 8a693a64055a..1d52f71c866e 100644 --- a/app/workers/delete_stored_files_worker.rb +++ b/app/workers/delete_stored_files_worker.rb @@ -15,7 +15,7 @@ def perform(class_name, keys) unless klass message = "Unknown class '#{class_name}'" logger.error(message) - Gitlab::Sentry.track_exception(RuntimeError.new(message)) + Gitlab::Sentry.track_and_raise_for_dev_exception(RuntimeError.new(message)) return end diff --git a/app/workers/pages_domain_removal_cron_worker.rb b/app/workers/pages_domain_removal_cron_worker.rb index b1506831056a..ba3c89d0e70d 100644 --- a/app/workers/pages_domain_removal_cron_worker.rb +++ b/app/workers/pages_domain_removal_cron_worker.rb @@ -11,7 +11,7 @@ def perform PagesDomain.for_removal.find_each do |domain| domain.destroy! rescue => e - Raven.capture_exception(e) + Gitlab::Sentry.track_exception(e) end end end diff --git a/app/workers/run_pipeline_schedule_worker.rb b/app/workers/run_pipeline_schedule_worker.rb index 853f774875af..450dee0e83ef 100644 --- a/app/workers/run_pipeline_schedule_worker.rb +++ b/app/workers/run_pipeline_schedule_worker.rb @@ -39,9 +39,9 @@ def error(schedule, error) "schedule_id: #{schedule.id} message: #{error.message}" Gitlab::Sentry - .track_exception(error, + .track_and_raise_for_dev_exception(error, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/41231', - extra: { schedule_id: schedule.id }) + schedule_id: schedule.id) end # rubocop:enable Gitlab/RailsLogger diff --git a/app/workers/stuck_ci_jobs_worker.rb b/app/workers/stuck_ci_jobs_worker.rb index b116965d1050..99eff044eae4 100644 --- a/app/workers/stuck_ci_jobs_worker.rb +++ b/app/workers/stuck_ci_jobs_worker.rb @@ -80,12 +80,12 @@ def drop_build(type, build, status, timeout, reason) end def track_exception_for_build(ex, build) - Gitlab::Sentry.track_acceptable_exception(ex, extra: { + Gitlab::Sentry.track_exception(ex, build_id: build.id, build_name: build.name, build_stage: build.stage, pipeline_id: build.pipeline_id, project_id: build.project_id - }) + ) end end diff --git a/config/initializers/forbid_sidekiq_in_transactions.rb b/config/initializers/forbid_sidekiq_in_transactions.rb index bb190af60b56..bd59fd4ab903 100644 --- a/config/initializers/forbid_sidekiq_in_transactions.rb +++ b/config/initializers/forbid_sidekiq_in_transactions.rb @@ -29,7 +29,7 @@ module NoEnqueueingFromTransactions MSG rescue Sidekiq::Worker::EnqueueFromTransactionError => e ::Rails.logger.error(e.message) if ::Rails.env.production? - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) end end diff --git a/config/initializers/sentry.rb b/config/initializers/sentry.rb index 48daca3d2542..cebb0edf275b 100644 --- a/config/initializers/sentry.rb +++ b/config/initializers/sentry.rb @@ -2,35 +2,4 @@ require 'gitlab/current_settings' -def configure_sentry - if Gitlab::Sentry.enabled? - Raven.configure do |config| - config.dsn = Gitlab.config.sentry.dsn - config.release = Gitlab.revision - config.current_environment = Gitlab.config.sentry.environment - - # Sanitize fields based on those sanitized from Rails. - config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s) - # Sanitize authentication headers - config.sanitize_http_headers = %w[Authorization Private-Token] - config.tags = { program: Gitlab.process_name } - # Debugging for https://gitlab.com/gitlab-org/gitlab-foss/issues/57727 - config.before_send = lambda do |event, hint| - if ActiveModel::MissingAttributeError === hint[:exception] - columns_hash = ActiveRecord::Base - .connection - .schema_cache - .instance_variable_get(:@columns_hash) - .map { |k, v| [k, v.map(&:first)] } - .to_h - - event.extra.merge!(columns_hash) - end - - event - end - end - end -end - -configure_sentry if Rails.env.production? || Rails.env.development? +Gitlab::Sentry.configure diff --git a/doc/development/logging.md b/doc/development/logging.md index 22f3059d4b24..4ccb5a1a06e5 100644 --- a/doc/development/logging.md +++ b/doc/development/logging.md @@ -127,6 +127,68 @@ importer progresses. Here's what to do: logger.info(message: "Import error", error_code: 1, error: "I/O failure") ``` +## Exception Handling + +It often happens that you catch the exception and want to track it. + +It should be noted that manual logging of exceptions is not allowed, as: + +1. Manual logged exceptions can leak confidential data, +1. Manual logged exception very often require to clean backtrace + which reduces the boilerplate, +1. Very often manually logged exception needs to be tracked to Sentry as well, +1. Manually logged exceptions does not use `correlation_id`, which makes hard + to pin them to request, user and context in which this exception was raised, +1. It is very likely that manually logged exceptions will end-up across + multiple files, which increases burden scraping all logging files. + +To avoid duplicating and having consistent behavior the `Gitlab::Sentry` +provides helper methods to track exceptions: + +1. `Gitlab::Sentry.track_and_raise_exception`: this method logs, + sends exception to Sentry (if configured) and re-raises the exception, +1. `Gitlab::Sentry.track_exception`: this method only logs + and sends exception to Sentry (if configured), +1. `Gitlab::Sentry.log_exception`: this method only logs the exception, + and DOES NOT send the exception to Sentry, +1. `Gitlab::Sentry.track_and_raise_for_dev_exception`: this method logs, + sends exception to Sentry (if configured) and re-raises the exception + for development and test enviroments. + +It is advised to only use `Gitlab::Sentry.track_and_raise_exception` +and `Gitlab::Sentry.track_exception` as presented on below examples. + +Consider adding additional extra parameters to provide more context +for each tracked exception. + +### Example + +```ruby +class MyService < ::BaseService + def execute + project.perform_expensive_operation + + success + rescue => e + Gitlab::Sentry.track_exception(e, project_id: project.id) + + error('Exception occurred') + end +end +``` + +```ruby +class MyService < ::BaseService + def execute + project.perform_expensive_operation + + success + rescue => e + Gitlab::Sentry.track_and_raise_exception(e, project_id: project.id) + end +end +``` + ## Additional steps with new log files 1. Consider log retention settings. By default, Omnibus will rotate any diff --git a/ee/app/models/ee/clusters/platforms/kubernetes.rb b/ee/app/models/ee/clusters/platforms/kubernetes.rb index 2a52d93001a5..35e3eb227e65 100644 --- a/ee/app/models/ee/clusters/platforms/kubernetes.rb +++ b/ee/app/models/ee/clusters/platforms/kubernetes.rb @@ -126,7 +126,7 @@ def handle_exceptions(resource_not_found_error_message, opts, &block) status: :error }.merge(opts) rescue Kubeclient::HttpError => e - ::Gitlab::Sentry.track_acceptable_exception(e) + ::Gitlab::Sentry.track_exception(e) { error: _('Kubernetes API returned status code: %{error_code}') % { diff --git a/ee/app/models/project_services/jenkins_deprecated_service.rb b/ee/app/models/project_services/jenkins_deprecated_service.rb index 594121845d57..0ee1fede223c 100644 --- a/ee/app/models/project_services/jenkins_deprecated_service.rb +++ b/ee/app/models/project_services/jenkins_deprecated_service.rb @@ -106,7 +106,7 @@ def read_commit_status(sha, ref) begin src = Nokogiri.parse(response).css('img.build-caption-status-icon,.build-caption>img').first.attributes['src'].value rescue NoMethodError => ex - Raven.capture_exception(ex, extra: { 'response' => response }) + Gitlab::Sentry.track_exception(ex, response: response) return :error end diff --git a/ee/lib/api/conan_packages.rb b/ee/lib/api/conan_packages.rb index 6600a51727c0..1c420dc1f696 100644 --- a/ee/lib/api/conan_packages.rb +++ b/ee/lib/api/conan_packages.rb @@ -392,7 +392,7 @@ def upload_package_file(file_type) # conan sends two upload requests, the first has no file, so we skip record creation if file.size == 0 ::Packages::Conan::CreatePackageFileService.new(current_package, uploaded_file, params.merge(conan_file_type: file_type)).execute unless params['file.size'] == 0 rescue ObjectStorage::RemoteStoreError => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { file_name: params[:file_name], project_id: project.id }) + Gitlab::Sentry.track_exception(e, file_name: params[:file_name], project_id: project.id) forbidden! end diff --git a/ee/lib/ee/gitlab/ci/limit.rb b/ee/lib/ee/gitlab/ci/limit.rb index 18b3f396a5c9..91e86aa0781d 100644 --- a/ee/lib/ee/gitlab/ci/limit.rb +++ b/ee/lib/ee/gitlab/ci/limit.rb @@ -28,7 +28,7 @@ def log_error!(extra_context = {}) error = LimitExceededError.new(message) # TODO: change this to Gitlab::Sentry.log_exception(error, extra_context) # https://gitlab.com/gitlab-org/gitlab/issues/32906 - ::Gitlab::Sentry.track_acceptable_exception(error, extra: extra_context) + ::Gitlab::Sentry.track_exception(error, extra_context) end end end diff --git a/ee/lib/gitlab/ci/parsers/license_compliance/license_scanning.rb b/ee/lib/gitlab/ci/parsers/license_compliance/license_scanning.rb index 1f72221b0750..2e57577fd983 100644 --- a/ee/lib/gitlab/ci/parsers/license_compliance/license_scanning.rb +++ b/ee/lib/gitlab/ci/parsers/license_compliance/license_scanning.rb @@ -18,7 +18,7 @@ def parse!(json_data, report) rescue JSON::ParserError raise LicenseScanningParserError, 'JSON parsing failed' rescue => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) raise LicenseScanningParserError, 'License scanning report parsing failed' end end diff --git a/ee/lib/gitlab/ci/parsers/metrics/generic.rb b/ee/lib/gitlab/ci/parsers/metrics/generic.rb index 81672d0040ff..750e60842764 100644 --- a/ee/lib/gitlab/ci/parsers/metrics/generic.rb +++ b/ee/lib/gitlab/ci/parsers/metrics/generic.rb @@ -19,7 +19,7 @@ def parse_line(line, metrics_report) metrics_report.add_metric(name, metric_values.first) rescue => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) raise MetricsParserError, "Metrics parsing failed" end end diff --git a/ee/lib/gitlab/ci/parsers/security/common.rb b/ee/lib/gitlab/ci/parsers/security/common.rb index 57cf66498d36..64d3e683362d 100644 --- a/ee/lib/gitlab/ci/parsers/security/common.rb +++ b/ee/lib/gitlab/ci/parsers/security/common.rb @@ -17,7 +17,7 @@ def parse!(json_data, report) rescue JSON::ParserError raise SecurityReportParserError, 'JSON parsing failed' rescue => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) raise SecurityReportParserError, "#{report.type} security report parsing failed" end diff --git a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/activity_spec.rb b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/activity_spec.rb index 4e244db4e652..b427e22b1a89 100644 --- a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/activity_spec.rb +++ b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/activity_spec.rb @@ -54,9 +54,9 @@ end it 'logs the error' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).with( + expect(Gitlab::Sentry).to receive(:track_exception).with( instance_of(EE::Gitlab::Ci::Limit::LimitExceededError), - extra: { project_id: project.id, plan: namespace.actual_plan_name } + project_id: project.id, plan: namespace.actual_plan_name ) subject @@ -83,7 +83,7 @@ end it 'does not log any error' do - expect(Gitlab::Sentry).not_to receive(:track_acceptable_exception) + expect(Gitlab::Sentry).not_to receive(:track_exception) subject end diff --git a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/job_activity_spec.rb b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/job_activity_spec.rb index d79f8a47093f..83229cd2b974 100644 --- a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/job_activity_spec.rb +++ b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/job_activity_spec.rb @@ -56,9 +56,9 @@ end it 'logs the error' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).with( + expect(Gitlab::Sentry).to receive(:track_exception).with( instance_of(EE::Gitlab::Ci::Limit::LimitExceededError), - extra: { project_id: project.id, plan: namespace.actual_plan_name } + project_id: project.id, plan: namespace.actual_plan_name ) subject @@ -85,7 +85,7 @@ end it 'does not log any error' do - expect(Gitlab::Sentry).not_to receive(:track_acceptable_exception) + expect(Gitlab::Sentry).not_to receive(:track_exception) subject end diff --git a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/size_spec.rb b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/size_spec.rb index ca393d05eab6..8aaa5470d408 100644 --- a/ee/spec/lib/gitlab/ci/pipeline/chain/limit/size_spec.rb +++ b/ee/spec/lib/gitlab/ci/pipeline/chain/limit/size_spec.rb @@ -72,9 +72,9 @@ end it 'logs the error' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).with( + expect(Gitlab::Sentry).to receive(:track_exception).with( instance_of(EE::Gitlab::Ci::Limit::LimitExceededError), - extra: { project_id: project.id, plan: namespace.actual_plan_name } + project_id: project.id, plan: namespace.actual_plan_name ) subject @@ -124,7 +124,7 @@ end it 'does not log any error' do - expect(Gitlab::Sentry).not_to receive(:track_acceptable_exception) + expect(Gitlab::Sentry).not_to receive(:track_exception) subject end diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 49b86489a8b9..a3648ec3df3b 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -384,8 +384,9 @@ def render_api_error!(message, status) def handle_api_exception(exception) if report_exception?(exception) define_params_for_grape_middleware - Gitlab::Sentry.context(current_user) - Gitlab::Sentry.track_acceptable_exception(exception, extra: params) + Gitlab::Sentry.with_context(current_user) do + Gitlab::Sentry.track_exception(exception, params) + end end # This is used with GrapeLogging::Loggers::ExceptionLogger diff --git a/lib/gitlab/bitbucket_import/importer.rb b/lib/gitlab/bitbucket_import/importer.rb index e01ffb631ba0..5fbf7be25689 100644 --- a/lib/gitlab/bitbucket_import/importer.rb +++ b/lib/gitlab/bitbucket_import/importer.rb @@ -11,7 +11,6 @@ class Importer { title: 'task', color: '#7F8C8D' }].freeze attr_reader :project, :client, :errors, :users - attr_accessor :logger def initialize(project) @project = project @@ -20,7 +19,6 @@ def initialize(project) @labels = {} @errors = [] @users = {} - @logger = Gitlab::Import::Logger.build end def execute @@ -47,7 +45,8 @@ def store_pull_request_error(pull_request, ex) backtrace = Gitlab::Profiler.clean_backtrace(ex.backtrace) error = { type: :pull_request, iid: pull_request.iid, errors: ex.message, trace: backtrace, raw_response: pull_request.raw } - log_error(error) + Gitlab::Sentry.log_exception(ex, error) + # Omit the details from the database to avoid blowing up usage in the error column error.delete(:trace) error.delete(:raw_response) @@ -275,10 +274,6 @@ def comment_note(comment) author.to_s + comment.note.to_s end - def log_error(details) - logger.error(log_base_data.merge(details)) - end - def log_base_data { class: self.class.name, diff --git a/lib/gitlab/bitbucket_server_import/importer.rb b/lib/gitlab/bitbucket_server_import/importer.rb index af50a27c47b0..2d6e62c8131d 100644 --- a/lib/gitlab/bitbucket_server_import/importer.rb +++ b/lib/gitlab/bitbucket_server_import/importer.rb @@ -133,7 +133,10 @@ def import_repository log_info(stage: 'import_repository', message: 'finished import') rescue Gitlab::Shell::Error => e - log_error(stage: 'import_repository', message: 'failed import', error: e.message) + Gitlab::Sentry.log_exception( + e, + stage: 'import_repository', message: 'failed import', error: e.message + ) # Expire cache to prevent scenarios such as: # 1. First import failed, but the repo was imported successfully, so +exists?+ returns true @@ -164,8 +167,10 @@ def import_pull_requests batch.each do |pull_request| import_bitbucket_pull_request(pull_request) rescue StandardError => e - backtrace = Gitlab::Profiler.clean_backtrace(e.backtrace) - log_error(stage: 'import_pull_requests', iid: pull_request.iid, error: e.message, backtrace: backtrace) + Gitlab::Sentry.log_exception( + e, + stage: 'import_pull_requests', iid: pull_request.iid, error: e.message, backtrace: backtrace + ) errors << { type: :pull_request, iid: pull_request.iid, errors: e.message, backtrace: backtrace.join("\n"), raw_response: pull_request.raw } end @@ -177,7 +182,11 @@ def delete_temp_branches client.delete_branch(project_key, repository_slug, branch.name, branch.sha) project.repository.delete_branch(branch.name) rescue BitbucketServer::Connection::ConnectionError => e - log_error(stage: 'delete_temp_branches', branch: branch.name, error: e.message) + Gitlab::Sentry.log_exception( + e, + stage: 'delete_temp_branches', branch: branch.name, error: e.message + ) + @errors << { type: :delete_temp_branches, branch_name: branch.name, errors: e.message } end end @@ -288,7 +297,11 @@ def create_diff_note(merge_request, comment, position, discussion_id = nil) # a regular note. create_fallback_diff_note(merge_request, comment, position) rescue StandardError => e - log_error(stage: 'create_diff_note', comment_id: comment.id, error: e.message) + Gitlab::Sentry.log_exception( + e, + stage: 'create_diff_note', comment_id: comment.id, error: e.message + ) + errors << { type: :pull_request, id: comment.id, errors: e.message } nil end @@ -325,7 +338,11 @@ def import_standalone_pr_comments(pr_comments, merge_request) merge_request.notes.create!(pull_request_comment_attributes(replies)) end rescue StandardError => e - log_error(stage: 'import_standalone_pr_comments', merge_request_id: merge_request.id, comment_id: comment.id, error: e.message) + Gitlab::Sentry.log_exception( + e, + stage: 'import_standalone_pr_comments', merge_request_id: merge_request.id, comment_id: comment.id, error: e.message + ) + errors << { type: :pull_request, comment_id: comment.id, errors: e.message } end end @@ -360,10 +377,6 @@ def log_info(details) logger.info(log_base_data.merge(details)) end - def log_error(details) - logger.error(log_base_data.merge(details)) - end - def log_warn(details) logger.warn(log_base_data.merge(details)) end diff --git a/lib/gitlab/ci/config.rb b/lib/gitlab/ci/config.rb index 9c1e6277e95e..ef11b8dc5301 100644 --- a/lib/gitlab/ci/config.rb +++ b/lib/gitlab/ci/config.rb @@ -67,11 +67,11 @@ def expand_config(config) build_config(config) rescue Gitlab::Config::Loader::Yaml::DataTooLargeError => e - track_exception(e) + track_and_raise_for_dev_exception(e) raise Config::ConfigError, e.message rescue Gitlab::Ci::Config::External::Context::TimeoutError => e - track_exception(e) + track_and_raise_for_dev_exception(e) raise Config::ConfigError, TIMEOUT_MESSAGE end @@ -94,8 +94,8 @@ def build_context(project:, sha:, user:) user: user) end - def track_exception(error) - Gitlab::Sentry.track_exception(error, extra: @context.sentry_payload) + def track_and_raise_for_dev_exception(error) + Gitlab::Sentry.track_and_raise_for_dev_exception(error, @context.sentry_payload) end # Overriden in EE diff --git a/lib/gitlab/ci/pipeline/chain/config/process.rb b/lib/gitlab/ci/pipeline/chain/config/process.rb index 731b0fdb286c..feb5b4556e11 100644 --- a/lib/gitlab/ci/pipeline/chain/config/process.rb +++ b/lib/gitlab/ci/pipeline/chain/config/process.rb @@ -21,10 +21,10 @@ def perform! rescue Gitlab::Ci::YamlProcessor::ValidationError => ex error(ex.message, config_error: true) rescue => ex - Gitlab::Sentry.track_acceptable_exception(ex, extra: { + Gitlab::Sentry.track_exception(ex, project_id: project.id, sha: @pipeline.sha - }) + ) error("Undefined error (#{Labkit::Correlation::CorrelationId.current_id})", config_error: true) diff --git a/lib/gitlab/diff/highlight.rb b/lib/gitlab/diff/highlight.rb index ca7974930af2..676743dbd592 100644 --- a/lib/gitlab/diff/highlight.rb +++ b/lib/gitlab/diff/highlight.rb @@ -35,7 +35,7 @@ def highlight # match the blob, which is a bug. But we shouldn't fail to render # completely in that case, even though we want to report the error. rescue RangeError => e - Gitlab::Sentry.track_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/45441') + Gitlab::Sentry.track_and_raise_for_dev_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/45441') end end diff --git a/lib/gitlab/gitaly_client.rb b/lib/gitlab/gitaly_client.rb index 373539f55164..41207e8cbdea 100644 --- a/lib/gitlab/gitaly_client.rb +++ b/lib/gitlab/gitaly_client.rb @@ -67,8 +67,7 @@ def self.stub_certs File.read(cert_file).scan(PEM_REGEX).map do |cert| OpenSSL::X509::Certificate.new(cert).to_pem rescue OpenSSL::OpenSSLError => e - Rails.logger.error "Could not load certificate #{cert_file} #{e}" # rubocop:disable Gitlab/RailsLogger - Gitlab::Sentry.track_exception(e, extra: { cert_file: cert_file }) + Gitlab::Sentry.track_and_raise_for_dev_exception(e, cert_file: cert_file) nil end.compact end.uniq.join("\n") diff --git a/lib/gitlab/github_import/importer/pull_request_importer.rb b/lib/gitlab/github_import/importer/pull_request_importer.rb index dc15b95505ad..d762ea179e38 100644 --- a/lib/gitlab/github_import/importer/pull_request_importer.rb +++ b/lib/gitlab/github_import/importer/pull_request_importer.rb @@ -91,12 +91,10 @@ def create_source_branch_if_not_exists(merge_request) project.repository.add_branch(project.creator, source_branch, pull_request.source_branch_sha) rescue Gitlab::Git::CommandError => e - Gitlab::Sentry.track_acceptable_exception(e, - extra: { - source_branch: source_branch, - project_id: merge_request.project.id, - merge_request_id: merge_request.id - }) + Gitlab::Sentry.track_exception(e, + source_branch: source_branch, + project_id: merge_request.project.id, + merge_request_id: merge_request.id) end end end diff --git a/lib/gitlab/gpg.rb b/lib/gitlab/gpg.rb index abe90bba19c9..048534e04de3 100644 --- a/lib/gitlab/gpg.rb +++ b/lib/gitlab/gpg.rb @@ -110,9 +110,9 @@ def optimistic_using_tmp_keychain folder_contents = Dir.children(tmp_dir) # This means we left a GPG-agent process hanging. Logging the problem in # sentry will make this more visible. - Gitlab::Sentry.track_exception(e, + Gitlab::Sentry.track_and_raise_for_dev_exception(e, issue_url: 'https://gitlab.com/gitlab-org/gitlab/issues/20918', - extra: { tmp_dir: tmp_dir, contents: folder_contents }) + tmp_dir: tmp_dir, contents: folder_contents) end tmp_keychains_removed.increment unless File.exist?(tmp_dir) diff --git a/lib/gitlab/graphql/calls_gitaly/instrumentation.rb b/lib/gitlab/graphql/calls_gitaly/instrumentation.rb index fbd5e348c7d6..118748d5a562 100644 --- a/lib/gitlab/graphql/calls_gitaly/instrumentation.rb +++ b/lib/gitlab/graphql/calls_gitaly/instrumentation.rb @@ -32,7 +32,7 @@ def calls_gitaly_check(type_object, calls) # Will inform you if there needs to be `calls_gitaly: true` as a kwarg in the field declaration # if there is at least 1 Gitaly call involved with the field resolution. error = RuntimeError.new("Gitaly is called for field '#{type_object.name}' on #{type_object.owner.try(:name)} - please either specify a constant complexity or add `calls_gitaly: true` to the field declaration") - Gitlab::Sentry.track_exception(error) + Gitlab::Sentry.track_and_raise_for_dev_exception(error) end end end diff --git a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb index 01b55a1667f5..a6eb35be4278 100644 --- a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb +++ b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb @@ -18,7 +18,7 @@ def initial_value(query) variables: variables }) rescue => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) default_initial_values(query) end @@ -38,7 +38,7 @@ def final_value(memo) GraphqlLogger.info(memo.except!(:time_started, :query)) rescue => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) end private diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb index 5663b9f20cf0..fdb81e62bd75 100644 --- a/lib/gitlab/highlight.rb +++ b/lib/gitlab/highlight.rb @@ -61,7 +61,7 @@ def highlight_rich(text, continue: true) tokens = lexer.lex(text, continue: continue) Timeout.timeout(timeout_time) { @formatter.format(tokens, tag: tag).html_safe } rescue Timeout::Error => e - Gitlab::Sentry.track_exception(e) + Gitlab::Sentry.track_and_raise_for_dev_exception(e) highlight_plain(text) rescue highlight_plain(text) diff --git a/lib/gitlab/import_export/relation_tree_restorer.rb b/lib/gitlab/import_export/relation_tree_restorer.rb index 8a2b0cab915c..c43fc266ba36 100644 --- a/lib/gitlab/import_export/relation_tree_restorer.rb +++ b/lib/gitlab/import_export/relation_tree_restorer.rb @@ -82,11 +82,8 @@ def process_relation_item!(relation_key, relation_definition, relation_index, da end def log_import_failure(relation_key, relation_index, exception) - Gitlab::Sentry.track_acceptable_exception( - exception, - extra: { project_id: @importable.id, - relation_key: relation_key, - relation_index: relation_index }) + Gitlab::Sentry.track_exception(exception, + project_id: @project.id, relation_key: relation_key, relation_index: relation_index) ImportFailure.create( project: @importable, diff --git a/lib/gitlab/import_export/shared.rb b/lib/gitlab/import_export/shared.rb index 2539a6828c32..7bdfd928723c 100644 --- a/lib/gitlab/import_export/shared.rb +++ b/lib/gitlab/import_export/shared.rb @@ -56,11 +56,7 @@ def lock_files_path end def error(error) - error_payload = { message: error.message } - error_payload[:error_backtrace] = Gitlab::Profiler.clean_backtrace(error.backtrace) if error.backtrace - log_error(error_payload) - - Gitlab::Sentry.track_acceptable_exception(error, extra: log_base_data) + Gitlab::Sentry.track_exception(error, log_base_data) add_error_message(error.message) end diff --git a/lib/gitlab/profiler.rb b/lib/gitlab/profiler.rb index 560618bb4861..f2f6180c464e 100644 --- a/lib/gitlab/profiler.rb +++ b/lib/gitlab/profiler.rb @@ -118,6 +118,8 @@ def debug(message, *) end def self.clean_backtrace(backtrace) + return unless backtrace + Array(Rails.backtrace_cleaner.clean(backtrace)).reject do |line| line.match(Regexp.union(IGNORE_BACKTRACES)) end diff --git a/lib/gitlab/sentry.rb b/lib/gitlab/sentry.rb index 005cb3112b8c..7822eef7014a 100644 --- a/lib/gitlab/sentry.rb +++ b/lib/gitlab/sentry.rb @@ -2,76 +2,152 @@ module Gitlab module Sentry - def self.enabled? - (Rails.env.production? || Rails.env.development?) && - Gitlab.config.sentry.enabled - end + class << self + def configure + Raven.configure do |config| + config.dsn = sentry_dsn + config.release = Gitlab.revision + config.current_environment = Gitlab.config.sentry.environment + + # Sanitize fields based on those sanitized from Rails. + config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s) + # Sanitize authentication headers + config.sanitize_http_headers = %w[Authorization Private-Token] + config.tags = { program: Gitlab.process_name } + # Debugging for https://gitlab.com/gitlab-org/gitlab-foss/issues/57727 + config.before_send = method(:add_context_from_exception_type) + end + end + + def with_context(current_user = nil) + last_user_context = Raven.context.user - def self.context(current_user = nil) - return unless enabled? + user_context = { + id: current_user&.id, + email: current_user&.email, + username: current_user&.username + }.compact - Raven.tags_context(default_tags) + Raven.tags_context(default_tags) + Raven.user_context(user_context) - if current_user - Raven.user_context( - id: current_user.id, - email: current_user.email, - username: current_user.username - ) + yield + ensure + Raven.user_context(last_user_context) end - end - # 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: {}) - track_acceptable_exception(exception, issue_url: issue_url, extra: extra) - - raise exception if should_raise_for_dev? - end + # This should be used when you want to passthrough exception handling: + # rescue and raise to be catched in upper layers of the application. + # + # If the exception implements the method `sentry_extra_data` and that method + # returns a Hash, then the return value of that method will be merged into + # `extra`. Exceptions can use this mechanism to provide structured data + # to sentry in addition to their message and back-trace. + def track_and_raise_exception(exception, extra = {}) + process_exception(exception, sentry: true, extra: extra) - # 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. - # - # If the exception implements the method `sentry_extra_data` and that method - # returns a Hash, then the return value of that method will be merged into - # `extra`. Exceptions can use this mechanism to provide structured data - # to sentry in addition to their message and back-trace. - def self.track_acceptable_exception(exception, issue_url: nil, extra: {}) - if enabled? - extra = build_extra_data(exception, issue_url, extra) - context # Make sure we've set everything we know in the context - - Raven.capture_exception(exception, tags: default_tags, extra: extra) + raise exception end - end - def self.should_raise_for_dev? - Rails.env.development? || Rails.env.test? - end + # 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. + # + # If the exception implements the method `sentry_extra_data` and that method + # returns a Hash, then the return value of that method will be merged into + # `extra`. Exceptions can use this mechanism to provide structured data + # to sentry in addition to their message and back-trace. + # + # Provide an issue URL for follow up. + # as `issue_url: 'http://gitlab.com/gitlab-org/gitlab/issues/111'` + def track_and_raise_for_dev_exception(exception, extra = {}) + process_exception(exception, sentry: true, extra: extra) - def self.default_tags - { - Labkit::Correlation::CorrelationId::LOG_KEY.to_sym => Labkit::Correlation::CorrelationId.current_id, - locale: I18n.locale - } - end + raise exception if should_raise_for_dev? + end - def self.build_extra_data(exception, issue_url, extra) - exception.try(:sentry_extra_data)&.tap do |data| - extra.merge!(data) if data.is_a?(Hash) + # This should be used when you only want to track the exception. + # + # If the exception implements the method `sentry_extra_data` and that method + # returns a Hash, then the return value of that method will be merged into + # `extra`. Exceptions can use this mechanism to provide structured data + # to sentry in addition to their message and back-trace. + def track_exception(exception, extra = {}) + process_exception(exception, sentry: true, extra: extra) end - extra.merge({ issue_url: issue_url }.compact) - end + # This should be used when you only want to log the exception, + # but not send it to Sentry. + # + # If the exception implements the method `sentry_extra_data` and that method + # returns a Hash, then the return value of that method will be merged into + # `extra`. Exceptions can use this mechanism to provide structured data + # to sentry in addition to their message and back-trace. + def log_exception(exception, extra = {}) + process_exception(exception, extra: extra) + end + + private + + def process_exception(exception, sentry: false, logging: true, extra:) + exception.try(:sentry_extra_data)&.tap do |data| + extra = extra.merge(data) if data.is_a?(Hash) + end + + if sentry && Raven.configuration.server + Raven.capture_exception(exception, tags: default_tags, extra: extra) + end - private_class_method :build_extra_data + if logging + log_hash = { + exception: exception.class.name, + message: exception.message, + backtrace: Gitlab::Profiler.clean_backtrace(exception.backtrace), + tags: Raven.context.tags, + user: Raven.context.user, + extra: Raven.context.extra.merge(extra), + transaction: Raven.context.transaction + } + + Gitlab::Sentry::Logger.error(log_hash) + end + end + + def sentry_dsn + return unless Rails.env.production? || Rails.env.development? + return unless Gitlab.config.sentry.enabled + + Gitlab.config.sentry.dsn + end + + def should_raise_for_dev? + Rails.env.development? || Rails.env.test? + end + + def default_tags + { + Labkit::Correlation::CorrelationId::LOG_KEY.to_sym => Labkit::Correlation::CorrelationId.current_id, + locale: I18n.locale + } + end + + def add_context_from_exception_type(event, hint) + if ActiveModel::MissingAttributeError === hint[:exception] + columns_hash = ActiveRecord::Base + .connection + .schema_cache + .instance_variable_get(:@columns_hash) + .map { |k, v| [k, v.map(&:first)] } + .to_h + + event.extra.merge!(columns_hash) + end + + event + end + end end end diff --git a/lib/gitlab/sentry/logger.rb b/lib/gitlab/sentry/logger.rb new file mode 100644 index 000000000000..fa24b8d17d24 --- /dev/null +++ b/lib/gitlab/sentry/logger.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Gitlab + module Sentry + class Logger < ::Gitlab::JsonLogger + def self.file_name_noext + 'exceptions_json' + end + end + end +end diff --git a/lib/gitlab/shell.rb b/lib/gitlab/shell.rb index 28e5d0ba8f5f..45de77f77aaf 100644 --- a/lib/gitlab/shell.rb +++ b/lib/gitlab/shell.rb @@ -126,7 +126,7 @@ def mv_repository(storage, path, new_path) true rescue => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { path: path, new_path: new_path, storage: storage }) + Gitlab::Sentry.track_exception(e, path: path, new_path: new_path, storage: storage) false end @@ -158,7 +158,7 @@ def remove_repository(storage, name) true rescue => e Rails.logger.warn("Repository does not exist: #{e} at: #{name}.git") # rubocop:disable Gitlab/RailsLogger - Gitlab::Sentry.track_acceptable_exception(e, extra: { path: name, storage: storage }) + Gitlab::Sentry.track_exception(e, path: name, storage: storage) false end @@ -267,7 +267,7 @@ def rm_namespace(storage, name) def mv_namespace(storage, old_name, new_name) Gitlab::GitalyClient::NamespaceService.new(storage).rename(old_name, new_name) rescue GRPC::InvalidArgument => e - Gitlab::Sentry.track_acceptable_exception(e, extra: { old_name: old_name, new_name: new_name, storage: storage }) + Gitlab::Sentry.track_exception(e, old_name: old_name, new_name: new_name, storage: storage) false end diff --git a/lib/gitlab/usage_data_counters/base_counter.rb b/lib/gitlab/usage_data_counters/base_counter.rb index 2b52571c3ccb..461d562a0d42 100644 --- a/lib/gitlab/usage_data_counters/base_counter.rb +++ b/lib/gitlab/usage_data_counters/base_counter.rb @@ -8,7 +8,7 @@ class BaseCounter class << self def redis_key(event) - Gitlab::Sentry.track_exception(UnknownEvent, extra: { event: event }) unless known_events.include?(event.to_s) + Gitlab::Sentry.track_and_raise_for_dev_exception(UnknownEvent.new, event: event) unless known_events.include?(event.to_s) "USAGE_#{prefix}_#{event}".upcase end diff --git a/lib/sentry/client.rb b/lib/sentry/client.rb index 47b497accc17..851896a64298 100644 --- a/lib/sentry/client.rb +++ b/lib/sentry/client.rb @@ -62,7 +62,7 @@ def validate_size(issues) def handle_mapping_exceptions(&block) yield rescue KeyError => e - Gitlab::Sentry.track_acceptable_exception(e) + Gitlab::Sentry.track_exception(e) raise MissingKeysError, "Sentry API response is missing keys. #{e.message}" end @@ -118,7 +118,7 @@ def get_issue_latest_event(issue_id:) def handle_request_exceptions yield rescue Gitlab::HTTP::Error => e - Gitlab::Sentry.track_acceptable_exception(e) + Gitlab::Sentry.track_exception(e) raise_error 'Error when connecting to Sentry' rescue Net::OpenTimeout raise_error 'Connection to Sentry timed out' @@ -129,7 +129,7 @@ def handle_request_exceptions rescue Errno::ECONNREFUSED raise_error 'Connection refused' rescue => e - Gitlab::Sentry.track_acceptable_exception(e) + Gitlab::Sentry.track_exception(e) raise_error "Sentry request failed due to #{e.class}" end diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 9c7dcdcae634..843bca7eb7ba 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -19146,9 +19146,6 @@ msgstr "" msgid "Upload file" msgstr "" -msgid "Upload file does not exist" -msgstr "" - msgid "Upload object map" msgstr "" diff --git a/spec/controllers/projects/issues_controller_spec.rb b/spec/controllers/projects/issues_controller_spec.rb index 51d5a0d2144f..619250c21300 100644 --- a/spec/controllers/projects/issues_controller_spec.rb +++ b/spec/controllers/projects/issues_controller_spec.rb @@ -1141,7 +1141,7 @@ def post_spam end it "prevents deletion if destroy_confirm is not set" do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original delete :destroy, params: { namespace_id: project.namespace, project_id: project, id: issue.iid } @@ -1150,7 +1150,7 @@ def post_spam end it "prevents deletion in JSON format if destroy_confirm is not set" do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original delete :destroy, params: { namespace_id: project.namespace, project_id: project, id: issue.iid, format: 'json' } diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index ef5f286502ba..4b1304a91030 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -620,7 +620,7 @@ def merge_when_pipeline_succeeds end it "prevents deletion if destroy_confirm is not set" do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original delete :destroy, params: { namespace_id: project.namespace, project_id: project, id: merge_request.iid } @@ -629,7 +629,7 @@ def merge_when_pipeline_succeeds end it "prevents deletion in JSON format if destroy_confirm is not set" do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original delete :destroy, params: { namespace_id: project.namespace, project_id: project, id: merge_request.iid, format: 'json' } diff --git a/spec/lib/gitlab/ci/config_spec.rb b/spec/lib/gitlab/ci/config_spec.rb index 6594a3dfa8bc..b039c572677d 100644 --- a/spec/lib/gitlab/ci/config_spec.rb +++ b/spec/lib/gitlab/ci/config_spec.rb @@ -157,7 +157,7 @@ describe '.new' do it 'raises error' do - expect(Gitlab::Sentry).to receive(:track_exception) + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) expect { config }.to raise_error( described_class::ConfigError, @@ -367,7 +367,7 @@ end it 'raises error TimeoutError' do - expect(Gitlab::Sentry).to receive(:track_exception) + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) expect { config }.to raise_error( described_class::ConfigError, diff --git a/spec/lib/gitlab/diff/highlight_spec.rb b/spec/lib/gitlab/diff/highlight_spec.rb index f5d3d14ccc59..1a14f6d4f67f 100644 --- a/spec/lib/gitlab/diff/highlight_spec.rb +++ b/spec/lib/gitlab/diff/highlight_spec.rb @@ -105,7 +105,7 @@ end it 'keeps the original rich line' do - allow(Gitlab::Sentry).to receive(:track_exception) + allow(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) code = %q{+ raise RuntimeError, "System commands must be given as an array of strings"} @@ -114,7 +114,7 @@ end it 'reports to Sentry if configured' do - expect(Gitlab::Sentry).to receive(:track_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception).and_call_original expect { subject }. to raise_exception(RangeError) end diff --git a/spec/lib/gitlab/gitaly_client_spec.rb b/spec/lib/gitlab/gitaly_client_spec.rb index 80c1493d01b0..b7ad6cebf813 100644 --- a/spec/lib/gitlab/gitaly_client_spec.rb +++ b/spec/lib/gitlab/gitaly_client_spec.rb @@ -86,12 +86,11 @@ def stub_repos_storages(address) describe '.stub_certs' do it 'skips certificates if OpenSSLError is raised and report it' do - expect(Rails.logger).to receive(:error).at_least(:once) expect(Gitlab::Sentry) - .to receive(:track_exception) + .to receive(:track_and_raise_for_dev_exception) .with( a_kind_of(OpenSSL::X509::CertificateError), - extra: { cert_file: a_kind_of(String) }).at_least(:once) + cert_file: a_kind_of(String)).at_least(:once) expect(OpenSSL::X509::Certificate) .to receive(:new) diff --git a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb index 30d23fcf9d4c..7065b3f9bcbe 100644 --- a/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb +++ b/spec/lib/gitlab/github_import/importer/pull_request_importer_spec.rb @@ -301,7 +301,7 @@ it 'ignores Git command errors when creating a branch' do expect(project.repository).to receive(:add_branch).and_raise(Gitlab::Git::CommandError) - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original mr = insert_git_data diff --git a/spec/lib/gitlab/gpg_spec.rb b/spec/lib/gitlab/gpg_spec.rb index 5d43023502cb..c073d86b4a58 100644 --- a/spec/lib/gitlab/gpg_spec.rb +++ b/spec/lib/gitlab/gpg_spec.rb @@ -182,17 +182,17 @@ expected_tmp_dir = nil expect(described_class).to receive(:cleanup_tmp_dir).and_raise(expected_exception) - allow(Gitlab::Sentry).to receive(:track_exception) + allow(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) described_class.using_tmp_keychain do expected_tmp_dir = described_class.current_home_dir FileUtils.touch(File.join(expected_tmp_dir, 'dummy.file')) end - expect(Gitlab::Sentry).to have_received(:track_exception).with( + expect(Gitlab::Sentry).to have_received(:track_and_raise_for_dev_exception).with( expected_exception, issue_url: 'https://gitlab.com/gitlab-org/gitlab/issues/20918', - extra: { tmp_dir: expected_tmp_dir, contents: ['dummy.file'] } + tmp_dir: expected_tmp_dir, contents: ['dummy.file'] ) end diff --git a/spec/lib/gitlab/import_export/shared_spec.rb b/spec/lib/gitlab/import_export/shared_spec.rb index 8d522252e2d8..9a2339e576aa 100644 --- a/spec/lib/gitlab/import_export/shared_spec.rb +++ b/spec/lib/gitlab/import_export/shared_spec.rb @@ -49,24 +49,9 @@ it 'updates the import JID' do import_state = create(:import_state, project: project, jid: 'jid-test') - expect_next_instance_of(Gitlab::Import::Logger) do |logger| - expect(logger).to receive(:error).with(hash_including(import_jid: import_state.jid)) - end - - subject.error(error) - end - - it 'calls the error logger without a backtrace' do - expect(subject).to receive(:log_error).with(message: error.message) - - subject.error(error) - end - - it 'calls the error logger with the full message' do - backtrace = caller - allow(error).to receive(:backtrace).and_return(caller) - - expect(subject).to receive(:log_error).with(message: error.message, error_backtrace: Gitlab::Profiler.clean_backtrace(backtrace)) + expect(Gitlab::Sentry) + .to receive(:track_exception) + .with(error, hash_including(import_jid: import_state.jid)) subject.error(error) end diff --git a/spec/lib/gitlab/import_export/version_checker_spec.rb b/spec/lib/gitlab/import_export/version_checker_spec.rb index 6a626d864a92..befbd1b4c197 100644 --- a/spec/lib/gitlab/import_export/version_checker_spec.rb +++ b/spec/lib/gitlab/import_export/version_checker_spec.rb @@ -8,10 +8,20 @@ describe 'bundle a project Git repo' do let(:version) { Gitlab::ImportExport.version } + let(:version_file) { Tempfile.new('VERSION') } before do allow_any_instance_of(Gitlab::ImportExport::Shared).to receive(:relative_archive_path).and_return('') - allow(File).to receive(:open).and_return(version) + + version_file.write(version) + version_file.rewind + + allow_any_instance_of(described_class).to receive(:version_file).and_return(version_file.path) + end + + after do + version_file.close + version_file.unlink end it 'returns true if Import/Export have the same version' do diff --git a/spec/lib/gitlab/sentry_spec.rb b/spec/lib/gitlab/sentry_spec.rb index 024ac733a073..8fd403e98477 100644 --- a/spec/lib/gitlab/sentry_spec.rb +++ b/spec/lib/gitlab/sentry_spec.rb @@ -3,12 +3,11 @@ require 'spec_helper' describe Gitlab::Sentry do - describe '.context' do + describe '.with_context' do it 'adds the expected tags' do - expect(described_class).to receive(:enabled?).and_return(true) allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid') - described_class.context(nil) + described_class.with_context {} expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s) expect(Raven.tags_context[Labkit::Correlation::CorrelationId::LOG_KEY.to_sym].to_s) @@ -16,16 +15,22 @@ end end - describe '.track_exception' do + describe '.track_and_raise_for_dev_exception' do let(:exception) { RuntimeError.new('boom') } before do - allow(described_class).to receive(:enabled?).and_return(true) + stub_sentry_settings + + expect(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) + + described_class.configure end it 'raises the exception if it should' do expect(described_class).to receive(:should_raise_for_dev?).and_return(true) - expect { described_class.track_exception(exception) } + expect(Raven).to receive(:capture_exception) + + expect { described_class.track_and_raise_for_dev_exception(exception) } .to raise_error(RuntimeError) end @@ -50,22 +55,16 @@ tags: a_hash_including(expected_tags), extra: a_hash_including(expected_extras)) - described_class.track_exception( + described_class.track_and_raise_for_dev_exception( exception, issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1', - extra: { some_other_info: 'info' } + some_other_info: 'info' ) end - - it 'sets the context' do - expect(described_class).to receive(:context) - - described_class.track_exception(exception) - end end end - context '.track_acceptable_exception' do + context '.track_exception' do let(:exception) { RuntimeError.new('boom') } let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } @@ -89,34 +88,34 @@ tags: a_hash_including(expected_tags), extra: a_hash_including(expected_extras)) - described_class.track_acceptable_exception( + described_class.track_exception( exception, issue_url: issue_url, - extra: { some_other_info: 'info' } + some_other_info: 'info' ) end context 'the exception implements :sentry_extra_data' do let(:extra_info) { { event: 'explosion', size: :massive } } - let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info) } + let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info, backtrace: caller) } it 'includes the extra data from the exception in the tracking information' do expect(Raven).to receive(:capture_exception) .with(exception, a_hash_including(extra: a_hash_including(extra_info))) - described_class.track_acceptable_exception(exception) + described_class.track_exception(exception) end end context 'the exception implements :sentry_extra_data, which returns nil' do - let(:exception) { double(message: 'bang!', sentry_extra_data: nil) } + let(:exception) { double(message: 'bang!', sentry_extra_data: nil, backtrace: caller) } it 'just includes the other extra info' do extra_info = { issue_url: issue_url } expect(Raven).to receive(:capture_exception) .with(exception, a_hash_including(extra: a_hash_including(extra_info))) - described_class.track_acceptable_exception(exception, extra_info) + described_class.track_exception(exception, extra_info) end end end diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index bcab621ab6ab..dde37e62e6cf 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -3522,7 +3522,7 @@ def run_job_without_exception end it 'can drop the build' do - expect(Gitlab::Sentry).to receive(:track_exception) + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) expect { build.drop! }.not_to raise_error diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 01abaea73997..036b6c7902f9 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -1004,8 +1004,8 @@ it { is_expected.to eq(connection_status: :unknown_failure) } it 'notifies Sentry' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(instance_of(StandardError), hash_including(extra: { cluster_id: cluster.id })) + expect(Gitlab::Sentry).to receive(:track_exception) + .with(instance_of(StandardError), hash_including(cluster_id: cluster.id)) subject end diff --git a/spec/models/concerns/group_descendant_spec.rb b/spec/models/concerns/group_descendant_spec.rb index 192e884f3e88..6af57283a2a4 100644 --- a/spec/models/concerns/group_descendant_spec.rb +++ b/spec/models/concerns/group_descendant_spec.rb @@ -82,7 +82,7 @@ def all_preloaded_groups(*groups) end it 'tracks the exception when a parent was not preloaded' do - expect(Gitlab::Sentry).to receive(:track_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception).and_call_original expect { described_class.build_hierarchy([subsub_group]) }.to raise_error(ArgumentError) end @@ -91,7 +91,7 @@ def all_preloaded_groups(*groups) expected_hierarchy = { parent => { subgroup => subsub_group } } # this does not raise in production, so stubbing it here. - allow(Gitlab::Sentry).to receive(:track_exception) + allow(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) expect(described_class.build_hierarchy([subsub_group])).to eq(expected_hierarchy) end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 03434c952180..6128e7d2a244 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -171,8 +171,7 @@ it 'sends a message to Sentry' do upload = create(:upload, :issuable_upload) - expect(Gitlab::Sentry).to receive(:enabled?).and_return(true) - expect(Raven).to receive(:capture_message).with("Upload file does not exist", extra: upload.attributes) + expect(Gitlab::Sentry).to receive(:track_exception).with(instance_of(RuntimeError), upload.attributes) upload.exist? end diff --git a/spec/requests/api/graphql_spec.rb b/spec/requests/api/graphql_spec.rb index 54401ec40858..bfdfd034ccae 100644 --- a/spec/requests/api/graphql_spec.rb +++ b/spec/requests/api/graphql_spec.rb @@ -46,7 +46,7 @@ end it 'logs the exception in Sentry and continues with the request' do - expect(Gitlab::Sentry).to receive(:track_exception).at_least(1).times + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception).at_least(1).times expect(Gitlab::GraphqlLogger).to receive(:info) post_graphql(query, variables: {}) @@ -146,7 +146,7 @@ end it "logs a warning that the 'calls_gitaly' field declaration is missing" do - expect(Gitlab::Sentry).to receive(:track_exception).once + expect(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception).once post_graphql(query, current_user: user) end diff --git a/spec/requests/api/helpers_spec.rb b/spec/requests/api/helpers_spec.rb index 0c53c04ba40a..1bec860bf0e8 100644 --- a/spec/requests/api/helpers_spec.rb +++ b/spec/requests/api/helpers_spec.rb @@ -226,11 +226,11 @@ def set_param(key, value) describe '.handle_api_exception' do before do allow_any_instance_of(self.class).to receive(:rack_response) - allow(Gitlab::Sentry).to receive(:enabled?).and_return(true) stub_sentry_settings - configure_sentry + expect(Gitlab::Sentry).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) + Gitlab::Sentry.configure Raven.client.configuration.encoding = 'json' end diff --git a/spec/services/ci/archive_trace_service_spec.rb b/spec/services/ci/archive_trace_service_spec.rb index 47bc26c0521d..64fa74ccce56 100644 --- a/spec/services/ci/archive_trace_service_spec.rb +++ b/spec/services/ci/archive_trace_service_spec.rb @@ -60,10 +60,10 @@ it 'increments Prometheus counter, sends crash report to Sentry and ignore an error for continuing to archive' do expect(Gitlab::Sentry) - .to receive(:track_exception) + .to receive(:track_and_raise_for_dev_exception) .with(::Gitlab::Ci::Trace::ArchiveError, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/51502', - extra: { job_id: job.id } ).once + job_id: job.id).once expect(Sidekiq.logger).to receive(:warn).with( class: ArchiveTraceWorker.name, diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb index 4c83f9229d99..c4274f0bd17e 100644 --- a/spec/services/ci/create_pipeline_service_spec.rb +++ b/spec/services/ci/create_pipeline_service_spec.rb @@ -528,7 +528,7 @@ def previous_commit_sha_from_ref(ref) end it 'logs error' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original execute_service end @@ -613,7 +613,7 @@ def previous_commit_sha_from_ref(ref) end it 'logs error' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).and_call_original + expect(Gitlab::Sentry).to receive(:track_exception).and_call_original execute_service end diff --git a/spec/services/ci/prepare_build_service_spec.rb b/spec/services/ci/prepare_build_service_spec.rb index 2d027f13e52d..87061b3b15a6 100644 --- a/spec/services/ci/prepare_build_service_spec.rb +++ b/spec/services/ci/prepare_build_service_spec.rb @@ -51,8 +51,8 @@ it 'drops the build and notifies Sentry' do expect(build).to receive(:drop).with(:unmet_prerequisites).once - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(instance_of(Kubeclient::HttpError), hash_including(extra: { build_id: build.id })) + expect(Gitlab::Sentry).to receive(:track_exception) + .with(instance_of(Kubeclient::HttpError), hash_including(build_id: build.id)) subject end diff --git a/spec/services/ci/register_job_service_spec.rb b/spec/services/ci/register_job_service_spec.rb index 04334fb8915b..aa31d98c4fbe 100644 --- a/spec/services/ci/register_job_service_spec.rb +++ b/spec/services/ci/register_job_service_spec.rb @@ -514,8 +514,8 @@ module Ci subject { execute(specific_runner, {}) } it 'does drop the build and logs both failures' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(anything, a_hash_including(extra: a_hash_including(build_id: pending_job.id))) + expect(Gitlab::Sentry).to receive(:track_exception) + .with(anything, a_hash_including(build_id: pending_job.id)) .twice .and_call_original @@ -540,8 +540,8 @@ module Ci subject { execute(specific_runner, {}) } it 'does drop the build and logs failure' do - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(anything, a_hash_including(extra: a_hash_including(build_id: pending_job.id))) + expect(Gitlab::Sentry).to receive(:track_exception) + .with(anything, a_hash_including(build_id: pending_job.id)) .once .and_call_original diff --git a/spec/support/shared_examples/services/base_helm_service_shared_examples.rb b/spec/support/shared_examples/services/base_helm_service_shared_examples.rb index fa76b95f7688..f867cb69cfd7 100644 --- a/spec/support/shared_examples/services/base_helm_service_shared_examples.rb +++ b/spec/support/shared_examples/services/base_helm_service_shared_examples.rb @@ -11,20 +11,10 @@ } end - let(:logger_hash) do - error_hash.merge( - exception: error_name, - message: error_message, - backtrace: instance_of(Array) - ) - end - it 'logs into kubernetes.log and Sentry' do - expect(service.send(:logger)).to receive(:error).with(hash_including(logger_hash)) - - expect(Gitlab::Sentry).to receive(:track_acceptable_exception).with( + expect(Gitlab::Sentry).to receive(:track_exception).with( error, - extra: hash_including(error_hash) + hash_including(error_hash) ) service.execute diff --git a/spec/workers/ci/archive_traces_cron_worker_spec.rb b/spec/workers/ci/archive_traces_cron_worker_spec.rb index 01232e2a58b8..291fe54c4e37 100644 --- a/spec/workers/ci/archive_traces_cron_worker_spec.rb +++ b/spec/workers/ci/archive_traces_cron_worker_spec.rb @@ -63,7 +63,7 @@ let!(:build) { create(:ci_build, :success, :trace_live, finished_at: finished_at) } before do - allow(Gitlab::Sentry).to receive(:track_exception) + allow(Gitlab::Sentry).to receive(:track_and_raise_for_dev_exception) allow_any_instance_of(Gitlab::Ci::Trace).to receive(:archive!).and_raise('Unexpected error') end diff --git a/spec/workers/run_pipeline_schedule_worker_spec.rb b/spec/workers/run_pipeline_schedule_worker_spec.rb index 2bf1d470b092..1d376e1f02a1 100644 --- a/spec/workers/run_pipeline_schedule_worker_spec.rb +++ b/spec/workers/run_pipeline_schedule_worker_spec.rb @@ -43,10 +43,10 @@ allow(Ci::CreatePipelineService).to receive(:new) { raise ActiveRecord::StatementInvalid } expect(Gitlab::Sentry) - .to receive(:track_exception) + .to receive(:track_and_raise_for_dev_exception) .with(ActiveRecord::StatementInvalid, issue_url: 'https://gitlab.com/gitlab-org/gitlab-foss/issues/41231', - extra: { schedule_id: pipeline_schedule.id } ).once + schedule_id: pipeline_schedule.id).once end it 'increments Prometheus counter' do diff --git a/spec/workers/stuck_ci_jobs_worker_spec.rb b/spec/workers/stuck_ci_jobs_worker_spec.rb index 59707409b5a5..48a20f498b7c 100644 --- a/spec/workers/stuck_ci_jobs_worker_spec.rb +++ b/spec/workers/stuck_ci_jobs_worker_spec.rb @@ -30,8 +30,8 @@ it "does drop the job and logs the reason" do job.update_columns(yaml_variables: '[{"key" => "value"}]') - expect(Gitlab::Sentry).to receive(:track_acceptable_exception) - .with(anything, a_hash_including(extra: a_hash_including(build_id: job.id))) + expect(Gitlab::Sentry).to receive(:track_exception) + .with(anything, a_hash_including(build_id: job.id)) .once .and_call_original -- GitLab From 21fe0edc23013d1b6b411e59a12c04e31fb0be1c Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Tue, 10 Dec 2019 13:57:04 +0300 Subject: [PATCH 2/7] Fix spec failures --- lib/gitlab/import_export/relation_tree_restorer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/import_export/relation_tree_restorer.rb b/lib/gitlab/import_export/relation_tree_restorer.rb index c43fc266ba36..d2d0cddfcbb6 100644 --- a/lib/gitlab/import_export/relation_tree_restorer.rb +++ b/lib/gitlab/import_export/relation_tree_restorer.rb @@ -83,7 +83,7 @@ def process_relation_item!(relation_key, relation_definition, relation_index, da def log_import_failure(relation_key, relation_index, exception) Gitlab::Sentry.track_exception(exception, - project_id: @project.id, relation_key: relation_key, relation_index: relation_index) + project_id: @importable.id, relation_key: relation_key, relation_index: relation_index) ImportFailure.create( project: @importable, -- GitLab From c0d33926111d73cb83f019175a477ce38dcafcb3 Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Tue, 10 Dec 2019 17:30:03 +0300 Subject: [PATCH 3/7] Fix backtrace issue --- lib/gitlab/bitbucket_server_import/importer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/bitbucket_server_import/importer.rb b/lib/gitlab/bitbucket_server_import/importer.rb index 2d6e62c8131d..1a9ac65e641f 100644 --- a/lib/gitlab/bitbucket_server_import/importer.rb +++ b/lib/gitlab/bitbucket_server_import/importer.rb @@ -169,7 +169,7 @@ def import_pull_requests rescue StandardError => e Gitlab::Sentry.log_exception( e, - stage: 'import_pull_requests', iid: pull_request.iid, error: e.message, backtrace: backtrace + stage: 'import_pull_requests', iid: pull_request.iid, error: e.message ) errors << { type: :pull_request, iid: pull_request.iid, errors: e.message, backtrace: backtrace.join("\n"), raw_response: pull_request.raw } -- GitLab From 2ecaa9b553836dfb6784acd64c92e4958ca029c0 Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Wed, 11 Dec 2019 16:56:30 +0300 Subject: [PATCH 4/7] Use ExceptionLogFormatter to add exception details --- lib/gitlab/sentry.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/gitlab/sentry.rb b/lib/gitlab/sentry.rb index 7822eef7014a..3ae1cffc1418 100644 --- a/lib/gitlab/sentry.rb +++ b/lib/gitlab/sentry.rb @@ -103,15 +103,14 @@ def process_exception(exception, sentry: false, logging: true, extra:) if logging log_hash = { - exception: exception.class.name, - message: exception.message, - backtrace: Gitlab::Profiler.clean_backtrace(exception.backtrace), tags: Raven.context.tags, user: Raven.context.user, extra: Raven.context.extra.merge(extra), transaction: Raven.context.transaction } + Gitlab::ExceptionLogFormatter.format!(exception, log_hash) + Gitlab::Sentry::Logger.error(log_hash) end end -- GitLab From 8ea05aec203610b92a9f67e2543e5b95beb48c41 Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Thu, 12 Dec 2019 20:33:26 +0300 Subject: [PATCH 5/7] Flatten tags, user and extra for logging --- lib/gitlab/sentry.rb | 14 ++++++++------ spec/lib/gitlab/sentry_spec.rb | 35 +++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/gitlab/sentry.rb b/lib/gitlab/sentry.rb index 3ae1cffc1418..aa8f6fa12b15 100644 --- a/lib/gitlab/sentry.rb +++ b/lib/gitlab/sentry.rb @@ -102,12 +102,14 @@ def process_exception(exception, sentry: false, logging: true, extra:) end if logging - log_hash = { - tags: Raven.context.tags, - user: Raven.context.user, - extra: Raven.context.extra.merge(extra), - transaction: Raven.context.transaction - } + # TODO: this logic could migrate into `Gitlab::ExceptionLogFormatter` + # and we could also flatten deep nested hashes if required for search + # (e.g. if `extra` includes hash of hashes). + # In the current implementation, we don't flatten multi-level folded hashes. + log_hash = {} + Raven.context.tags.each { |name, value| log_hash["tags.#{name}"] = value } + Raven.context.user.each { |name, value| log_hash["user.#{name}"] = value } + Raven.context.extra.merge(extra).each { |name, value| log_hash["extra.#{name}"] = value } Gitlab::ExceptionLogFormatter.format!(exception, log_hash) diff --git a/spec/lib/gitlab/sentry_spec.rb b/spec/lib/gitlab/sentry_spec.rb index 8fd403e98477..1e7fa58f962f 100644 --- a/spec/lib/gitlab/sentry_spec.rb +++ b/spec/lib/gitlab/sentry_spec.rb @@ -17,6 +17,7 @@ describe '.track_and_raise_for_dev_exception' do let(:exception) { RuntimeError.new('boom') } + let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } before do stub_sentry_settings @@ -57,7 +58,23 @@ described_class.track_and_raise_for_dev_exception( exception, - issue_url: 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1', + issue_url: issue_url, + some_other_info: 'info' + ) + end + + it 'calls Gitlab::Sentry::Logger.error with formatted payload' do + expect(Gitlab::Sentry::Logger).to receive(:error) + .with(a_hash_including( + { 'exception.class' => 'RuntimeError' }, + { 'exception.message' => 'boom' }, + { 'tags.correlation_id' => 'cid' }, + { 'extra.some_other_info' => 'info' }, + { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + + described_class.track_exception( + exception, + issue_url: issue_url, some_other_info: 'info' ) end @@ -95,6 +112,22 @@ ) end + it 'calls Gitlab::Sentry::Logger.error with formatted payload' do + expect(Gitlab::Sentry::Logger).to receive(:error) + .with(a_hash_including( + { 'exception.class' => 'RuntimeError' }, + { 'exception.message' => 'boom' }, + { 'tags.correlation_id' => 'cid' }, + { 'extra.some_other_info' => 'info' }, + { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + + described_class.track_exception( + exception, + issue_url: issue_url, + some_other_info: 'info' + ) + end + context 'the exception implements :sentry_extra_data' do let(:extra_info) { { event: 'explosion', size: :massive } } let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info, backtrace: caller) } -- GitLab From e148f9b34c5a2600126b0323ad733ceeea903bc5 Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Thu, 12 Dec 2019 20:53:00 +0300 Subject: [PATCH 6/7] Add additional spec, fix typo --- spec/lib/gitlab/sentry_spec.rb | 42 ++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/spec/lib/gitlab/sentry_spec.rb b/spec/lib/gitlab/sentry_spec.rb index 1e7fa58f962f..d874feaf848e 100644 --- a/spec/lib/gitlab/sentry_spec.rb +++ b/spec/lib/gitlab/sentry_spec.rb @@ -72,7 +72,7 @@ { 'extra.some_other_info' => 'info' }, { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) - described_class.track_exception( + described_class.track_and_raise_for_dev_exception( exception, issue_url: issue_url, some_other_info: 'info' @@ -81,7 +81,45 @@ end end - context '.track_exception' do + describe '.track_and_raise_exception' do + let(:exception) { RuntimeError.new('boom') } + let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } + + before do + stub_sentry_settings + + expect(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) + + described_class.configure + end + + it 'always raises the exception' do + expect(Raven).to receive(:capture_exception) + + expect { described_class.track_and_raise_exception(exception) } + .to raise_error(RuntimeError) + end + + it 'calls Gitlab::Sentry::Logger.error with formatted payload' do + expect(Gitlab::Sentry::Logger).to receive(:error) + .with(a_hash_including( + { 'exception.class' => 'RuntimeError' }, + { 'exception.message' => 'boom' }, + { 'tags.correlation_id' => 'cid' }, + { 'extra.some_other_info' => 'info' }, + { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + + expect do + described_class.track_and_raise_exception( + exception, + issue_url: issue_url, + some_other_info: 'info' + ) + end.to raise_error(RuntimeError) + end + end + + describe '.track_exception' do let(:exception) { RuntimeError.new('boom') } let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } -- GitLab From 6132db3d744e6330ccdc1b018f57a16005d92cc3 Mon Sep 17 00:00:00 2001 From: Aleksei Lipniagov Date: Thu, 12 Dec 2019 21:06:01 +0300 Subject: [PATCH 7/7] Refactor specs; remove duplication --- spec/lib/gitlab/sentry_spec.rb | 93 +++++++++++++--------------------- 1 file changed, 36 insertions(+), 57 deletions(-) diff --git a/spec/lib/gitlab/sentry_spec.rb b/spec/lib/gitlab/sentry_spec.rb index d874feaf848e..6d05241e72db 100644 --- a/spec/lib/gitlab/sentry_spec.rb +++ b/spec/lib/gitlab/sentry_spec.rb @@ -3,10 +3,30 @@ require 'spec_helper' describe Gitlab::Sentry do + let(:exception) { RuntimeError.new('boom') } + let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } + + let(:expected_payload_includes) do + [ + { 'exception.class' => 'RuntimeError' }, + { 'exception.message' => 'boom' }, + { 'tags.correlation_id' => 'cid' }, + { 'extra.some_other_info' => 'info' }, + { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } + ] + end + + before do + stub_sentry_settings + + allow(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) + allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid') + + described_class.configure + end + describe '.with_context' do it 'adds the expected tags' do - allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid') - described_class.with_context {} expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s) @@ -16,29 +36,22 @@ end describe '.track_and_raise_for_dev_exception' do - let(:exception) { RuntimeError.new('boom') } - let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } - - before do - stub_sentry_settings - - expect(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) - - described_class.configure - end + context 'when exceptions for dev should be raised' do + before do + expect(described_class).to receive(:should_raise_for_dev?).and_return(true) + end - it 'raises the exception if it should' do - expect(described_class).to receive(:should_raise_for_dev?).and_return(true) - expect(Raven).to receive(:capture_exception) + it 'raises the exception' do + expect(Raven).to receive(:capture_exception) - expect { described_class.track_and_raise_for_dev_exception(exception) } - .to raise_error(RuntimeError) + expect { described_class.track_and_raise_for_dev_exception(exception) } + .to raise_error(RuntimeError) + end end - context 'when exceptions should not be raised' do + context 'when exceptions for dev should not be raised' do before do - allow(described_class).to receive(:should_raise_for_dev?).and_return(false) - allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid') + expect(described_class).to receive(:should_raise_for_dev?).and_return(false) end it 'logs the exception with all attributes passed' do @@ -65,12 +78,7 @@ it 'calls Gitlab::Sentry::Logger.error with formatted payload' do expect(Gitlab::Sentry::Logger).to receive(:error) - .with(a_hash_including( - { 'exception.class' => 'RuntimeError' }, - { 'exception.message' => 'boom' }, - { 'tags.correlation_id' => 'cid' }, - { 'extra.some_other_info' => 'info' }, - { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + .with(a_hash_including(*expected_payload_includes)) described_class.track_and_raise_for_dev_exception( exception, @@ -82,17 +90,6 @@ end describe '.track_and_raise_exception' do - let(:exception) { RuntimeError.new('boom') } - let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } - - before do - stub_sentry_settings - - expect(described_class).to receive(:sentry_dsn).and_return(Gitlab.config.sentry.dsn) - - described_class.configure - end - it 'always raises the exception' do expect(Raven).to receive(:capture_exception) @@ -102,12 +99,7 @@ it 'calls Gitlab::Sentry::Logger.error with formatted payload' do expect(Gitlab::Sentry::Logger).to receive(:error) - .with(a_hash_including( - { 'exception.class' => 'RuntimeError' }, - { 'exception.message' => 'boom' }, - { 'tags.correlation_id' => 'cid' }, - { 'extra.some_other_info' => 'info' }, - { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + .with(a_hash_including(*expected_payload_includes)) expect do described_class.track_and_raise_exception( @@ -120,14 +112,6 @@ end describe '.track_exception' do - let(:exception) { RuntimeError.new('boom') } - let(:issue_url) { 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' } - - before do - allow(described_class).to receive(:enabled?).and_return(true) - allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid') - end - it 'calls Raven.capture_exception' do expected_extras = { some_other_info: 'info', @@ -152,12 +136,7 @@ it 'calls Gitlab::Sentry::Logger.error with formatted payload' do expect(Gitlab::Sentry::Logger).to receive(:error) - .with(a_hash_including( - { 'exception.class' => 'RuntimeError' }, - { 'exception.message' => 'boom' }, - { 'tags.correlation_id' => 'cid' }, - { 'extra.some_other_info' => 'info' }, - { 'extra.issue_url' => 'http://gitlab.com/gitlab-org/gitlab-foss/issues/1' })) + .with(a_hash_including(*expected_payload_includes)) described_class.track_exception( exception, -- GitLab