prometheus-api-client 0.1.1 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a557e4448ade4360c97b1467ccf8a5afc23c244
4
- data.tar.gz: 255437c66a4bfbc9b5129519234006b44536b1b3
3
+ metadata.gz: 7248e7525d856f9fbf407b8e15b6f761e84115af
4
+ data.tar.gz: a590b35b49f625b63bc572948f2dbd4292b08a8c
5
5
  SHA512:
6
- metadata.gz: 3b1463359dae9bbbde9c7ceff7a23db40f03d92a9c6b30d33a80fcfaccf76560038acbbad48858b229e03f61f1f93041582c037269a373be58642ea59ac4fd9b
7
- data.tar.gz: 149728f4c861090bdbeda63d7356708743803269861aa3448207d281b8391f74c335aaa39b1d098b84b9057885bc09032fdb9ad36de1e477efc1a8215934c319
6
+ metadata.gz: c7df2b7fd83c373290b1fa2bbe8335dda983f376b4371b90f34d78b12ee68e3c66e81314193ec8e5c93d1ed7da0a8f8072906733d54a42906a501164e222e8ab
7
+ data.tar.gz: 8153bee7639544c55f133216e295d10f23ad82fca6045bf0686fd76268bd3cc59cac62e02fe69e580b39daf03f782214dd8ecc39edafe5c107dd71e9096a8032
data/README.md CHANGED
@@ -36,7 +36,7 @@ prometheus.get(
36
36
 
37
37
  ```ruby
38
38
  # 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')
40
40
  ```
41
41
 
42
42
  #### Authentication proxy
@@ -45,7 +45,7 @@ If an authentication proxy ( e.g. oauth2 ) is used in a layer above the promethe
45
45
 
46
46
  ```ruby
47
47
  # 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'})
49
49
  ```
50
50
 
51
51
  #### High level calls
@@ -55,8 +55,7 @@ prometheus = Prometheus::ApiClient.client('https://example.com:443', credentials
55
55
  # send a query request to server
56
56
  prometheus.query(
57
57
  :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",
60
59
  )
61
60
 
62
61
  # send a query_range request to server
@@ -70,6 +69,23 @@ prometheus.query_range(
70
69
  # send a label request to server
71
70
  prometheus.label('__name__')
72
71
  ```
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
+
73
89
  ## Tests
74
90
 
75
91
  Install necessary development gems with `bundle install` and run tests with
@@ -3,61 +3,24 @@
3
3
  require 'uri'
4
4
  require 'openssl'
5
5
  require 'prometheus/api_client/client'
6
+ require 'prometheus/api_client/cadvisor'
6
7
 
7
8
  module Prometheus
8
- # Client is a ruby implementation for a Prometheus compatible api_client.
9
+ # Api Client is a ruby implementation for a Prometheus compatible api_client.
9
10
  module ApiClient
10
- DEFAULT_ENTRYPOINT = 'http://localhost:9090'.freeze
11
- DEFAULT_ARGS = {
12
- path: '/api/v1/',
13
- credentials: {},
14
- options: {
15
- open_timeout: 2,
16
- timeout: 5,
17
- },
18
- }.freeze
19
-
20
- # Returns a default client object
21
- def self.client(entrypoint = DEFAULT_ENTRYPOINT, args = {})
22
- args = DEFAULT_ARGS.merge(args)
23
-
24
- Client.new(
25
- prometheus_args(entrypoint, args),
26
- )
27
- end
28
-
29
- def self.prometheus_proxy(options)
30
- options[:http_proxy_uri] if options[:http_proxy_uri]
31
- end
32
-
33
- def self.prometheus_verify_ssl(options)
34
- return unless options[:verify_ssl]
35
-
36
- {
37
- verify: options[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
38
- cert_store: options[:ssl_cert_store],
39
- }
40
- end
41
-
42
- def self.prometheus_headers(credentials)
43
- return unless credentials[:token]
44
-
45
- {
46
- Authorization: 'Bearer ' + credentials[:token].to_s,
47
- }
48
- end
49
-
50
- def self.prometheus_args(entrypoint, args = {})
51
- {
52
- url: entrypoint + args[:path],
53
- proxy: prometheus_proxy(args[:options]),
54
- ssl: prometheus_verify_ssl(args[:options]),
55
- headers: prometheus_headers(args[:credentials]),
56
- request: {
57
- open_timeout: args[:options][:open_timeout],
58
- timeout: args[:options][:timeout],
59
- },
60
- }
11
+ # Create a Prometheus API client:
12
+ #
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.
20
+ #
21
+ # A default client is created if options is omitted.
22
+ def self.client(options = {})
23
+ Client.new(options)
61
24
  end
62
25
  end
63
26
  end
@@ -0,0 +1,92 @@
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
+ # Create a Prometheus API client:
12
+ #
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.
20
+ #
21
+ # A default client is created if options is omitted.
22
+ class Node < Client
23
+ def initialize(instance:, **options)
24
+ region = options[:region] || 'infra'
25
+ zone = options[:zone] || 'default'
26
+
27
+ @labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
28
+ "zone=\"#{zone}\",instance=\"#{instance}\""
29
+ super(options)
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:, **options)
46
+ namespace = options[:namespace] || 'default'
47
+ region = options[:region] || 'infra'
48
+ zone = options[:zone] || 'default'
49
+
50
+ @labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
51
+ "zone=\"#{zone}\",namespace=\"#{namespace}\"," \
52
+ "pod_name=\"#{pod_name}\",container_name=\"POD\""
53
+ super(options)
54
+ end
55
+
56
+ def query(options)
57
+ options[:query] = update_query(options[:query], @labels)
58
+ super(options)
59
+ end
60
+
61
+ def query_range(options)
62
+ options[:query] = update_query(options[:query], @labels)
63
+ super(options)
64
+ end
65
+ end
66
+
67
+ # A client with special labels for container cadvisor metrics
68
+ class Container < Client
69
+ def initialize(container_name:, pod_name:, **options)
70
+ namespace = args[:namespace] || 'default'
71
+ region = options[:region] || 'infra'
72
+ zone = options[:zone] || 'default'
73
+
74
+ @labels = "job=\"kubernetes-cadvisor\",region=\"#{region}\"," \
75
+ "zone=\"#{zone}\",namespace=\"#{namespace}\"," \
76
+ "pod_name=\"#{pod_name}\",container_name=\"#{container_name}\""
77
+ super(options)
78
+ end
79
+
80
+ def query(options)
81
+ options[:query] = update_query(options[:query], @labels)
82
+ super(options)
83
+ end
84
+
85
+ def query_range(options)
86
+ options[:query] = update_query(options[:query], @labels)
87
+ super(options)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -10,26 +10,93 @@ module Prometheus
10
10
  class Client
11
11
  class RequestError < StandardError; end
12
12
 
13
- def initialize(args)
14
- @client = Faraday.new(args)
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
+
24
+ # Create a Prometheus API client:
25
+ #
26
+ # @param [Hash] options
27
+ # @option options [Hash] :url String base URL.
28
+ # @option options [Hash] :params URI query unencoded key/value pairs.
29
+ # @option options [Hash] :headers Unencoded HTTP header key/value pairs.
30
+ # @option options [Hash] :request Request options.
31
+ # @option options [Hash] :ssl SSL options.
32
+ # @option options [Hash] :proxy Proxy options.
33
+ #
34
+ # A default client is created if options is omitted.
35
+ def initialize(options)
36
+ options = DEFAULT_ARGS.merge(options)
37
+
38
+ @client = Faraday.new(
39
+ faraday_options(options),
40
+ )
15
41
  end
16
42
 
43
+ # Evaluates an instant query at a single point in time:
44
+ #
45
+ # @param [Hash] options
46
+ # @option options [String] :query Prometheus expression query string.
47
+ # @option options [String] :time <rfc3339 | unix_timestamp> Evaluation
48
+ # timestamp. Optional.
49
+ # @option options [String] :timeout Evaluation timeout. Optional.
50
+ # Defaults to and is capped by the value of the -query.timeout flag.
51
+ #
52
+ # The current server time is used if the time parameter is omitted.
17
53
  def query(options)
18
54
  run_command('query', options)
19
55
  end
20
56
 
57
+ # Evaluates an expression query over a range of time:
58
+ #
59
+ # @param [Hash] options
60
+ # @option options [String] :query Prometheus expression query string.
61
+ # @option options [String] :start <rfc3339 | unix_timestamp> Start
62
+ # timestamp.
63
+ # @option options [String] :end <rfc3339 | unix_timestamp> End timestamp.
64
+ # @option options [String] :step <duration> Query resolution step width.
65
+ # @option options [String] :timeout Evaluation timeout. Optional.
66
+ # Defaults to and is capped by the value of the -query.timeout flag.
67
+ #
68
+ # The current server time is used if the time parameter is omitted.
21
69
  def query_range(options)
22
70
  run_command('query_range', options)
23
71
  end
24
72
 
25
- def label(tag, options = {})
26
- run_command("label/#{tag}/values", options)
73
+ # Returns an overview of the current state of the Prometheus target
74
+ # discovery:
75
+ #
76
+ # @param [Hash] options
77
+ #
78
+ # No options used.
79
+ def targets(options = {})
80
+ run_command('targets', options)
81
+ end
82
+
83
+ # Returns a list of label values for a provided label name:
84
+ #
85
+ # @param [String] label Label name
86
+ # @param [Hash] options
87
+ #
88
+ # No options used.
89
+ def label(label, options = {})
90
+ run_command("label/#{label}/values", options)
27
91
  end
28
92
 
93
+ # Issues a get request to the low level client.
29
94
  def get(command, options)
30
95
  @client.get(command, options)
31
96
  end
32
97
 
98
+ # Issues a get request to the low level client, and evalueate the
99
+ # response JSON.
33
100
  def run_command(command, options)
34
101
  response = get(command, options)
35
102
 
@@ -37,6 +104,57 @@ module Prometheus
37
104
  rescue
38
105
  raise RequestError, 'Bad response from server'
39
106
  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
40
158
  end
41
159
  end
42
160
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Prometheus
4
4
  module ApiClient
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.10'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaacov Zamir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-06 00:00:00.000000000 Z
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: quantile
@@ -48,6 +48,7 @@ files:
48
48
  - README.md
49
49
  - lib/prometheus.rb
50
50
  - lib/prometheus/api_client.rb
51
+ - lib/prometheus/api_client/cadvisor.rb
51
52
  - lib/prometheus/api_client/client.rb
52
53
  - lib/prometheus/api_client/version.rb
53
54
  homepage: https://github.com/yaacov/prometheus_api_client_ruby