2019-09-04 21:01:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module QueryAnalyzers
|
|
|
|
class LoggerAnalyzer
|
|
|
|
COMPLEXITY_ANALYZER = GraphQL::Analysis::QueryComplexity.new { |query, complexity_value| complexity_value }
|
|
|
|
DEPTH_ANALYZER = GraphQL::Analysis::QueryDepth.new { |query, depth_value| depth_value }
|
2021-01-03 14:25:43 +05:30
|
|
|
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
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
def initial_value(query)
|
|
|
|
variables = process_variables(query.provided_variables)
|
|
|
|
default_initial_values(query).merge({
|
2021-04-29 21:17:54 +05:30
|
|
|
operation_name: query.operation_name,
|
2019-09-04 21:01:54 +05:30
|
|
|
query_string: query.query_string,
|
|
|
|
variables: variables
|
|
|
|
})
|
|
|
|
rescue => e
|
2020-01-01 13:55:28 +05:30
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2019-09-04 21:01:54 +05:30
|
|
|
default_initial_values(query)
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def call(memo, *)
|
|
|
|
memo
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def final_value(memo)
|
|
|
|
return if memo.nil?
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
complexity, depth, field_usages = GraphQL::Analysis.analyze_query(memo[:query], ALL_ANALYZERS)
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
memo[:depth] = depth
|
|
|
|
memo[:complexity] = complexity
|
2021-01-03 14:25:43 +05:30
|
|
|
# This duration is not the execution time of the
|
|
|
|
# query but the execution time of the analyzer.
|
2020-05-24 23:13:21 +05:30
|
|
|
memo[:duration_s] = duration(memo[:time_started]).round(1)
|
2021-01-03 14:25:43 +05:30
|
|
|
memo[:used_fields] = field_usages.first
|
|
|
|
memo[:used_deprecated_fields] = field_usages.second
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
RequestStore.store[:graphql_logs] ||= []
|
|
|
|
RequestStore.store[:graphql_logs] << memo
|
2019-09-04 21:01:54 +05:30
|
|
|
GraphqlLogger.info(memo.except!(:time_started, :query))
|
|
|
|
rescue => e
|
2020-01-01 13:55:28 +05:30
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_variables(variables)
|
2021-02-04 15:43:07 +05:30
|
|
|
filtered_variables = filter_sensitive_variables(variables)
|
|
|
|
|
|
|
|
if filtered_variables.respond_to?(:to_s)
|
|
|
|
filtered_variables.to_s
|
2019-09-04 21:01:54 +05:30
|
|
|
else
|
2021-02-04 15:43:07 +05:30
|
|
|
filtered_variables
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-04 15:43:07 +05:30
|
|
|
def filter_sensitive_variables(variables)
|
|
|
|
ActiveSupport::ParameterFilter
|
|
|
|
.new(::Rails.application.config.filter_parameters)
|
|
|
|
.filter(variables)
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def duration(time_started)
|
2020-07-28 23:09:34 +05:30
|
|
|
Gitlab::Metrics::System.monotonic_time - time_started
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def default_initial_values(query)
|
|
|
|
{
|
|
|
|
time_started: Gitlab::Metrics::System.monotonic_time,
|
|
|
|
query_string: nil,
|
|
|
|
query: query,
|
|
|
|
variables: nil,
|
2020-05-24 23:13:21 +05:30
|
|
|
duration_s: nil
|
2019-09-04 21:01:54 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|