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
6e0b367c
Commit
6e0b367c
authored
Feb 08, 2021
by
Quang-Minh Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Write tests for ApplicationContextProcessor
Issue
https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/846
parent
97a94ae7
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
98 deletions
+141
-98
lib/gitlab/application_context.rb
lib/gitlab/application_context.rb
+0
-8
spec/lib/gitlab/error_tracking/processor/application_context_processor_spec.rb
..._tracking/processor/application_context_processor_spec.rb
+141
-0
spec/lib/gitlab/error_tracking_spec.rb
spec/lib/gitlab/error_tracking_spec.rb
+0
-90
No files found.
lib/gitlab/application_context.rb
View file @
6e0b367c
...
...
@@ -82,14 +82,6 @@ module Gitlab
user
&
.
username
end
def
user_id
user
&
.
id
end
def
user_email
user
&
.
email
end
def
root_namespace_path
if
namespace
namespace
.
full_path_components
.
first
...
...
spec/lib/gitlab/error_tracking/processor/application_context_processor_spec.rb
0 → 100644
View file @
6e0b367c
# frozen_string_literal: true
require
'spec_helper'
require
'rspec-parameterized'
RSpec
.
describe
Gitlab
::
ErrorTracking
::
Processor
::
ApplicationContextProcessor
do
subject
(
:processor
)
{
described_class
.
new
}
before
do
allow
(
Labkit
::
Correlation
::
CorrelationId
).
to
receive
(
:current_id
).
and_return
(
'cid'
)
allow
(
I18n
).
to
receive
(
:locale
).
and_return
(
'en'
)
end
context
'user metadata'
do
let
(
:user
)
{
create
(
:user
)
}
context
'when event user metadata was not set'
do
it
'appends username to the event metadata'
do
event
=
{}
Gitlab
::
ApplicationContext
.
with_context
(
user:
user
)
do
processor
.
process
(
event
)
end
expect
(
event
[
:user
]).
to
eql
(
username:
user
.
username
)
end
end
context
'when event user metadata was already set'
do
it
'appends username to the event metadata'
do
event
=
{
user:
{
ip_address:
'127.0.0.1'
}
}
Gitlab
::
ApplicationContext
.
with_context
(
user:
user
)
do
processor
.
process
(
event
)
end
expect
(
event
[
:user
]).
to
eql
(
ip_address:
'127.0.0.1'
,
username:
user
.
username
)
end
end
end
context
'tags metadata'
do
context
'when the GITLAB_SENTRY_EXTRA_TAGS env is not set'
do
before
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
nil
)
end
it
'does not log into AppLogger'
do
expect
(
Gitlab
::
AppLogger
).
not_to
receive
(
:debug
)
end
it
'does not send any extra tags'
do
event
=
{}
Gitlab
::
ApplicationContext
.
with_context
(
feature_category:
'feature_a'
)
do
processor
.
process
(
event
)
end
expect
(
event
[
:tags
]).
to
eql
(
correlation_id:
'cid'
,
locale:
'en'
,
program:
'test'
,
feature_category:
'feature_a'
)
end
end
context
'when the GITLAB_SENTRY_EXTRA_TAGS env is a JSON hash'
do
it
'includes those tags in all events'
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
{
foo:
'bar'
,
baz:
'quux'
}.
to_json
)
event
=
{}
Gitlab
::
ApplicationContext
.
with_context
(
feature_category:
'feature_a'
)
do
processor
.
process
(
event
)
end
expect
(
event
[
:tags
]).
to
eql
(
correlation_id:
'cid'
,
locale:
'en'
,
program:
'test'
,
feature_category:
'feature_a'
,
'foo'
=>
'bar'
,
'baz'
=>
'quux'
)
end
it
'does not log into AppLogger'
do
expect
(
Gitlab
::
AppLogger
).
not_to
receive
(
:debug
)
end
end
context
'when the GITLAB_SENTRY_EXTRA_TAGS env is not a JSON hash'
do
using
RSpec
::
Parameterized
::
TableSyntax
where
(
:env_var
,
:error
)
do
{
foo:
'bar'
,
baz:
'quux'
}.
inspect
|
'JSON::ParserError'
[].
to_json
|
'NoMethodError'
[
%w[foo bar]
].
to_json
|
'NoMethodError'
%w[foo bar]
.
to_json
|
'NoMethodError'
'"string"'
|
'NoMethodError'
end
with_them
do
before
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
env_var
)
end
it
'logs into AppLogger'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:debug
).
with
(
a_string_matching
(
error
))
processor
.
process
({})
end
it
'does not include any extra tags'
do
event
=
{}
Gitlab
::
ApplicationContext
.
with_context
(
feature_category:
'feature_a'
)
do
processor
.
process
(
event
)
end
expect
(
event
[
:tags
]).
to
eql
(
correlation_id:
'cid'
,
locale:
'en'
,
program:
'test'
,
feature_category:
'feature_a'
)
end
end
end
end
end
spec/lib/gitlab/error_tracking_spec.rb
View file @
6e0b367c
...
...
@@ -31,96 +31,6 @@ RSpec.describe Gitlab::ErrorTracking do
end
end
describe
'.configure'
do
context
'default tags from GITLAB_SENTRY_EXTRA_TAGS'
do
context
'when the value is a JSON hash'
do
it
'includes those tags in all events'
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
{
foo:
'bar'
,
baz:
'quux'
}.
to_json
)
described_class
.
configure
do
|
config
|
config
.
encoding
=
'json'
end
described_class
.
track_exception
(
StandardError
.
new
)
expect
(
sentry_event
[
'tags'
].
except
(
'correlation_id'
,
'locale'
,
'program'
))
.
to
eq
(
'foo'
=>
'bar'
,
'baz'
=>
'quux'
)
end
end
context
'when the value is not set'
do
before
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
nil
)
end
it
'does not log an error'
do
expect
(
Gitlab
::
AppLogger
).
not_to
receive
(
:debug
)
described_class
.
configure
do
|
config
|
config
.
encoding
=
'json'
end
end
it
'does not send any extra tags'
do
described_class
.
configure
do
|
config
|
config
.
encoding
=
'json'
end
described_class
.
track_exception
(
StandardError
.
new
)
expect
(
sentry_event
[
'tags'
].
keys
).
to
contain_exactly
(
'correlation_id'
,
'locale'
,
'program'
)
end
end
context
'when the value is not a JSON hash'
do
using
RSpec
::
Parameterized
::
TableSyntax
where
(
:env_var
,
:error
)
do
{
foo:
'bar'
,
baz:
'quux'
}.
inspect
|
'JSON::ParserError'
[].
to_json
|
'NoMethodError'
[
%w[foo bar]
].
to_json
|
'NoMethodError'
%w[foo bar]
.
to_json
|
'NoMethodError'
'"string"'
|
'NoMethodError'
end
with_them
do
before
do
stub_env
(
'GITLAB_SENTRY_EXTRA_TAGS'
,
env_var
)
end
it
'does not include any extra tags'
do
described_class
.
configure
do
|
config
|
config
.
encoding
=
'json'
end
described_class
.
track_exception
(
StandardError
.
new
)
expect
(
sentry_event
[
'tags'
].
except
(
'correlation_id'
,
'locale'
,
'program'
))
.
to
be_empty
end
it
'logs the error class'
do
expect
(
Gitlab
::
AppLogger
).
to
receive
(
:debug
).
with
(
a_string_matching
(
error
))
described_class
.
configure
do
|
config
|
config
.
encoding
=
'json'
end
end
end
end
end
end
describe
'.with_context'
do
it
'adds the expected tags'
do
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
)
.
to
eq
(
'cid'
)
end
end
describe
'.track_and_raise_for_dev_exception'
do
context
'when exceptions for dev should be raised'
do
before
do
...
...
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