summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/query_analyzers/logger_analyzer.rb
blob: 1e49c894ecb4be5d780e5d42fb8f30722964c5d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module Gitlab
  module Graphql
    module QueryAnalyzers
      class LoggerAnalyzer
        def analyze?(query)
          Feature.enabled?(:graphql_logging, default_enabled: true)
        end

        def initial_value(query)
          analyzers = [complexity_analyzer, depth_analyzer]
          complexity, depth = GraphQL::Analysis.analyze_query(query, analyzers)
          {
            time_started: Gitlab::Metrics::System.monotonic_time,
            query_string: query.query_string,
            variables: process_variables(query.provided_variables),
            complexity: complexity,
            depth: depth,
            duration: nil
          }
        end

        def call(memo, visit_type, irep_node)
          memo
        end

        def final_value(memo)
          memo[:duration] = "#{duration(memo[:time_started]).round(1)}ms"
          GraphqlLogger.info(memo.except!(:time_started))
          memo
        end

        private

        def process_variables(variables)
          if variables.respond_to?(:to_unsafe_h)
            variables.to_unsafe_h
          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
      end
    end
  end
end