From 1f37aed1c917260eefda63a18d3a9af91c4a1abb Mon Sep 17 00:00:00 2001 From: charlieablett Date: Tue, 30 Apr 2019 20:30:15 +1200 Subject: New logfile for graphql queries Specify dedicated logfile and logger class for GraphQL queries. Move complexity analyzer to a dedicated class. --- .../graphql/query_analyzers/log_query_complexity.rb | 2 +- lib/gitlab/graphql_logger.rb | 19 +++++++++++++++++++ spec/lib/gitlab/graphql_logger_spec.rb | 21 +++++++++++++++++++++ spec/requests/api/graphql/gitlab_schema_spec.rb | 10 ++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 lib/gitlab/graphql_logger.rb create mode 100644 spec/lib/gitlab/graphql_logger_spec.rb 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')) -- cgit v1.2.1