Class: Prometheus::ApiClient::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(options = {}) ⇒ Client

Create a Prometheus API client:

A default client is created if options is omitted.

Options Hash (options):

  • :url (String)

    Server base URL.

  • :credentials (Hash)

    Authentication credentials.

  • :options (Hash)

    Options used to define connection.

  • :headers (Hash)

    Unencoded HTTP header key/value pairs.

  • :request (Hash)

    Request options.

  • :ssl (Hash)

    SSL options.

  • :proxy (String)

    Proxy url.



36
37
38
39
40
41
42
# File 'lib/prometheus/api_client/client.rb', line 36

def initialize(options = {})
  options = DEFAULT_ARGS.merge(options)

  @client = Faraday.new(
    faraday_options(options),
  )
end

Instance Method Details

#faraday_headers(options) ⇒ Object

Helper function to evalueate the low level headers option



131
132
133
134
135
136
137
138
139
140
# File 'lib/prometheus/api_client/client.rb', line 131

def faraday_headers(options)
  return options[:headers] if options[:headers]

  headers = options[: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



156
157
158
159
160
161
162
163
164
# File 'lib/prometheus/api_client/client.rb', line 156

def faraday_options(options)
  {
    url: options[:url] + options[:path],
    proxy: faraday_proxy(options),
    ssl: faraday_ssl(options),
    headers: faraday_headers(options),
    request: faraday_request(options),
  }
end

#faraday_proxy(options) ⇒ Object

Helper function to evalueate the low level proxy option



110
111
112
113
114
115
# File 'lib/prometheus/api_client/client.rb', line 110

def faraday_proxy(options)
  return options[:proxy] if options[:proxy]

  proxy = options[:options]
  proxy[:http_proxy_uri] if proxy[:http_proxy_uri]
end

#faraday_request(options) ⇒ Object

Helper function to evalueate the low level headers option



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/prometheus/api_client/client.rb', line 143

def faraday_request(options)
  return options[:request] if options[:request]

  request = options[: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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/prometheus/api_client/client.rb', line 118

def faraday_ssl(options)
  return options[:ssl] if options[:ssl]

  ssl = options[: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.



95
96
97
# File 'lib/prometheus/api_client/client.rb', line 95

def get(command, options)
  @client.get(command, options)
end

#label(label, options = {}) ⇒ Object

Returns a list of label values for a provided label name:

No options used.



90
91
92
# File 'lib/prometheus/api_client/client.rb', line 90

def label(label, options = {})
  run_command("label/#{label}/values", options)
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.

Options Hash (options):

  • :query (String)

    Prometheus expression query string.

  • :time (String)

    <rfc3339 | unix_timestamp> Evaluation timestamp. Optional.

  • :timeout (String)

    Evaluation timeout. Optional. Defaults to and is capped by the value of the -query.timeout flag.



54
55
56
# File 'lib/prometheus/api_client/client.rb', line 54

def query(options)
  run_command('query', options)
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.

Options Hash (options):

  • :query (String)

    Prometheus expression query string.

  • :start (String)

    <rfc3339 | unix_timestamp> Start timestamp.

  • :end (String)

    <rfc3339 | unix_timestamp> End timestamp.

  • :step (String)

    <duration> Query resolution step width.

  • :timeout (String)

    Evaluation timeout. Optional. Defaults to and is capped by the value of the -query.timeout flag.



70
71
72
# File 'lib/prometheus/api_client/client.rb', line 70

def query_range(options)
  run_command('query_range', options)
end

#run_command(command, options) ⇒ Object

Issues a get request to the low level client, and evalueate the response JSON.



101
102
103
104
105
106
107
# File 'lib/prometheus/api_client/client.rb', line 101

def run_command(command, options)
  response = get(command, options)

  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.



80
81
82
# File 'lib/prometheus/api_client/client.rb', line 80

def targets(options = {})
  run_command('targets', options)
end