summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcharlieablett <cablett@gitlab.com>2019-04-30 20:30:15 +1200
committercharlieablett <cablett@gitlab.com>2019-05-30 18:27:28 +1200
commit1f37aed1c917260eefda63a18d3a9af91c4a1abb (patch)
treea6912284b0984fb50a487a2e23ce6fbc0c3905d6
parente33cab9f20971aa1b8076a4fef42e5022abe115f (diff)
downloadgitlab-ce-1f37aed1c917260eefda63a18d3a9af91c4a1abb.tar.gz
New logfile for graphql queries
Specify dedicated logfile and logger class for GraphQL queries. Move complexity analyzer to a dedicated class.
-rw-r--r--lib/gitlab/graphql/query_analyzers/log_query_complexity.rb2
-rw-r--r--lib/gitlab/graphql_logger.rb19
-rw-r--r--spec/lib/gitlab/graphql_logger_spec.rb21
-rw-r--r--spec/requests/api/graphql/gitlab_schema_spec.rb10
4 files changed, 51 insertions, 1 deletions
diff --git a/lib/gitlab/graphql/query_analyzers/log_query_complexity.rb b/lib/gitlab/graphql/query_analyzers/log_query_complexity.rb
index 95db130dbfc..d3e21946357 100644
--- a/lib/gitlab/graphql/query_analyzers/log_query_complexity.rb
+++ b/lib/gitlab/graphql/query_analyzers/log_query_complexity.rb
@@ -8,7 +8,7 @@ module Gitlab
def analyzer
GraphQL::Analysis::QueryComplexity.new do |query, complexity|
# temporary until https://gitlab.com/gitlab-org/gitlab-ce/issues/59587
- Rails.logger.info("[GraphQL Query Complexity] #{complexity} | admin? #{query.context[:current_user]&.admin?}")
+ GraphqlLogger.info("[Query Complexity] #{complexity} | admin? #{query.context[:current_user]&.admin?}")
end
end
end
diff --git a/lib/gitlab/graphql_logger.rb b/lib/gitlab/graphql_logger.rb
new file mode 100644
index 00000000000..aff3ff5f48d
--- /dev/null
+++ b/lib/gitlab/graphql_logger.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Gitlab
+ class GraphqlLogger < Gitlab::Logger
+ def self.file_name_noext
+ 'graphql_json'
+ end
+
+ # duration
+ # complexity
+ # depth
+ # sanitized variables (?)
+ # a structured representation of the query (?)
+
+ def format_message(severity, timestamp, progname, msg)
+ "#{timestamp.to_s(:long)}: #{msg}\n"
+ end
+ end
+end
diff --git a/spec/lib/gitlab/graphql_logger_spec.rb b/spec/lib/gitlab/graphql_logger_spec.rb
new file mode 100644
index 00000000000..51c77181927
--- /dev/null
+++ b/spec/lib/gitlab/graphql_logger_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Gitlab::GraphqlLogger, :request_store do
+ subject { described_class.new('/dev/null') }
+ let(:now) { Time.now }
+
+ it 'builds a logger once' do
+ expect(::Logger).to receive(:new).and_call_original
+
+ subject.info('hello world')
+ subject.error('hello again')
+ end
+
+ describe '#format_message' do
+ it 'formats properly' do
+ output = subject.format_message('INFO', now, 'test', 'Hello world')
+
+ expect(output).to match(/Hello world/)
+ end
+ end
+end
diff --git a/spec/requests/api/graphql/gitlab_schema_spec.rb b/spec/requests/api/graphql/gitlab_schema_spec.rb
index a724c5c3f1c..0eb026f1ed9 100644
--- a/spec/requests/api/graphql/gitlab_schema_spec.rb
+++ b/spec/requests/api/graphql/gitlab_schema_spec.rb
@@ -83,6 +83,16 @@ describe 'GitlabSchema configurations' do
end
end
+ context 'logging' do
+ it 'writes to the GraphQL log' do
+ expect(Gitlab::GraphqlLogger).to receive(:info).with(/Query Complexity/)
+
+ query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql'))
+
+ post_graphql(query, current_user: nil)
+ end
+ end
+
context 'when IntrospectionQuery' do
it 'is not too complex' do
query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql'))