Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
308d8524
Commit
308d8524
authored
Nov 05, 2019
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Include exception and backtrace in API logs
Closes
https://gitlab.com/gitlab-org/gitlab/issues/35362
parent
b633727c
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
0 deletions
+94
-0
changelogs/unreleased/sh-add-api-exception-to-logs.yml
changelogs/unreleased/sh-add-api-exception-to-logs.yml
+5
-0
lib/api/api.rb
lib/api/api.rb
+1
-0
lib/api/helpers.rb
lib/api/helpers.rb
+4
-0
lib/gitlab/grape_logging/loggers/exception_logger.rb
lib/gitlab/grape_logging/loggers/exception_logger.rb
+33
-0
spec/lib/gitlab/grape_logging/loggers/exception_logger_spec.rb
...lib/gitlab/grape_logging/loggers/exception_logger_spec.rb
+51
-0
No files found.
changelogs/unreleased/sh-add-api-exception-to-logs.yml
0 → 100644
View file @
308d8524
---
title
:
Include exception and backtrace in API logs
merge_request
:
19671
author
:
type
:
other
lib/api/api.rb
View file @
308d8524
...
@@ -21,6 +21,7 @@ module API
...
@@ -21,6 +21,7 @@ module API
Gitlab
::
GrapeLogging
::
Loggers
::
ClientEnvLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
ClientEnvLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
RouteLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
RouteLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
UserLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
UserLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
ExceptionLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
QueueDurationLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
QueueDurationLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
PerfLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
PerfLogger
.
new
,
Gitlab
::
GrapeLogging
::
Loggers
::
CorrelationIdLogger
.
new
Gitlab
::
GrapeLogging
::
Loggers
::
CorrelationIdLogger
.
new
...
...
lib/api/helpers.rb
View file @
308d8524
...
@@ -9,6 +9,7 @@ module API
...
@@ -9,6 +9,7 @@ module API
GITLAB_SHARED_SECRET_HEADER
=
"Gitlab-Shared-Secret"
GITLAB_SHARED_SECRET_HEADER
=
"Gitlab-Shared-Secret"
SUDO_PARAM
=
:sudo
SUDO_PARAM
=
:sudo
API_USER_ENV
=
'gitlab.api.user'
API_USER_ENV
=
'gitlab.api.user'
API_EXCEPTION_ENV
=
'gitlab.api.exception'
def
declared_params
(
options
=
{})
def
declared_params
(
options
=
{})
options
=
{
include_parent_namespaces:
false
}.
merge
(
options
)
options
=
{
include_parent_namespaces:
false
}.
merge
(
options
)
...
@@ -387,6 +388,9 @@ module API
...
@@ -387,6 +388,9 @@ module API
Gitlab
::
Sentry
.
track_acceptable_exception
(
exception
,
extra:
params
)
Gitlab
::
Sentry
.
track_acceptable_exception
(
exception
,
extra:
params
)
end
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
# lifted from https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb#L60
trace
=
exception
.
backtrace
trace
=
exception
.
backtrace
...
...
lib/gitlab/grape_logging/loggers/exception_logger.rb
0 → 100644
View file @
308d8524
# 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
spec/lib/gitlab/grape_logging/loggers/exception_logger_spec.rb
0 → 100644
View file @
308d8524
require
'spec_helper'
describe
Gitlab
::
GrapeLogging
::
Loggers
::
ExceptionLogger
do
subject
{
described_class
.
new
}
let
(
:mock_request
)
{
OpenStruct
.
new
(
env:
{})
}
describe
".parameters"
do
describe
'when no exception is available'
do
it
'returns an empty hash'
do
expect
(
subject
.
parameters
(
mock_request
,
nil
)).
to
eq
({})
end
end
describe
'when an exception is available'
do
let
(
:exception
)
{
RuntimeError
.
new
(
'This is a test'
)
}
let
(
:mock_request
)
do
OpenStruct
.
new
(
env:
{
::
API
::
Helpers
::
API_EXCEPTION_ENV
=>
exception
}
)
end
let
(
:expected
)
do
{
exception:
{
class:
'RuntimeError'
,
message:
'This is a test'
}
}
end
it
'returns the correct fields'
do
expect
(
subject
.
parameters
(
mock_request
,
nil
)).
to
eq
(
expected
)
end
context
'with backtrace'
do
before
do
current_backtrace
=
caller
allow
(
exception
).
to
receive
(
:backtrace
).
and_return
(
current_backtrace
)
expected
[
:exception
][
:backtrace
]
=
Gitlab
::
Profiler
.
clean_backtrace
(
current_backtrace
)
end
it
'includes the backtrace'
do
expect
(
subject
.
parameters
(
mock_request
,
nil
)).
to
eq
(
expected
)
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment