2019-05-02 00:16:49 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module QueryAnalyzers
|
|
|
|
class LoggerAnalyzer
|
2019-05-23 22:43:47 +00:00
|
|
|
COMPLEXITY_ANALYZER = GraphQL::Analysis::QueryComplexity.new { |query, complexity_value| complexity_value }
|
|
|
|
DEPTH_ANALYZER = GraphQL::Analysis::QueryDepth.new { |query, depth_value| depth_value }
|
|
|
|
|
2019-05-02 00:16:49 +00:00
|
|
|
def analyze?(query)
|
2019-05-20 22:30:11 +00:00
|
|
|
Feature.enabled?(:graphql_logging, default_enabled: true)
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initial_value(query)
|
2019-05-23 22:43:47 +00:00
|
|
|
variables = process_variables(query.provided_variables)
|
|
|
|
default_initial_values(query).merge({
|
2019-05-02 00:16:49 +00:00
|
|
|
query_string: query.query_string,
|
2019-05-23 22:43:47 +00:00
|
|
|
variables: variables
|
|
|
|
})
|
|
|
|
rescue => e
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2019-05-23 22:43:47 +00:00
|
|
|
default_initial_values(query)
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(memo, visit_type, irep_node)
|
2019-05-22 05:13:06 +00:00
|
|
|
memo
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def final_value(memo)
|
2019-05-23 22:43:47 +00:00
|
|
|
return if memo.nil?
|
|
|
|
|
|
|
|
analyzers = [COMPLEXITY_ANALYZER, DEPTH_ANALYZER]
|
2019-05-23 21:29:19 +00:00
|
|
|
complexity, depth = GraphQL::Analysis.analyze_query(memo[:query], analyzers)
|
|
|
|
|
|
|
|
memo[:depth] = depth
|
|
|
|
memo[:complexity] = complexity
|
2020-05-13 21:08:55 +00:00
|
|
|
memo[:duration_s] = duration(memo[:time_started]).round(1)
|
2019-05-23 21:29:19 +00:00
|
|
|
|
|
|
|
GraphqlLogger.info(memo.except!(:time_started, :query))
|
2019-05-23 22:43:47 +00:00
|
|
|
rescue => e
|
2019-12-16 12:07:43 +00:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-05-02 07:09:10 +00:00
|
|
|
def process_variables(variables)
|
2019-05-23 22:43:47 +00:00
|
|
|
if variables.respond_to?(:to_s)
|
|
|
|
variables.to_s
|
2019-05-02 07:09:10 +00:00
|
|
|
else
|
|
|
|
variables
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-02 00:16:49 +00:00
|
|
|
def duration(time_started)
|
2019-05-02 07:09:10 +00:00
|
|
|
nanoseconds = Gitlab::Metrics::System.monotonic_time - time_started
|
|
|
|
nanoseconds * 1000000
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
2019-05-23 22:43:47 +00:00
|
|
|
|
|
|
|
def default_initial_values(query)
|
|
|
|
{
|
|
|
|
time_started: Gitlab::Metrics::System.monotonic_time,
|
|
|
|
query_string: nil,
|
|
|
|
query: query,
|
|
|
|
variables: nil,
|
2020-05-13 21:08:55 +00:00
|
|
|
duration_s: nil
|
2019-05-23 22:43:47 +00:00
|
|
|
}
|
|
|
|
end
|
2019-05-02 00:16:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|