Commit 9c885651 authored by charlie ablett's avatar charlie ablett

Merge branch 'put_extra_graphql_logs' into 'master'

Log `used_fields` information on GraphQL API

See merge request gitlab-org/gitlab!42820
parents 94e47aed 47d1d6f9
---
title: Include `used_fields` and `used_deprecated_fields` in GraphQL logs
merge_request: 42820
author:
type: added
...@@ -6,6 +6,8 @@ module Gitlab ...@@ -6,6 +6,8 @@ module Gitlab
class LoggerAnalyzer class LoggerAnalyzer
COMPLEXITY_ANALYZER = GraphQL::Analysis::QueryComplexity.new { |query, complexity_value| complexity_value } COMPLEXITY_ANALYZER = GraphQL::Analysis::QueryComplexity.new { |query, complexity_value| complexity_value }
DEPTH_ANALYZER = GraphQL::Analysis::QueryDepth.new { |query, depth_value| depth_value } DEPTH_ANALYZER = GraphQL::Analysis::QueryDepth.new { |query, depth_value| depth_value }
FIELD_USAGE_ANALYZER = GraphQL::Analysis::FieldUsage.new { |query, used_fields, used_deprecated_fields| [used_fields, used_deprecated_fields] }
ALL_ANALYZERS = [COMPLEXITY_ANALYZER, DEPTH_ANALYZER, FIELD_USAGE_ANALYZER].freeze
def analyze?(query) def analyze?(query)
Feature.enabled?(:graphql_logging, default_enabled: true) Feature.enabled?(:graphql_logging, default_enabled: true)
...@@ -29,12 +31,13 @@ module Gitlab ...@@ -29,12 +31,13 @@ module Gitlab
def final_value(memo) def final_value(memo)
return if memo.nil? return if memo.nil?
analyzers = [COMPLEXITY_ANALYZER, DEPTH_ANALYZER] complexity, depth, field_usages = GraphQL::Analysis.analyze_query(memo[:query], ALL_ANALYZERS)
complexity, depth = GraphQL::Analysis.analyze_query(memo[:query], analyzers)
memo[:depth] = depth memo[:depth] = depth
memo[:complexity] = complexity memo[:complexity] = complexity
memo[:duration_s] = duration(memo[:time_started]).round(1) memo[:duration_s] = duration(memo[:time_started]).round(1)
memo[:used_fields] = field_usages.first
memo[:used_deprecated_fields] = field_usages.second
GraphqlLogger.info(memo.except!(:time_started, :query)) GraphqlLogger.info(memo.except!(:time_started, :query))
rescue => e rescue => e
......
...@@ -26,7 +26,7 @@ RSpec.describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do ...@@ -26,7 +26,7 @@ RSpec.describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do
end end
it 'returns a duration in seconds' do it 'returns a duration in seconds' do
allow(GraphQL::Analysis).to receive(:analyze_query).and_return([4, 2]) allow(GraphQL::Analysis).to receive(:analyze_query).and_return([4, 2, [[], []]])
allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after) allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after)
allow(Gitlab::GraphqlLogger).to receive(:info) allow(Gitlab::GraphqlLogger).to receive(:info)
......
...@@ -190,7 +190,9 @@ RSpec.describe 'GitlabSchema configurations' do ...@@ -190,7 +190,9 @@ RSpec.describe 'GitlabSchema configurations' do
variables: {}.to_s, variables: {}.to_s,
complexity: 181, complexity: 181,
depth: 13, depth: 13,
duration_s: 7 duration_s: 7,
used_fields: an_instance_of(Array),
used_deprecated_fields: an_instance_of(Array)
} }
expect_any_instance_of(Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer).to receive(:duration).and_return(7) expect_any_instance_of(Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer).to receive(:duration).and_return(7)
......
...@@ -9,7 +9,15 @@ RSpec.describe 'GraphQL' do ...@@ -9,7 +9,15 @@ RSpec.describe 'GraphQL' do
context 'logging' do context 'logging' do
shared_examples 'logging a graphql query' do shared_examples 'logging a graphql query' do
let(:expected_params) do let(:expected_params) do
{ query_string: query, variables: variables.to_s, duration_s: anything, depth: 1, complexity: 1 } {
query_string: query,
variables: variables.to_s,
duration_s: anything,
depth: 1,
complexity: 1,
used_fields: ['Query.echo'],
used_deprecated_fields: []
}
end end
it 'logs a query with the expected params' do it 'logs a query with the expected params' do
......
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