A Ruby library for reading metrics stored on a Prometheus server.
Install
gem install prometheus-api-client
Usage
Overview
require 'prometheus/api_client'
# return a client for host http://localhost:9090/api/v1/
prometheus = Prometheus::ApiClient.client
Changing server hostname
# return a client for host http://example.com:9090/api/v1/
prometheus = Prometheus::ApiClient.client(url: 'http://example.com:9090')
Authentication proxy
If an authentication proxy ( e.g. oauth2 ) is used in a layer above the
prometheus REST server, this client can use ssl and authentication headears.
# return a client for host https://example.com/api/v1/ using a Bearer token "TopSecret"
prometheus = Prometheus::ApiClient.client(url: 'https://example.com:443',
credentials: { token: 'TopSecret' })
Low level calls
query
# send a low level get request to server
prometheus.get(
'query_range',
query: 'sum(container_cpu_usage_seconds_total' \
'{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
start: '2015-07-01T20:10:30.781Z',
end: '2015-07-02T20:10:30.781Z',
step: '120s',
)
# response from server is a low level response struct including
# fields like: method, body and request_headers
# usually users will not need to use this low level calls
...
method=:get,
body="{\"status\":\"success\",
...
High level calls
query
# send a query request to server
prometheus.query(
query: 'sum(container_cpu_usage_seconds_total' \
'{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
time: '2015-07-01T20:10:30.781Z',
)
# response from server:
{"resultType"=>"vector", "result"=>[{"metric"=>{}, "value"=>[1502350741.161, "6606.310387038"]}]}
query_range
# send a query_range request to server
prometheus.query_range(
query: 'sum(container_cpu_usage_seconds_total' \
'{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
start: '2015-07-01T20:10:30.781Z',
end: '2015-07-02T20:10:30.781Z',
step: '120s',
)
Prometheus API Ruby Client
A Ruby library for reading metrics stored on a Prometheus server.
Install
Usage
Overview
Changing server hostname
Authentication proxy
If an authentication proxy ( e.g. oauth2 ) is used in a layer above the prometheus REST server, this client can use ssl and authentication headears.
Low level calls
query
High level calls
query
query_range
label
targets
Tests
Install necessary development gems with
bundle installand run tests with rspec: