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
70819379
Commit
70819379
authored
Oct 20, 2021
by
Paul Slaughter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ApplicationContextTracer to Graphql Schema
- This helps set up the logging and metrics tracer
parent
3bbc4ef3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
1 deletion
+85
-1
app/graphql/gitlab_schema.rb
app/graphql/gitlab_schema.rb
+1
-0
lib/gitlab/graphql/tracers/application_context_tracer.rb
lib/gitlab/graphql/tracers/application_context_tracer.rb
+40
-0
spec/lib/gitlab/graphql/tracers/application_context_tracer_spec.rb
...gitlab/graphql/tracers/application_context_tracer_spec.rb
+43
-0
spec/requests/api/graphql_spec.rb
spec/requests/api/graphql_spec.rb
+1
-1
No files found.
app/graphql/gitlab_schema.rb
View file @
70819379
...
...
@@ -11,6 +11,7 @@ class GitlabSchema < GraphQL::Schema
AUTHENTICATED_MAX_DEPTH
=
20
# Tracers (order is important)
use
Gitlab
::
Graphql
::
Tracers
::
ApplicationContextTracer
use
Gitlab
::
Graphql
::
Tracers
::
LoggerTracer
use
Gitlab
::
Graphql
::
GenericTracing
# Old tracer which will be removed eventually
use
Gitlab
::
Graphql
::
Tracers
::
TimerTracer
...
...
lib/gitlab/graphql/tracers/application_context_tracer.rb
0 → 100644
View file @
70819379
# frozen_string_literal: true
module
Gitlab
module
Graphql
module
Tracers
# This graphql-ruby tracer sets up `ApplicationContext` for certain operations.
class
ApplicationContextTracer
def
self
.
use
(
schema
)
schema
.
tracer
(
self
.
new
)
end
# See docs on expected interface for trace
# https://graphql-ruby.org/api-doc/1.12.17/GraphQL/Tracing
def
trace
(
key
,
data
)
case
key
when
"execute_query"
operation
=
known_operation
(
data
)
::
Gitlab
::
ApplicationContext
.
with_context
(
caller_id:
operation
.
to_caller_id
)
do
yield
end
else
yield
end
end
private
def
known_operation
(
data
)
# The library guarantees that we should have :query for execute_query, but we're being defensive here
query
=
data
.
fetch
(
:query
,
nil
)
return
::
Gitlab
::
Graphql
::
KnownOperations
.
UNKNOWN
unless
query
::
Gitlab
::
Graphql
::
KnownOperations
.
default
.
from_query
(
query
)
end
end
end
end
end
spec/lib/gitlab/graphql/tracers/application_context_tracer_spec.rb
0 → 100644
View file @
70819379
# frozen_string_literal: true
require
"fast_spec_helper"
require
"support/graphql/fake_tracer"
require
"support/graphql/fake_query_type"
RSpec
.
describe
Gitlab
::
Graphql
::
Tracers
::
ApplicationContextTracer
do
let
(
:tracer_spy
)
{
spy
(
'tracer_spy'
)
}
let
(
:default_known_operations
)
{
::
Gitlab
::
Graphql
::
KnownOperations
.
new
([
'fooOperation'
])
}
let
(
:dummy_schema
)
do
schema
=
Class
.
new
(
GraphQL
::
Schema
)
do
use
Gitlab
::
Graphql
::
Tracers
::
ApplicationContextTracer
query
Graphql
::
FakeQueryType
end
fake_tracer
=
Graphql
::
FakeTracer
.
new
(
lambda
do
|
key
,
*
args
|
tracer_spy
.
trace
(
key
,
Gitlab
::
ApplicationContext
.
current
)
end
)
schema
.
tracer
(
fake_tracer
)
schema
end
before
do
allow
(
::
Gitlab
::
Graphql
::
KnownOperations
).
to
receive
(
:default
).
and_return
(
default_known_operations
)
end
it
"sets application context during execute_query and cleans up afterwards"
,
:aggregate_failures
do
dummy_schema
.
execute
(
"query fooOperation { helloWorld }"
)
# "parse" is just an arbitrary trace event that isn't setting caller_id
expect
(
tracer_spy
).
to
have_received
(
:trace
).
with
(
"parse"
,
hash_excluding
(
"meta.caller_id"
))
expect
(
tracer_spy
).
to
have_received
(
:trace
).
with
(
"execute_query"
,
hash_including
(
"meta.caller_id"
=>
"graphql:fooOperation"
)).
once
expect
(
Gitlab
::
ApplicationContext
.
current
).
not_to
include
(
"meta.caller_id"
)
end
it
"sets caller_id when operation is not known"
do
dummy_schema
.
execute
(
"query fuzz { helloWorld }"
)
expect
(
tracer_spy
).
to
have_received
(
:trace
).
with
(
"execute_query"
,
hash_including
(
"meta.caller_id"
=>
"graphql:unknown"
)).
once
end
end
spec/requests/api/graphql_spec.rb
View file @
70819379
...
...
@@ -15,7 +15,7 @@ RSpec.describe 'GraphQL' do
let
(
:expected_execute_query_log
)
do
{
"correlation_id"
=>
kind_of
(
String
),
"meta.caller_id"
=>
"
GraphqlController#execute
"
,
"meta.caller_id"
=>
"
graphql:anonymous
"
,
"meta.client_id"
=>
kind_of
(
String
),
"meta.feature_category"
=>
"not_owned"
,
"meta.remote_ip"
=>
kind_of
(
String
),
...
...
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