Skip to content

Commit da8ffdc

Browse files
committed
add cAdvisor class
1 parent 9178380 commit da8ffdc

8 files changed

Lines changed: 120 additions & 88 deletions

File tree

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ prometheus.get(
3636

3737
```ruby
3838
# return a client for host http://example.com:9090/api/v1/
39-
prometheus = Prometheus::ApiClient.client('http://example.com:9090')
39+
prometheus = Prometheus::ApiClient.client(url: 'http://example.com:9090')
4040
```
4141

4242
#### Authentication proxy
@@ -45,7 +45,7 @@ If an authentication proxy ( e.g. oauth2 ) is used in a layer above the promethe
4545

4646
```ruby
4747
# return a client for host https://example.com/api/v1/ using a Bearer token "TopSecret"
48-
prometheus = Prometheus::ApiClient.client('https://example.com:443', credentials: {token: 'TopSecret'})
48+
prometheus = Prometheus::ApiClient.client(url: 'https://example.com:443', credentials: {token: 'TopSecret'})
4949
```
5050

5151
#### High level calls
@@ -55,8 +55,7 @@ prometheus = Prometheus::ApiClient.client('https://example.com:443', credentials
5555
# send a query request to server
5656
prometheus.query(
5757
:query => "sum(container_cpu_usage_seconds_total{container_name=\"prometheus-hgv4s\",job=\"kubernetes-nodes\"})",
58-
:start => "2015-07-01T20:10:30.781Z",
59-
:end => "2015-07-02T20:10:30.781Z"
58+
:time => "2015-07-01T20:10:30.781Z",
6059
)
6160

6261
# send a query_range request to server
@@ -70,6 +69,23 @@ prometheus.query_range(
7069
# send a label request to server
7170
prometheus.label('__name__')
7271
```
72+
73+
#### cAdvisor specialize client
74+
75+
```ruby
76+
77+
# create a client for cAdvisor metrics of a Node instance 'example.com'
78+
prometheus = Prometheus::ApiClient::Cadvisor::Node.new(
79+
instance: 'example.com',
80+
url: 'http://example.com:8080',
81+
)
82+
83+
# send a query request to server
84+
prometheus.query(
85+
:query => "sum(container_cpu_usage_seconds_total)",
86+
:time => "2015-07-01T20:10:30.781Z",
87+
)
88+
7389
## Tests
7490

7591
Install necessary development gems with `bundle install` and run tests with

examples/authentication_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'prometheus/api_client'
22

33
# returns a client, with authentication proxy support
4-
prometheus = Prometheus::ApiClient.client('https://example.com:443',
4+
prometheus = Prometheus::ApiClient.client(url: 'https://example.com:443',
55
credentials: { token: 'TopSecret' })
66

77
prometheus.query(

examples/get_labels.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'prometheus/api_client'
22

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

66
prometheus.label('job')
77

examples/get_metrics.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'prometheus/api_client'
22

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

66
prometheus.query_range(
77
query: 'container_cpu_usage_seconds_total{id="/"}',

lib/prometheus/api_client.rb

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,13 @@
33
require 'uri'
44
require 'openssl'
55
require 'prometheus/api_client/client'
6+
require 'prometheus/api_client/cadvisor'
67

78
module Prometheus
89
# Api Client is a ruby implementation for a Prometheus compatible api_client.
910
module ApiClient
10-
# Default paramters for creating default client
11-
DEFAULT_ENTRYPOINT = 'http://localhost:9090'.freeze
12-
DEFAULT_ARGS = {
13-
path: '/api/v1/',
14-
credentials: {},
15-
options: {
16-
open_timeout: 2,
17-
timeout: 5,
18-
},
19-
}.freeze
20-
2111
# Create a Prometheus API client:
2212
#
23-
# @param [String] entrypoint The Prometheus server url.
24-
#
2513
# @param [Hash] options
2614
# @option options [Hash] :url String base URL.
2715
# @option options [Hash] :params URI query unencoded key/value pairs.
@@ -31,50 +19,8 @@ module ApiClient
3119
# @option options [Hash] :proxy Proxy options.
3220
#
3321
# A default client is created if options is omitted.
34-
def self.client(entrypoint = DEFAULT_ENTRYPOINT, options = {})
35-
options = DEFAULT_ARGS.merge(options)
36-
37-
Client.new(
38-
prometheus_args(entrypoint, options),
39-
)
40-
end
41-
42-
# Helper function to evalueate the low level proxy option
43-
def self.prometheus_proxy(options)
44-
options[:http_proxy_uri] if options[:http_proxy_uri]
45-
end
46-
47-
# Helper function to evalueate the low level ssl option
48-
def self.prometheus_verify_ssl(options)
49-
return unless options[:verify_ssl]
50-
51-
{
52-
verify: options[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
53-
cert_store: options[:ssl_cert_store],
54-
}
55-
end
56-
57-
# Helper function to evalueate the low level headers option
58-
def self.prometheus_headers(credentials)
59-
return unless credentials[:token]
60-
61-
{
62-
Authorization: 'Bearer ' + credentials[:token].to_s,
63-
}
64-
end
65-
66-
# Helper function to create the args for the low level client
67-
def self.prometheus_args(entrypoint, args = {})
68-
{
69-
url: entrypoint + args[:path],
70-
proxy: prometheus_proxy(args[:options]),
71-
ssl: prometheus_verify_ssl(args[:options]),
72-
headers: prometheus_headers(args[:credentials]),
73-
request: {
74-
open_timeout: args[:options][:open_timeout],
75-
timeout: args[:options][:timeout],
76-
},
77-
}
22+
def self.client(options = {})
23+
Client.new(options)
7824
end
7925
end
8026
end

lib/prometheus/api_client/cadvisor.rb

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ module ApiClient
88
# Client contains the implementation for a Prometheus compatible api_client,
99
# With special labels for the cadvisor job.
1010
module Cadvisor
11-
# Add labels to simple query variables.
11+
# Create a Prometheus API client:
1212
#
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])"
13+
# @param [Hash] options
14+
# @option options [Hash] :url String base URL.
15+
# @option options [Hash] :params URI query unencoded key/value pairs.
16+
# @option options [Hash] :headers Unencoded HTTP header key/value pairs.
17+
# @option options [Hash] :request Request options.
18+
# @option options [Hash] :ssl SSL options.
19+
# @option options [Hash] :proxy Proxy options.
1720
#
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
21+
# A default client is created if options is omitted.
2522
class Node < Client
26-
def initialize(instance, region = 'infra', zone = 'default', args = {})
23+
def initialize(instance:, **options)
24+
region = options[:region] || 'infra'
25+
zone = options[:zone] || 'default'
26+
2727
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
2828
"zone=\"#{zone}\",instance=\"#{instance}\""
29-
super(args)
29+
super(options)
3030
end
3131

3232
def query(options)
@@ -42,13 +42,15 @@ def query_range(options)
4242

4343
# A client with special labels for pod cadvisor metrics
4444
class Pod < Client
45-
def initialize(pod_name, namespace = 'default', region = 'infra',
46-
zone = 'default', args = {})
45+
def initialize(pod_name:, **options)
46+
namespace = options[:namespace] || 'default'
47+
region = options[:region] || 'infra'
48+
zone = options[:zone] || 'default'
4749

4850
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
4951
"zone=\"#{zone}\",namespace=\"#{namespace}\"," \
5052
"pod_name=\"#{pod_name}\",container_name=\"POD\""
51-
super(args)
53+
super(options)
5254
end
5355

5456
def query(options)
@@ -64,13 +66,15 @@ def query_range(options)
6466

6567
# A client with special labels for container cadvisor metrics
6668
class Container < Client
67-
def initialize(container_name, pod_name, namespace = 'default',
68-
region = 'infra', args = {})
69+
def initialize(container_name:, pod_name:, **options)
70+
namespace = args[:namespace] || 'default'
71+
region = options[:region] || 'infra'
72+
zone = options[:zone] || 'default'
6973

7074
@labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
71-
"namespace=\"#{namespace}\"," \
75+
"zone=\"#{zone}\",namespace=\"#{namespace}\"," \
7276
"pod_name=\"#{pod_name}\",container_name=\"#{container_name}\""
73-
super(args)
77+
super(options)
7478
end
7579

7680
def query(options)

lib/prometheus/api_client/client.rb

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ module ApiClient
1010
class Client
1111
class RequestError < StandardError; end
1212

13+
# Default paramters for creating default client
14+
DEFAULT_ARGS = {
15+
url: 'http://localhost:9090',
16+
path: '/api/v1/',
17+
credentials: {},
18+
options: {
19+
open_timeout: 2,
20+
timeout: 5,
21+
},
22+
}.freeze
23+
1324
# Create a Prometheus API client:
1425
#
1526
# @param [Hash] options
@@ -22,7 +33,11 @@ class RequestError < StandardError; end
2233
#
2334
# A default client is created if options is omitted.
2435
def initialize(options)
25-
@client = Faraday.new(options)
36+
options = DEFAULT_ARGS.merge(options)
37+
38+
@client = Faraday.new(
39+
faraday_options(options),
40+
)
2641
end
2742

2843
# Evaluates an instant query at a single point in time:
@@ -61,7 +76,7 @@ def query_range(options)
6176
# @param [Hash] options
6277
#
6378
# No options used.
64-
def targets(options)
79+
def targets(options = {})
6580
run_command('targets', options)
6681
end
6782

@@ -89,6 +104,57 @@ def run_command(command, options)
89104
rescue
90105
raise RequestError, 'Bad response from server'
91106
end
107+
108+
# Add labels to simple query variables.
109+
#
110+
# Example:
111+
# "cpu_usage" => "cpu_usage{labels...}"
112+
# "sum(cpu_usage)" => "sum(cpu_usage{labels...})"
113+
# "rate(cpu_usage[5m])" => "rate(cpu_usage{labels...}[5m])"
114+
#
115+
# Note:
116+
# Not supporting more complex queries.
117+
def update_query(query, labels)
118+
query.sub(/(?<r>\[.+\])?(?<f>[)])?$/, "{#{labels}}\\k<r>\\k<f>")
119+
end
120+
121+
# Helper function to evalueate the low level proxy option
122+
def faraday_proxy(options)
123+
options[:http_proxy_uri] if options[:http_proxy_uri]
124+
end
125+
126+
# Helper function to evalueate the low level ssl option
127+
def faraday_verify_ssl(options)
128+
return unless options[:verify_ssl]
129+
130+
{
131+
verify: options[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
132+
cert_store: options[:ssl_cert_store],
133+
}
134+
end
135+
136+
# Helper function to evalueate the low level headers option
137+
def faraday_headers(credentials)
138+
return unless credentials[:token]
139+
140+
{
141+
Authorization: 'Bearer ' + credentials[:token].to_s,
142+
}
143+
end
144+
145+
# Helper function to create the args for the low level client
146+
def faraday_options(options)
147+
{
148+
url: options[:url] + options[:path],
149+
proxy: faraday_proxy(options[:options]),
150+
ssl: faraday_verify_ssl(options[:options]),
151+
headers: faraday_headers(options[:credentials]),
152+
request: {
153+
open_timeout: options[:options][:open_timeout],
154+
timeout: options[:options][:timeout],
155+
},
156+
}
157+
end
92158
end
93159
end
94160
end

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.2.1'
5+
VERSION = '0.2.10'
66
end
77
end

0 commit comments

Comments
 (0)