Class: Prometheus::ProxyService

Inherits:
BaseService show all
Includes:
Gitlab::Utils::StrongMemoize, ReactiveCaching
Defined in:
app/services/prometheus/proxy_service.rb

Constant Summary collapse

PROMETHEUS_QUERY_API =
'query'
PROMETHEUS_QUERY_RANGE_API =
'query_range'
PROMETHEUS_SERIES_API =
'series'
PROXY_SUPPORT =
{
  PROMETHEUS_QUERY_API => {
    method: ['GET'],
    params: %w(query time timeout)
  },
  PROMETHEUS_QUERY_RANGE_API => {
    method: ['GET'],
    params: %w(query start end step timeout)
  },
  PROMETHEUS_SERIES_API => {
    method: %w(GET),
    params: %w(match start end)
  }
}.freeze

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(proxyable, method, path, params) ⇒ ProxyService

proxyable can be any model which responds to .prometheus_adapter like Environment.


59
60
61
62
63
64
65
66
67
68
# File 'app/services/prometheus/proxy_service.rb', line 59

def initialize(proxyable, method, path, params)
  @proxyable = proxyable
  @path = path

  # Convert ActionController::Parameters to hash because reactive_cache_worker
  # does not play nice with ActionController::Parameters.
  @params = filter_params(params, path).to_hash

  @method = method
end

Instance Attribute Details

#methodObject

Returns the value of attribute method


23
24
25
# File 'app/services/prometheus/proxy_service.rb', line 23

def method
  @method
end

#paramsObject

Returns the value of attribute params


23
24
25
# File 'app/services/prometheus/proxy_service.rb', line 23

def params
  @params
end

#pathObject

Returns the value of attribute path


23
24
25
# File 'app/services/prometheus/proxy_service.rb', line 23

def path
  @path
end

#proxyableObject

Returns the value of attribute proxyable


23
24
25
# File 'app/services/prometheus/proxy_service.rb', line 23

def proxyable
  @proxyable
end

Class Method Details

.from_cache(proxyable_class_name, proxyable_id, method, path, params) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/prometheus/proxy_service.rb', line 44

def self.from_cache(proxyable_class_name, proxyable_id, method, path, params)
  proxyable_class = begin
    proxyable_class_name.constantize
                    rescue NameError
                      nil
  end
  return unless proxyable_class

  proxyable = proxyable_class.find(proxyable_id)

  new(proxyable, method, path, params)
end

Instance Method Details

#cache_keyObject


93
94
95
# File 'app/services/prometheus/proxy_service.rb', line 93

def cache_key
  [@proxyable.class.name, @proxyable.id, @method, @path, @params]
end

#calculate_reactive_cache(proxyable_class_name, proxyable_id, method, path, params) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'app/services/prometheus/proxy_service.rb', line 83

def calculate_reactive_cache(proxyable_class_name, proxyable_id, method, path, params)
  return no_prometheus_response unless can_query?

  response = prometheus_client_wrapper.proxy(path, params)

  success(http_status: response.code, body: response.body)
rescue Gitlab::PrometheusClient::Error => err
  service_unavailable_response(err)
end

#executeObject


74
75
76
77
78
79
80
81
# File 'app/services/prometheus/proxy_service.rb', line 74

def execute
  return cannot_proxy_response unless can_proxy?
  return no_prometheus_response unless can_query?

  with_reactive_cache(*cache_key) do |result|
    result
  end
end

#idObject


70
71
72
# File 'app/services/prometheus/proxy_service.rb', line 70

def id
  nil
end