New logfile for graphql queries

Specify dedicated logfile and logger class for GraphQL queries. Move
complexity analyzer to a dedicated class.
This commit is contained in:
charlieablett 2019-04-30 20:30:15 +12:00
parent e33cab9f20
commit 1f37aed1c9
4 changed files with 51 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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'))