Class: Gitlab::PrometheusClient
- Inherits:
-
Object
- Object
- Gitlab::PrometheusClient
- Includes:
- Utils::StrongMemoize
- Defined in:
- lib/gitlab/prometheus_client.rb
Overview
Helper methods to interact with Prometheus network services & resources
Direct Known Subclasses
Constant Summary collapse
- Error =
Class.new(StandardError)
- ConnectionError =
Class.new(Gitlab::PrometheusClient::Error)
- UnexpectedResponseError =
Class.new(Gitlab::PrometheusClient::Error)
- QueryError =
Class.new(Gitlab::PrometheusClient::Error)
- HEALTHY_RESPONSE =
"Prometheus is Healthy.\n"
- QUERY_RANGE_DATA_POINTS =
Target number of data points for ‘query_range`. Please don’t exceed the limit of 11000 data points See github.com/prometheus/prometheus/blob/91306bdf24f5395e2601773316945a478b4b263d/web/api/v1/api.go#L347
600
- QUERY_RANGE_MIN_STEP =
Minimal value of the ‘step` parameter for `query_range` in seconds.
60
- RESTCLIENT_GITLAB_HTTP_KEYMAP =
Key translation between RestClient and Gitlab::HTTP (HTTParty)
{ ssl_cert_store: :cert_store }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#aggregate(aggregate_query, time: Time.now, transform_value: :to_f) ⇒ Hash
Queries Prometheus with the given aggregate query and groups the results by mapping metric labels to their respective values.
- #health_url ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(api_url, options = {}) ⇒ PrometheusClient
constructor
A new instance of PrometheusClient.
- #label_values(name = '__name__') ⇒ Object
- #ping ⇒ Object
- #proxy(type, args) ⇒ Object
- #query(query, time: Time.now) ⇒ Object
- #query_range(query, start_time: 8.hours.ago, end_time: Time.now) ⇒ Object
- #ready? ⇒ Boolean
- #ready_url ⇒ Object
- #series(*matches, start_time: 8.hours.ago, end_time: Time.now) ⇒ Object
Constructor Details
#initialize(api_url, options = {}) ⇒ PrometheusClient
Returns a new instance of PrometheusClient.
30 31 32 33 |
# File 'lib/gitlab/prometheus_client.rb', line 30 def initialize(api_url, = {}) @api_url = api_url.chomp('/') @options = end |
Class Method Details
.compute_step(start_time, end_time) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/gitlab/prometheus_client.rb', line 107 def self.compute_step(start_time, end_time) diff = end_time - start_time step = (diff / QUERY_RANGE_DATA_POINTS).ceil [QUERY_RANGE_MIN_STEP, step].max end |
Instance Method Details
#aggregate(aggregate_query, time: Time.now, transform_value: :to_f) ⇒ Hash
Queries Prometheus with the given aggregate query and groups the results by mapping metric labels to their respective values.
90 91 92 93 94 95 96 97 |
# File 'lib/gitlab/prometheus_client.rb', line 90 def aggregate(aggregate_query, time: Time.now, transform_value: :to_f) response = query(aggregate_query, time: time) response.to_h do |result| key = block_given? ? yield(result['metric']) : result['metric'] , value = result['value'] [key, value.public_send(transform_value)] # rubocop:disable GitlabSecurity/PublicSend end end |
#health_url ⇒ Object
115 116 117 |
# File 'lib/gitlab/prometheus_client.rb', line 115 def health_url "#{api_url}/-/healthy" end |
#healthy? ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/gitlab/prometheus_client.rb', line 39 def healthy? response_body = handle_management_api_response(get(health_url, {})) # From Prometheus docs: This endpoint always returns 200 and should be used to check Prometheus health. response_body == HEALTHY_RESPONSE end |
#label_values(name = '__name__') ⇒ Object
99 100 101 |
# File 'lib/gitlab/prometheus_client.rb', line 99 def label_values(name = '__name__') json_api_get("label/#{name}/values") end |
#ping ⇒ Object
35 36 37 |
# File 'lib/gitlab/prometheus_client.rb', line 35 def ping json_api_get('query', query: '1') end |
#proxy(type, args) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/gitlab/prometheus_client.rb', line 55 def proxy(type, args) path = api_path(type) get(path, args) rescue Gitlab::HTTP::ResponseError => ex raise PrometheusClient::ConnectionError, "Network connection error" unless ex.response && ex.response.try(:code) (ex.response) end |
#query(query, time: Time.now) ⇒ Object
64 65 66 67 68 |
# File 'lib/gitlab/prometheus_client.rb', line 64 def query(query, time: Time.now) get_result('vector') do json_api_get('query', query: query, time: time.to_f) end end |
#query_range(query, start_time: 8.hours.ago, end_time: Time.now) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gitlab/prometheus_client.rb', line 70 def query_range(query, start_time: 8.hours.ago, end_time: Time.now) start_time = start_time.to_f end_time = end_time.to_f step = self.class.compute_step(start_time, end_time) get_result('matrix') do json_api_get( 'query_range', query: query, start: start_time, end: end_time, step: step ) end end |
#ready? ⇒ Boolean
46 47 48 49 50 51 52 53 |
# File 'lib/gitlab/prometheus_client.rb', line 46 def ready? response = get(ready_url, {}) # From Prometheus docs: This endpoint returns 200 when Prometheus is ready to serve traffic (i.e. respond to queries). response.code == 200 rescue StandardError => e raise PrometheusClient::UnexpectedResponseError, e..to_s end |
#ready_url ⇒ Object
119 120 121 |
# File 'lib/gitlab/prometheus_client.rb', line 119 def ready_url "#{api_url}/-/ready" end |
#series(*matches, start_time: 8.hours.ago, end_time: Time.now) ⇒ Object
103 104 105 |
# File 'lib/gitlab/prometheus_client.rb', line 103 def series(*matches, start_time: 8.hours.ago, end_time: Time.now) json_api_get('series', match: matches, start: start_time.to_f, end: end_time.to_f) end |