diff options
Diffstat (limited to 'lib/gitlab/graphql/query_analyzers/logger_analyzer.rb')
-rw-r--r-- | lib/gitlab/graphql/query_analyzers/logger_analyzer.rb | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb index 8119d232124..01b55a1667f 100644 --- a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb +++ b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb @@ -4,18 +4,22 @@ 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 } + def analyze?(query) Feature.enabled?(:graphql_logging, default_enabled: true) end def initial_value(query) - { - time_started: Gitlab::Metrics::System.monotonic_time, + variables = process_variables(query.provided_variables) + default_initial_values(query).merge({ query_string: query.query_string, - query: query, - variables: process_variables(query.provided_variables), - duration: nil - } + variables: variables + }) + rescue => e + Gitlab::Sentry.track_exception(e) + default_initial_values(query) end def call(memo, visit_type, irep_node) @@ -23,7 +27,9 @@ module Gitlab end def final_value(memo) - analyzers = [complexity_analyzer, depth_analyzer] + return if memo.nil? + + analyzers = [COMPLEXITY_ANALYZER, DEPTH_ANALYZER] complexity, depth = GraphQL::Analysis.analyze_query(memo[:query], analyzers) memo[:depth] = depth @@ -31,34 +37,34 @@ module Gitlab memo[:duration] = duration(memo[:time_started]).round(1) GraphqlLogger.info(memo.except!(:time_started, :query)) + rescue => e + Gitlab::Sentry.track_exception(e) end private def process_variables(variables) - if variables.respond_to?(:to_json) - variables.to_json + if variables.respond_to?(:to_s) + variables.to_s else variables end end - def complexity_analyzer - GraphQL::Analysis::QueryComplexity.new do |query, complexity_value| - complexity_value - end - end - - def depth_analyzer - GraphQL::Analysis::QueryDepth.new do |query, depth_value| - depth_value - end - end - def duration(time_started) nanoseconds = Gitlab::Metrics::System.monotonic_time - time_started nanoseconds * 1000000 end + + def default_initial_values(query) + { + time_started: Gitlab::Metrics::System.monotonic_time, + query_string: nil, + query: query, + variables: nil, + duration: nil + } + end end end end |