Class: Prometheus::ApiClient::Client
- Inherits:
-
Object
- Object
- Prometheus::ApiClient::Client
- Defined in:
- lib/prometheus/api_client/client.rb
Overview
Client contains the implementation for a Prometheus compatible api_client.
Direct Known Subclasses
Prometheus::ApiClient::Cadvisor::Container, Prometheus::ApiClient::Cadvisor::Node, Prometheus::ApiClient::Cadvisor::Pod
Defined Under Namespace
Classes: RequestError
Constant Summary collapse
- DEFAULT_ARGS =
Default paramters for creating default client
{ url: 'http://localhost:9090', path: '/api/v1/', credentials: {}, options: { open_timeout: 2, timeout: 5, }, }.freeze
Instance Method Summary collapse
-
#faraday_headers(credentials) ⇒ Object
Helper function to evalueate the low level headers option.
-
#faraday_options(options) ⇒ Object
Helper function to create the args for the low level client.
-
#faraday_proxy(options) ⇒ Object
Helper function to evalueate the low level proxy option.
-
#faraday_verify_ssl(options) ⇒ Object
Helper function to evalueate the low level ssl option.
-
#get(command, options) ⇒ Object
Issues a get request to the low level client.
-
#initialize(options = {}) ⇒ Client
constructor
Create a Prometheus API client:.
-
#label(label, options = {}) ⇒ Object
Returns a list of label values for a provided label name:.
-
#query(options) ⇒ Object
Evaluates an instant query at a single point in time:.
-
#query_range(options) ⇒ Object
Evaluates an expression query over a range of time:.
-
#run_command(command, options) ⇒ Object
Issues a get request to the low level client, and evalueate the response JSON.
-
#targets(options = {}) ⇒ Object
Returns an overview of the current state of the Prometheus target discovery:.
-
#update_query(query, labels) ⇒ Object
Add labels to simple query variables.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a Prometheus API client:
A default client is created if options is omitted.
35 36 37 38 39 40 41 |
# File 'lib/prometheus/api_client/client.rb', line 35 def initialize( = {}) = DEFAULT_ARGS.merge() @client = Faraday.new( (), ) end |
Instance Method Details
#faraday_headers(credentials) ⇒ Object
Helper function to evalueate the low level headers option
137 138 139 140 141 142 143 |
# File 'lib/prometheus/api_client/client.rb', line 137 def faraday_headers(credentials) return unless credentials[:token] { Authorization: 'Bearer ' + credentials[:token].to_s, } end |
#faraday_options(options) ⇒ Object
Helper function to create the args for the low level client
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/prometheus/api_client/client.rb', line 146 def () { url: [:url] + [:path], proxy: faraday_proxy([:options]), ssl: faraday_verify_ssl([:options]), headers: faraday_headers([:credentials]), request: { open_timeout: [:options][:open_timeout], timeout: [:options][:timeout], }, } end |
#faraday_proxy(options) ⇒ Object
Helper function to evalueate the low level proxy option
122 123 124 |
# File 'lib/prometheus/api_client/client.rb', line 122 def faraday_proxy() [:http_proxy_uri] if [:http_proxy_uri] end |
#faraday_verify_ssl(options) ⇒ Object
Helper function to evalueate the low level ssl option
127 128 129 130 131 132 133 134 |
# File 'lib/prometheus/api_client/client.rb', line 127 def faraday_verify_ssl() return unless [:verify_ssl] { verify: [:verify_ssl] != OpenSSL::SSL::VERIFY_NONE, cert_store: [:ssl_cert_store], } end |
#get(command, options) ⇒ Object
Issues a get request to the low level client.
94 95 96 |
# File 'lib/prometheus/api_client/client.rb', line 94 def get(command, ) @client.get(command, ) end |
#label(label, options = {}) ⇒ Object
Returns a list of label values for a provided label name:
No options used.
89 90 91 |
# File 'lib/prometheus/api_client/client.rb', line 89 def label(label, = {}) run_command("label/#{label}/values", ) end |
#query(options) ⇒ Object
Evaluates an instant query at a single point in time:
The current server time is used if the time parameter is omitted.
53 54 55 |
# File 'lib/prometheus/api_client/client.rb', line 53 def query() run_command('query', ) end |
#query_range(options) ⇒ Object
Evaluates an expression query over a range of time:
The current server time is used if the time parameter is omitted.
69 70 71 |
# File 'lib/prometheus/api_client/client.rb', line 69 def query_range() run_command('query_range', ) end |
#run_command(command, options) ⇒ Object
Issues a get request to the low level client, and evalueate the response JSON.
100 101 102 103 104 105 106 |
# File 'lib/prometheus/api_client/client.rb', line 100 def run_command(command, ) response = get(command, ) JSON.parse(response.body)['data'] rescue raise RequestError, 'Bad response from server' end |
#targets(options = {}) ⇒ Object
Returns an overview of the current state of the Prometheus target discovery:
No options used.
79 80 81 |
# File 'lib/prometheus/api_client/client.rb', line 79 def targets( = {}) run_command('targets', ) end |
#update_query(query, labels) ⇒ Object
Add labels to simple query variables.
Example:
"cpu_usage" => "cpu_usage{labels...}"
"sum(cpu_usage)" => "sum(cpu_usage{labels...})"
"rate(cpu_usage[5m])" => "rate(cpu_usage{labels...}[5m])"
Note:
Not supporting more complex queries.
117 118 119 |
# File 'lib/prometheus/api_client/client.rb', line 117 def update_query(query, labels) query.sub(/(?<r>\[.+\])?(?<f>[)])?$/, "{#{labels}}\\k<r>\\k<f>") end |