Class: Analytics::Glql::QueryService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/analytics/glql/query_service.rb

Overview

Service for handling GLQL queries with rate limiting, complexity tracking, and logging Used by both Glql::BaseController and API::Glql

Constant Summary collapse

GlqlQueryLockedError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(current_user:, original_query:, request: nil, current_organization: nil) ⇒ QueryService

Returns a new instance of QueryService.



12
13
14
15
16
17
# File 'app/services/analytics/glql/query_service.rb', line 12

def initialize(current_user:, original_query:, request: nil, current_organization: nil)
  @current_user = current_user
  @original_query = original_query
  @request = request
  @current_organization = current_organization
end

Instance Method Details

#execute(query:, variables: {}, context: {}) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/services/analytics/glql/query_service.rb', line 19

def execute(query:, variables: {}, context: {})
  @query = query
  @variables = variables
  @graphql_context = build_graphql_context(context)

  start_time = Gitlab::Metrics::System.monotonic_time
  exception_caught = nil

  begin
    check_rate_limit
    result = execute_graphql

    # Enhance GraphQL logs with GLQL metadata for both controller and API
    enhance_graphql_logs

    query_result = {
      data: result&.dig('data'),
      errors: result&.dig('errors'),
      complexity_score: extract_complexity_score,
      duration_s: Gitlab::Metrics::System.monotonic_time - start_time,
      timeout_occurred: false,
      rate_limited: false
    }

    # Log execution metrics for monitoring and optimization
    log_execution_metrics(query_result)

    query_result

  rescue GlqlQueryLockedError => e
    exception_caught = e
    # Enhance logs even for rate limited queries
    enhance_graphql_logs

    {
      data: nil,
      errors: [{ message: e.message }],
      complexity_score: nil,
      duration_s: Gitlab::Metrics::System.monotonic_time - start_time,
      timeout_occurred: false,
      rate_limited: true
    }

  rescue ActiveRecord::QueryAborted => e
    exception_caught = e
    # Handle timeout - increment rate limiter
    increment_rate_limit_counter

    # Enhance logs for timeout scenarios
    enhance_graphql_logs

    query_result = {
      data: nil,
      errors: [{ message: 'Query timed out' }],
      complexity_score: extract_complexity_score,
      duration_s: Gitlab::Metrics::System.monotonic_time - start_time,
      timeout_occurred: true,
      rate_limited: false
    }

    # Log execution metrics for monitoring and optimization
    log_execution_metrics(query_result)

    query_result

  rescue StandardError => e
    exception_caught = e
    # Handle other errors
    enhance_graphql_logs

    {
      data: nil,
      errors: [{ message: e.message }],
      complexity_score: extract_complexity_score,
      duration_s: Gitlab::Metrics::System.monotonic_time - start_time,
      timeout_occurred: false,
      rate_limited: false,
      exception: e
    }

  ensure
    # Record SLI metrics
    increment_glql_sli(
      duration_s: Gitlab::Metrics::System.monotonic_time - start_time,
      error_type: error_type_from(exception_caught)
    )
  end
end