diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-11 15:06:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-11-11 15:06:42 +0000 |
commit | 7071f9bf3e131a7a668922ee450da529f1866b6f (patch) | |
tree | 590371b44d47d428f2826b600d7fab4f10520051 /lib | |
parent | 16bd8409bcb61d2331227d1df539c40683b6bda3 (diff) | |
download | gitlab-ce-7071f9bf3e131a7a668922ee450da529f1866b6f.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/helpers.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/grape_logging/loggers/exception_logger.rb | 33 |
3 files changed, 38 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index d71f0c38ce6..0062759d993 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -21,6 +21,7 @@ module API Gitlab::GrapeLogging::Loggers::ClientEnvLogger.new, Gitlab::GrapeLogging::Loggers::RouteLogger.new, Gitlab::GrapeLogging::Loggers::UserLogger.new, + Gitlab::GrapeLogging::Loggers::ExceptionLogger.new, Gitlab::GrapeLogging::Loggers::QueueDurationLogger.new, Gitlab::GrapeLogging::Loggers::PerfLogger.new, Gitlab::GrapeLogging::Loggers::CorrelationIdLogger.new diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 19c29847ce3..e740134f85f 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -9,6 +9,7 @@ module API GITLAB_SHARED_SECRET_HEADER = "Gitlab-Shared-Secret" SUDO_PARAM = :sudo API_USER_ENV = 'gitlab.api.user' + API_EXCEPTION_ENV = 'gitlab.api.exception' def declared_params(options = {}) options = { include_parent_namespaces: false }.merge(options) @@ -387,6 +388,9 @@ module API Gitlab::Sentry.track_acceptable_exception(exception, extra: params) end + # This is used with GrapeLogging::Loggers::ExceptionLogger + env[API_EXCEPTION_ENV] = exception + # lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60 trace = exception.backtrace diff --git a/lib/gitlab/grape_logging/loggers/exception_logger.rb b/lib/gitlab/grape_logging/loggers/exception_logger.rb new file mode 100644 index 00000000000..022eb15d28d --- /dev/null +++ b/lib/gitlab/grape_logging/loggers/exception_logger.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Gitlab + module GrapeLogging + module Loggers + class ExceptionLogger < ::GrapeLogging::Loggers::Base + def parameters(request, _) + # grape-logging attempts to pass the logger the exception + # (https://github.com/aserafin/grape_logging/blob/v1.7.0/lib/grape_logging/middleware/request_logger.rb#L63), + # but it appears that the rescue_all in api.rb takes + # precedence so the logger never sees it. We need to + # store and retrieve the exception from the environment. + exception = request.env[::API::Helpers::API_EXCEPTION_ENV] + + return {} unless exception.is_a?(Exception) + + data = { + exception: { + class: exception.class.to_s, + message: exception.message + } + } + + if exception.backtrace + data[:exception][:backtrace] = Gitlab::Profiler.clean_backtrace(exception.backtrace) + end + + data + end + end + end + end +end |