Commit 1f37aed1 authored by charlieablett's avatar charlieablett

New logfile for graphql queries

Specify dedicated logfile and logger class for GraphQL queries. Move
complexity analyzer to a dedicated class.
parent e33cab9f
...@@ -8,7 +8,7 @@ module Gitlab ...@@ -8,7 +8,7 @@ module Gitlab
def analyzer def analyzer
GraphQL::Analysis::QueryComplexity.new do |query, complexity| GraphQL::Analysis::QueryComplexity.new do |query, complexity|
# temporary until https://gitlab.com/gitlab-org/gitlab-ce/issues/59587 # 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 end
end end
......
# 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
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
...@@ -83,6 +83,16 @@ describe 'GitlabSchema configurations' do ...@@ -83,6 +83,16 @@ describe 'GitlabSchema configurations' do
end end
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 context 'when IntrospectionQuery' do
it 'is not too complex' do it 'is not too complex' do
query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql')) query = File.read(Rails.root.join('spec/fixtures/api/graphql/introspection.graphql'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment