Skip to content

Commit 9178380

Browse files
committed
add a cadvisor class
1 parent 6175dfb commit 9178380

8 files changed

Lines changed: 275 additions & 17 deletions

File tree

examples/authentication_proxy.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'prometheus/api_client'
2+
3+
# returns a client, with authentication proxy support
4+
prometheus = Prometheus::ApiClient.client('https://example.com:443',
5+
credentials: { token: 'TopSecret' })
6+
7+
prometheus.query(
8+
query: 'container_cpu_usage_seconds_total{id="/"}',
9+
time: '2017-08-07T06:10:30.781Z',
10+
)
11+
12+
# will result in a hash containing the results data struct:
13+
#
14+
# {"resultType"=>"vector",
15+
# "result"=>
16+
# [{"metric"=>
17+
# {"__name__"=>"container_cpu_usage_seconds_total",
18+
# "beta_kubernetes_io_arch"=>"amd64",
19+
# "beta_kubernetes_io_os"=>"linux",
20+
# "cpu"=>"cpu00",
21+
# "id"=>"/",
22+
# "instance"=>"example.com",
23+
# "job"=>"kubernetes-cadvisor",
24+
# "kubernetes_io_hostname"=>"example.com",
25+
# "region"=>"infra",
26+
# "zone"=>"default"},
27+
# "value"=>[1502089057.125, "51412.007276689"]},
28+
# {"metric"=>
29+
# {"__name__"=>"container_cpu_usage_seconds_total",
30+
# "beta_kubernetes_io_arch"=>"amd64",
31+
# "beta_kubernetes_io_os"=>"linux",
32+
# "cpu"=>"cpu01",
33+
# "id"=>"/",
34+
# "instance"=>"example.com",
35+
# "job"=>"kubernetes-cadvisor",
36+
# "kubernetes_io_hostname"=>"example.com",
37+
# "region"=>"infra",
38+
# "zone"=>"default"},
39+
# "value"=>[1502089057.125, "54034.698666487"]}]}

examples/get_labels.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'prometheus/api_client'
2+
3+
# returns a client
4+
prometheus = Prometheus::ApiClient.client('http://example.com:8080')
5+
6+
prometheus.label('job')
7+
8+
# will result in an array containing the results data struct:
9+
#
10+
# ["kubernetes-apiservers", "kubernetes-cadvisor", "kubernetes-nodes",
11+
# "kubernetes-service-endpoints"]

examples/get_metrics.rb

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
require 'prometheus/api_client'
22

3-
# returns a default client
4-
prometheus = Prometheus::ApiClient.client
3+
# returns a client
4+
prometheus = Prometheus::ApiClient.client('http://example.com:8080')
55

6-
prometheus.get(
7-
'query_range',
8-
query: 'sum(container_cpu_usage_seconds_total{job="kubernetes-cadvisor"})',
9-
start: '2015-07-01T20:10:30.781Z',
10-
end: '2015-07-02T20:10:30.781Z',
6+
prometheus.query_range(
7+
query: 'container_cpu_usage_seconds_total{id="/"}',
8+
start: '2017-08-07T06:10:30.781Z',
9+
end: '2017-08-07T06:14:30.781Z',
1110
step: '120s',
1211
)
12+
13+
# will result in a hash containing the results data struct:
14+
#
15+
# {"resultType"=>"matrix",
16+
# "result"=>
17+
# [{"metric"=>
18+
# {"__name__"=>"container_cpu_usage_seconds_total",
19+
# "beta_kubernetes_io_arch"=>"amd64",
20+
# "beta_kubernetes_io_os"=>"linux",
21+
# "cpu"=>"cpu00",
22+
# "id"=>"/",
23+
# "instance"=>"example.com",
24+
# "job"=>"kubernetes-cadvisor",
25+
# "kubernetes_io_hostname"=>"example.com",
26+
# "region"=>"infra",
27+
# "zone"=>"default"},
28+
# "values"=>[[1502086230.781, "51264.830099022"],
29+
# [1502086470.781, "51277.367732154"]]},
30+
# {"metric"=>
31+
# {"__name__"=>"container_cpu_usage_seconds_total",
32+
# "beta_kubernetes_io_arch"=>"amd64",
33+
# "beta_kubernetes_io_os"=>"linux",
34+
# "cpu"=>"cpu01",
35+
# "id"=>"/",
36+
# "instance"=>"example.com",
37+
# "job"=>"kubernetes-cadvisor",
38+
# "kubernetes_io_hostname"=>"example.com",
39+
# "region"=>"infra",
40+
# "zone"=>"default"},
41+
# "values"=>[[1502086230.781, "53879.644934689"],
42+
# [1502086470.781, "53892.665282065"]]}]}

examples/low_level.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'prometheus/api_client'
2+
3+
# returns a default client
4+
prometheus = Prometheus::ApiClient.client
5+
6+
prometheus.get(
7+
'query_range',
8+
query: 'sum(container_cpu_usage_seconds_total{job="kubernetes-cadvisor"})',
9+
start: '2015-07-01T20:10:30.781Z',
10+
end: '2015-07-02T20:10:30.781Z',
11+
step: '120s',
12+
)
13+
14+
# will result in a low level Response object:
15+
#
16+
# <Faraday::Response:0x0055d24b49b998
17+
# @env=
18+
# #<struct Faraday::Env
19+
# method=:get,
20+
# body=
21+
# ...

lib/prometheus/api_client.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
require 'prometheus/api_client/client'
66

77
module Prometheus
8-
# Client is a ruby implementation for a Prometheus compatible api_client.
8+
# Api Client is a ruby implementation for a Prometheus compatible api_client.
99
module ApiClient
10+
# Default paramters for creating default client
1011
DEFAULT_ENTRYPOINT = 'http://localhost:9090'.freeze
1112
DEFAULT_ARGS = {
1213
path: '/api/v1/',
@@ -17,19 +18,33 @@ module ApiClient
1718
},
1819
}.freeze
1920

20-
# Returns a default client object
21-
def self.client(entrypoint = DEFAULT_ENTRYPOINT, args = {})
22-
args = DEFAULT_ARGS.merge(args)
21+
# Create a Prometheus API client:
22+
#
23+
# @param [String] entrypoint The Prometheus server url.
24+
#
25+
# @param [Hash] options
26+
# @option options [Hash] :url String base URL.
27+
# @option options [Hash] :params URI query unencoded key/value pairs.
28+
# @option options [Hash] :headers Unencoded HTTP header key/value pairs.
29+
# @option options [Hash] :request Request options.
30+
# @option options [Hash] :ssl SSL options.
31+
# @option options [Hash] :proxy Proxy options.
32+
#
33+
# A default client is created if options is omitted.
34+
def self.client(entrypoint = DEFAULT_ENTRYPOINT, options = {})
35+
options = DEFAULT_ARGS.merge(options)
2336

2437
Client.new(
25-
prometheus_args(entrypoint, args),
38+
prometheus_args(entrypoint, options),
2639
)
2740
end
2841

42+
# Helper function to evalueate the low level proxy option
2943
def self.prometheus_proxy(options)
3044
options[:http_proxy_uri] if options[:http_proxy_uri]
3145
end
3246

47+
# Helper function to evalueate the low level ssl option
3348
def self.prometheus_verify_ssl(options)
3449
return unless options[:verify_ssl]
3550

@@ -39,6 +54,7 @@ def self.prometheus_verify_ssl(options)
3954
}
4055
end
4156

57+
# Helper function to evalueate the low level headers option
4258
def self.prometheus_headers(credentials)
4359
return unless credentials[:token]
4460

@@ -47,6 +63,7 @@ def self.prometheus_headers(credentials)
4763
}
4864
end
4965

66+
# Helper function to create the args for the low level client
5067
def self.prometheus_args(entrypoint, args = {})
5168
{
5269
url: entrypoint + args[:path],
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# encoding: UTF-8
2+
3+
require 'prometheus/api_client/client'
4+
5+
module Prometheus
6+
# Client is a ruby implementation for a Prometheus compatible api_client.
7+
module ApiClient
8+
# Client contains the implementation for a Prometheus compatible api_client,
9+
# With special labels for the cadvisor job.
10+
module Cadvisor
11+
# Add labels to simple query variables.
12+
#
13+
# Example:
14+
# "cpu_usage" => "cpu_usage{labels...}"
15+
# "sum(cpu_usage)" => "sum(cpu_usage{labels...})"
16+
# "rate(cpu_usage[5m])" => "rate(cpu_usage{labels...}[5m])"
17+
#
18+
# Note:
19+
# Not supporting more complex queries.
20+
def self.update_query(query, labels)
21+
query.sub(/(?<r>\[.+\])?(?<f>[)])?$/, "{#{labels}}\\k<r>\\k<f>")
22+
end
23+
24+
# A client with special labels for node cadvisor metrics
25+
class Node < Client
26+
def initialize(instance, region = 'infra', zone = 'default', args = {})
27+
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
28+
"zone=\"#{zone}\",instance=\"#{instance}\""
29+
super(args)
30+
end
31+
32+
def query(options)
33+
options[:query] = update_query(options[:query], @labels)
34+
super(options)
35+
end
36+
37+
def query_range(options)
38+
options[:query] = update_query(options[:query], @labels)
39+
super(options)
40+
end
41+
end
42+
43+
# A client with special labels for pod cadvisor metrics
44+
class Pod < Client
45+
def initialize(pod_name, namespace = 'default', region = 'infra',
46+
zone = 'default', args = {})
47+
48+
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
49+
"zone=\"#{zone}\",namespace=\"#{namespace}\"," \
50+
"pod_name=\"#{pod_name}\",container_name=\"POD\""
51+
super(args)
52+
end
53+
54+
def query(options)
55+
options[:query] = update_query(options[:query], @labels)
56+
super(options)
57+
end
58+
59+
def query_range(options)
60+
options[:query] = update_query(options[:query], @labels)
61+
super(options)
62+
end
63+
end
64+
65+
# A client with special labels for container cadvisor metrics
66+
class Container < Client
67+
def initialize(container_name, pod_name, namespace = 'default',
68+
region = 'infra', args = {})
69+
70+
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
71+
"namespace=\"#{namespace}\"," \
72+
"pod_name=\"#{pod_name}\",container_name=\"#{container_name}\""
73+
super(args)
74+
end
75+
76+
def query(options)
77+
options[:query] = update_query(options[:query], @labels)
78+
super(options)
79+
end
80+
81+
def query_range(options)
82+
options[:query] = update_query(options[:query], @labels)
83+
super(options)
84+
end
85+
end
86+
end
87+
end
88+
end

lib/prometheus/api_client/client.rb

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,78 @@ module ApiClient
1010
class Client
1111
class RequestError < StandardError; end
1212

13-
def initialize(args)
14-
@client = Faraday.new(args)
13+
# Create a Prometheus API client:
14+
#
15+
# @param [Hash] options
16+
# @option options [Hash] :url String base URL.
17+
# @option options [Hash] :params URI query unencoded key/value pairs.
18+
# @option options [Hash] :headers Unencoded HTTP header key/value pairs.
19+
# @option options [Hash] :request Request options.
20+
# @option options [Hash] :ssl SSL options.
21+
# @option options [Hash] :proxy Proxy options.
22+
#
23+
# A default client is created if options is omitted.
24+
def initialize(options)
25+
@client = Faraday.new(options)
1526
end
1627

28+
# Evaluates an instant query at a single point in time:
29+
#
30+
# @param [Hash] options
31+
# @option options [String] :query Prometheus expression query string.
32+
# @option options [String] :time <rfc3339 | unix_timestamp> Evaluation
33+
# timestamp. Optional.
34+
# @option options [String] :timeout Evaluation timeout. Optional.
35+
# Defaults to and is capped by the value of the -query.timeout flag.
36+
#
37+
# The current server time is used if the time parameter is omitted.
1738
def query(options)
1839
run_command('query', options)
1940
end
2041

42+
# Evaluates an expression query over a range of time:
43+
#
44+
# @param [Hash] options
45+
# @option options [String] :query Prometheus expression query string.
46+
# @option options [String] :start <rfc3339 | unix_timestamp> Start
47+
# timestamp.
48+
# @option options [String] :end <rfc3339 | unix_timestamp> End timestamp.
49+
# @option options [String] :step <duration> Query resolution step width.
50+
# @option options [String] :timeout Evaluation timeout. Optional.
51+
# Defaults to and is capped by the value of the -query.timeout flag.
52+
#
53+
# The current server time is used if the time parameter is omitted.
2154
def query_range(options)
2255
run_command('query_range', options)
2356
end
2457

25-
def label(tag, options = {})
26-
run_command("label/#{tag}/values", options)
58+
# Returns an overview of the current state of the Prometheus target
59+
# discovery:
60+
#
61+
# @param [Hash] options
62+
#
63+
# No options used.
64+
def targets(options)
65+
run_command('targets', options)
2766
end
2867

68+
# Returns a list of label values for a provided label name:
69+
#
70+
# @param [String] label Label name
71+
# @param [Hash] options
72+
#
73+
# No options used.
74+
def label(label, options = {})
75+
run_command("label/#{label}/values", options)
76+
end
77+
78+
# Issues a get request to the low level client.
2979
def get(command, options)
3080
@client.get(command, options)
3181
end
3282

83+
# Issues a get request to the low level client, and evalueate the
84+
# response JSON.
3385
def run_command(command, options)
3486
response = get(command, options)
3587

lib/prometheus/api_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Prometheus
44
module ApiClient
5-
VERSION = '0.1.1'
5+
VERSION = '0.2.1'
66
end
77
end

0 commit comments

Comments
 (0)