diff options
author | charlieablett <cablett@gitlab.com> | 2019-05-02 10:09:10 +0300 |
---|---|---|
committer | charlieablett <cablett@gitlab.com> | 2019-05-30 09:27:28 +0300 |
commit | 2a1006416748950805294793f1bc8d6fa7435eea (patch) | |
tree | d54dfd57be5ddd7b9e5fc4e9d1fe271c88c41f3e /lib/gitlab/graphql | |
parent | 2c011cb5b452409db7fe1c810f1ad7440a6cedce (diff) |
Restructure complexity analyzer
Remove instance variables for class re-use, test individual methods,
use `monotonic_time`
Diffstat (limited to 'lib/gitlab/graphql')
-rw-r--r-- | lib/gitlab/graphql/query_analyzers/logger_analyzer.rb | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb index 3796eefd608..2cebd739a4e 100644 --- a/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb +++ b/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb @@ -4,10 +4,6 @@ module Gitlab module Graphql module QueryAnalyzers class LoggerAnalyzer - def initialize - @info_hash = {} - end - # Called before initializing the analyzer. # Returns true to run this analyzer, or false to skip it. def analyze?(query) @@ -18,16 +14,20 @@ module Gitlab # Returns the initial value for `memo` def initial_value(query) { - time_started: Time.zone.now, + time_started: Gitlab::Metrics::System.monotonic_time, query_string: query.query_string, - variables: query.provided_variables + variables: process_variables(query.provided_variables), + complexity: nil, + depth: nil, + duration: nil } end # This is like the `reduce` callback. # The return value is passed to the next call as `memo` def call(memo, visit_type, irep_node) - memo + memo = set_complexity(memo) + set_depth(memo) end # Called when we're done the whole visit. @@ -35,29 +35,37 @@ module Gitlab # Or, you can use this hook to write to a log, etc def final_value(memo) memo[:duration] = "#{duration(memo[:time_started]).round(1)}ms" - set_complexity - set_depth - GraphqlLogger.info(memo.except!(:time_started).merge(@info_hash)) + GraphqlLogger.info(memo.except!(:time_started)) memo end private - def set_complexity + def process_variables(variables) + if variables.respond_to?(:to_unsafe_h) + variables.to_unsafe_h + else + variables + end + end + + def set_complexity(memo) GraphQL::Analysis::QueryComplexity.new do |query, complexity_value| - @info_hash[:complexity] = complexity_value + memo[:complexity] = complexity_value end + memo end - def set_depth + def set_depth(memo) GraphQL::Analysis::QueryDepth.new do |query, depth_value| - @info_hash[:depth] = depth_value + memo[:depth] = depth_value end + memo end def duration(time_started) - nanoseconds = Time.zone.now - time_started - nanoseconds / 1000000 + nanoseconds = Gitlab::Metrics::System.monotonic_time - time_started + nanoseconds * 1000000 end end end |