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.
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(options) ⇒ 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_request(options) ⇒ Object
Helper function to evalueate the low level headers option.
-
#faraday_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:.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a Prometheus API client:
A default client is created if options is omitted.
37 38 39 40 41 42 43 44 45 |
# File 'lib/prometheus/api_client/client.rb', line 37 def initialize( = {}) = DEFAULT_ARGS.merge() @client = Faraday.new( (), ) do |f| f.adapter :net_http end end |
Instance Method Details
#faraday_headers(options) ⇒ Object
Helper function to evalueate the low level headers option
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/prometheus/api_client/client.rb', line 134 def faraday_headers() return [:headers] if [:headers] headers = [:credentials] return unless headers && headers[:token] { Authorization: 'Bearer ' + headers[:token].to_s, } end |
#faraday_options(options) ⇒ Object
Helper function to create the args for the low level client
159 160 161 162 163 164 165 166 167 |
# File 'lib/prometheus/api_client/client.rb', line 159 def () { url: [:url] + [:path], proxy: faraday_proxy(), ssl: faraday_ssl(), headers: faraday_headers(), request: faraday_request(), } end |
#faraday_proxy(options) ⇒ Object
Helper function to evalueate the low level proxy option
113 114 115 116 117 118 |
# File 'lib/prometheus/api_client/client.rb', line 113 def faraday_proxy() return [:proxy] if [:proxy] proxy = [:options] proxy[:http_proxy_uri] if proxy[:http_proxy_uri] end |
#faraday_request(options) ⇒ Object
Helper function to evalueate the low level headers option
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/prometheus/api_client/client.rb', line 146 def faraday_request() return [:request] if [:request] request = [:options] return unless request[:open_timeout] || request[:timeout] { open_timeout: request[:open_timeout], timeout: request[:timeout], } end |
#faraday_ssl(options) ⇒ Object
Helper function to evalueate the low level ssl option
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/prometheus/api_client/client.rb', line 121 def faraday_ssl() return [:ssl] if [:ssl] ssl = [:options] return unless ssl[:verify_ssl] || ssl[:ssl_cert_store] { verify: ssl[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE, cert_store: ssl[:ssl_cert_store], } end |
#get(command, options) ⇒ Object
Issues a get request to the low level client.
98 99 100 |
# File 'lib/prometheus/api_client/client.rb', line 98 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.
93 94 95 |
# File 'lib/prometheus/api_client/client.rb', line 93 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.
57 58 59 |
# File 'lib/prometheus/api_client/client.rb', line 57 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.
73 74 75 |
# File 'lib/prometheus/api_client/client.rb', line 73 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.
104 105 106 107 108 109 110 |
# File 'lib/prometheus/api_client/client.rb', line 104 def run_command(command, ) response = get(command, ) JSON.parse(response.body)['data'] rescue StandardError => err raise RequestError, err. end |
#targets(options = {}) ⇒ Object
Returns an overview of the current state of the Prometheus target discovery:
No options used.
83 84 85 |
# File 'lib/prometheus/api_client/client.rb', line 83 def targets( = {}) run_command('targets', ) end |