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)
|
2021-12-11 22:18:48 +05:30
|
|
|
{
|
|
|
|
time_started: Gitlab::Metrics::System.monotonic_time,
|
|
|
|
query: query
|
|
|
|
}
|
2019-09-04 21:01:54 +05:30
|
|
|
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-12-11 22:18:48 +05:30
|
|
|
query = memo[:query]
|
|
|
|
complexity, depth, field_usages = GraphQL::Analysis.analyze_query(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.
|
2021-12-11 22:18:48 +05:30
|
|
|
memo[:duration_s] = duration(memo[:time_started])
|
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-12-11 22:18:48 +05:30
|
|
|
push_to_request_store(memo)
|
|
|
|
|
|
|
|
# This gl_analysis is included in the tracer log
|
|
|
|
query.context[:gl_analysis] = memo.except!(:time_started, :query)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => 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
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def push_to_request_store(memo)
|
|
|
|
query = memo[:query]
|
|
|
|
|
|
|
|
# TODO: This RequestStore management is used to handle setting request wide metadata
|
|
|
|
# to improve preexisting logging. We should handle this either with ApplicationContext
|
|
|
|
# or in a separate tracer.
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/343802
|
|
|
|
|
|
|
|
RequestStore.store[:graphql_logs] ||= []
|
|
|
|
RequestStore.store[:graphql_logs] << memo.except(:time_started, :duration_s, :query).merge({
|
|
|
|
variables: process_variables(query.provided_variables),
|
|
|
|
operation_name: query.operation_name
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|