Skip to content

Commit 09c19fa

Browse files
committed
refactor cadvisor client
1 parent 35f98ad commit 09c19fa

4 files changed

Lines changed: 48 additions & 47 deletions

File tree

examples/cadvisor.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 client for cadvisor node data ( for node instance 'example.com' )
4+
prometheus = Prometheus::ApiClient::Cadvisor::Node.new(
5+
instance: 'example.com',
6+
url: 'http://example.com:8080',
7+
)
8+
9+
prometheus.query_range(
10+
query: 'sum(container_cpu_usage_seconds_total)',
11+
start: '2017-08-07T06:10:30.781Z',
12+
end: '2017-08-07T06:14:30.781Z',
13+
step: '120s',
14+
)
15+
16+
# will result in a hash containing the results data struct:
17+
#
18+
# {"resultType"=>"matrix", "result"=>[{"metric"=>{}, "values"=>
19+
# [[1502086230.781, "314016.35813638807"],
20+
# [1502086350.781, "5125.822337834"],
21+
# [1502086470.781, "314093.6892872209"]]}]}

lib/prometheus/api_client/cadvisor.rb

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,42 @@ 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+
# A client with special labels for cadvisor metrics
12+
class CadvisorClient < Client
13+
# Add labels to simple query variables.
14+
#
15+
# Example:
16+
# "cpu_usage" => "cpu_usage{labels...}"
17+
# "sum(cpu_usage)" => "sum(cpu_usage{labels...})"
18+
# "rate(cpu_usage[5m])" => "rate(cpu_usage{labels...}[5m])"
19+
#
20+
# Note:
21+
# Not supporting more complex queries.
22+
def update_query(query, labels)
23+
query.sub(/(?<r>\[.+\])?(?<f>[)])?$/, "{#{labels}}\\k<r>\\k<f>")
24+
end
25+
26+
# Issues a get request to the low level client.
27+
# Update the query options to use specific labels
28+
def get(command, options)
29+
options[:query] = update_query(options[:query], @labels)
30+
@client.get(command, options)
31+
end
32+
end
33+
1134
# A client with special labels for node cadvisor metrics
12-
class Node < Client
35+
class Node < CadvisorClient
1336
def initialize(options = {})
1437
instance = options[:instance]
1538

1639
@labels = "job=\"kubernetes-cadvisor\",instance=\"#{instance}\"," \
1740
'id="/"'
1841
super(options)
1942
end
20-
21-
def query(options)
22-
options[:query] = update_query(options[:query], @labels)
23-
super(options)
24-
end
25-
26-
def query_range(options)
27-
options[:query] = update_query(options[:query], @labels)
28-
super(options)
29-
end
3043
end
3144

3245
# A client with special labels for pod cadvisor metrics
33-
class Pod < Client
46+
class Pod < CadvisorClient
3447
def initialize(options = {})
3548
pod_name = options[:pod_name]
3649
namespace = options[:namespace] || 'default'
@@ -39,20 +52,10 @@ def initialize(options = {})
3952
"pod_name=\"#{pod_name}\",container_name=\"POD\""
4053
super(options)
4154
end
42-
43-
def query(options)
44-
options[:query] = update_query(options[:query], @labels)
45-
super(options)
46-
end
47-
48-
def query_range(options)
49-
options[:query] = update_query(options[:query], @labels)
50-
super(options)
51-
end
5255
end
5356

5457
# A client with special labels for container cadvisor metrics
55-
class Container < Client
58+
class Container < CadvisorClient
5659
def initialize(options = {})
5760
container_name = args[:container_name]
5861
pod_name = args[:pod_name]
@@ -62,16 +65,6 @@ def initialize(options = {})
6265
"pod_name=\"#{pod_name}\",container_name=\"#{container_name}\""
6366
super(options)
6467
end
65-
66-
def query(options)
67-
options[:query] = update_query(options[:query], @labels)
68-
super(options)
69-
end
70-
71-
def query_range(options)
72-
options[:query] = update_query(options[:query], @labels)
73-
super(options)
74-
end
7568
end
7669
end
7770
end

lib/prometheus/api_client/client.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,6 @@ def run_command(command, options)
105105
raise RequestError, 'Bad response from server'
106106
end
107107

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-
121108
# Helper function to evalueate the low level proxy option
122109
def faraday_proxy(options)
123110
options[:http_proxy_uri] if options[:http_proxy_uri]

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

0 commit comments

Comments
 (0)