Class: Grafana::ProxyService

Inherits:
BaseService show all
Includes:
ReactiveCaching
Defined in:
app/services/grafana/proxy_service.rb

Constant Summary

Constants included from ReactiveCaching

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

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params

Class Method Summary collapse

Instance Method Summary collapse

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(project, datasource_id, proxy_path, query_params) ⇒ ProxyService

Returns a new instance of ProxyService.

Parameters:

  • project (Project)

    Project for which grafana is configured.

  • datasource_id (String)

    Grafana datasource id for Prometheus instance

  • proxy_path (String)

    Path to Prometheus endpoint; EX) 'api/v1/query_range'

  • query_params (Hash<String, String>)

    Supported params: [query, start, end, step]


33
34
35
36
37
38
# File 'app/services/grafana/proxy_service.rb', line 33

def initialize(project, datasource_id, proxy_path, query_params)
  @project = project
  @datasource_id = datasource_id
  @proxy_path = proxy_path
  @query_params = query_params
end

Instance Attribute Details

#datasource_idObject

Returns the value of attribute datasource_id.


18
19
20
# File 'app/services/grafana/proxy_service.rb', line 18

def datasource_id
  @datasource_id
end

#projectObject

Returns the value of attribute project.


18
19
20
# File 'app/services/grafana/proxy_service.rb', line 18

def project
  @project
end

#proxy_pathObject

Returns the value of attribute proxy_path.


18
19
20
# File 'app/services/grafana/proxy_service.rb', line 18

def proxy_path
  @proxy_path
end

#query_paramsObject

Returns the value of attribute query_params.


18
19
20
# File 'app/services/grafana/proxy_service.rb', line 18

def query_params
  @query_params
end

Class Method Details

.from_cache(project_id, datasource_id, proxy_path, query_params) ⇒ Object

See #initialize for other parameters.

Parameters:

  • project_id (Integer)

    Project id for which grafana is configured.


23
24
25
26
27
# File 'app/services/grafana/proxy_service.rb', line 23

def self.from_cache(project_id, datasource_id, proxy_path, query_params)
  project = Project.find(project_id)

  new(project, datasource_id, proxy_path, query_params)
end

Instance Method Details

#cache_keyObject


66
67
68
# File 'app/services/grafana/proxy_service.rb', line 66

def cache_key
  [project.id, datasource_id, proxy_path, query_params]
end

#calculate_reactive_cacheObject


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/grafana/proxy_service.rb', line 46

def calculate_reactive_cache(*)
  return cannot_proxy_response unless client

  response = client.proxy_datasource(
    datasource_id: datasource_id,
    proxy_path: proxy_path,
    query: query_params
  )

  success(http_status: response.code, body: response.body)
rescue ::Grafana::Client::Error => error
  service_unavailable_response(error)
end

#executeObject


40
41
42
43
44
# File 'app/services/grafana/proxy_service.rb', line 40

def execute
  return cannot_proxy_response unless client

  with_reactive_cache(*cache_key) { |result| result }
end

#idObject

Required for ReactiveCaching; Usage overridden by self.reactive_cache_worker_finder


62
63
64
# File 'app/services/grafana/proxy_service.rb', line 62

def id
  nil
end