prometheus-api-client 0.3.4 → 0.4.1

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: 417daaa76204d57bd000be9a56ed9ab8f0ef6f7c
4
- data.tar.gz: 7880f323f43968220a424d50446763858407c7f1
3
+ metadata.gz: 326c02ee0f9c8cdde2bca091c94117e8d5c40079
4
+ data.tar.gz: a1f70967df54fe8bd24228d9292f807b5e7e2875
5
5
  SHA512:
6
- metadata.gz: 177277cb8fa60af5cfd695b4e5582ec492b0312b56c56254eba4b0ef9f793042dbc199a3a4f624c430de1b74ba73c00c153dd8f7a1806d92cae1968381d68d4c
7
- data.tar.gz: 2bf2797121bb33425b2617ce4c93323f79cbfaea9594bc002dde448c955b444d3a6f4131520bc6c5cad5bc50224c7079e0daadc02f624844a20865b683ebea5c
6
+ metadata.gz: 561e2bdbf187d4d99fc2758e522258b84e7f85a9777718b14cd9f45faa2977e3135116b0ba60c0a4b16cc3d55acce6e4b919cd4ecb3e00b81e94b04e3b2df480
7
+ data.tar.gz: ac533dd2ca2bae96e190eb06d74cca8d6dd104842b6b2e0123df21b9635244ef27594a422172bf8b2a7151e4c1cdce4d1211787359395f5ac623857a0af1d23b
data/README.md CHANGED
@@ -22,15 +22,6 @@ require 'prometheus/api_client'
22
22
 
23
23
  # return a client for host http://localhost:9090/api/v1/
24
24
  prometheus = Prometheus::ApiClient.client
25
-
26
- prometheus.get(
27
- "query_range",
28
- query: "sum(container_cpu_usage_seconds_total" \
29
- "{container_name=\"prometheus-hgv4s\",job=\"kubernetes-nodes\"})",
30
- start: "2015-07-01T20:10:30.781Z",
31
- end: "2015-07-02T20:10:30.781Z",
32
- step: "120s"
33
- )
34
25
  ```
35
26
 
36
27
  #### Changing server hostname
@@ -48,31 +39,64 @@ prometheus REST server, this client can use ssl and authentication headears.
48
39
  ```ruby
49
40
  # return a client for host https://example.com/api/v1/ using a Bearer token "TopSecret"
50
41
  prometheus = Prometheus::ApiClient.client(url: 'https://example.com:443',
51
- credentials: {token: 'TopSecret'})
42
+ credentials: { token: 'TopSecret' })
52
43
  ```
53
44
 
45
+ #### Low lavel calls
46
+
47
+ ###### query
48
+
49
+ ```ruby
50
+ require 'prometheus/api_client'
51
+
52
+ prometheus.get(
53
+ 'query_range',
54
+ query: 'sum(container_cpu_usage_seconds_total' \
55
+ '{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
56
+ start: '2015-07-01T20:10:30.781Z',
57
+ end: '2015-07-02T20:10:30.781Z',
58
+ step: '120s',
59
+ )
60
+ ```
61
+ ```
62
+ # response from server is a low level response struct including
63
+ # fields like: method, body and request_headers
64
+ # usually users will not need to use this law level calls
65
+ ...
66
+ method=:get,
67
+ body="{\"status\":\"success\",
68
+ ...
69
+ ```
54
70
  #### High level calls
55
71
 
72
+ ###### query
73
+
56
74
  ```ruby
57
75
 
58
76
  # send a query request to server
59
77
  prometheus.query(
60
- query: "sum(container_cpu_usage_seconds_total" \
61
- "{container_name=\"prometheus-hgv4s\",job=\"kubernetes-nodes\"})",
62
- time: "2015-07-01T20:10:30.781Z",
78
+ query: 'sum(container_cpu_usage_seconds_total' \
79
+ '{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
80
+ time: '2015-07-01T20:10:30.781Z',
63
81
  )
64
82
  ```
83
+ ```
84
+ # response from server:
85
+ {"resultType"=>"vector", "result"=>[{"metric"=>{}, "value"=>[1502350741.161, "6606.310387038"]}]}
86
+ ```
87
+ ###### query_range
65
88
 
66
89
  ```ruby
67
90
  # send a query_range request to server
68
91
  prometheus.query_range(
69
- query: "sum(container_cpu_usage_seconds_total" \
70
- "{container_name=\"prometheus-hgv4s\",job=\"kubernetes-nodes\"})",
71
- start: "2015-07-01T20:10:30.781Z",
72
- end: "2015-07-02T20:10:30.781Z",
73
- step: "120s"
92
+ query: 'sum(container_cpu_usage_seconds_total' \
93
+ '{container_name="prometheus-hgv4s",job="kubernetes-nodes"})',
94
+ start: '2015-07-01T20:10:30.781Z',
95
+ end: '2015-07-02T20:10:30.781Z',
96
+ step: '120s',
74
97
  )
75
-
98
+ ```
99
+ ```
76
100
  # response from server:
77
101
  {"resultType"=>"matrix",
78
102
  "result"=>
@@ -104,36 +128,85 @@ prometheus.query_range(
104
128
  [1502086470.781, "53892.665282065"]]}]}
105
129
  ```
106
130
 
131
+ ###### label
132
+
107
133
  ```ruby
108
134
  # send a label request to server
109
135
  prometheus.label('__name__')
110
-
136
+ ```
137
+ ```
111
138
  # response from server:
112
- ["kubernetes-apiservers", "kubernetes-cadvisor", "kubernetes-nodes",
113
- "kubernetes-service-endpoints"]
139
+ ["APIServiceRegistrationController_adds",
140
+ "APIServiceRegistrationController_depth",
141
+ ...
142
+
114
143
  ```
115
144
 
145
+ ###### targets
146
+
116
147
  ```ruby
117
148
  # send a targets request to server
118
149
  prometheus.targets()
119
150
  ```
151
+ ```
152
+ # response from server:
153
+ {"activeTargets"=>
154
+ [{"discoveredLabels"=>
155
+ {"__address__"=>"10.35.19.248:8443",
156
+ "__meta_kubernetes_endpoint_port_name"=>"https",
157
+ "__meta_kubernetes_endpoint_port_protocol"=>"TCP",
158
+ "__meta_kubernetes_endpoint_ready"=>"true",
159
+ "__meta_kubernetes_endpoints_name"=>"kubernetes",
160
+ "__meta_kubernetes_namespace"=>"default",
161
+ "__meta_kubernetes_service_label_component"=>"apiserver",
162
+ "__meta_kubernetes_service_label_provider"=>"kubernetes",
163
+ "__meta_kubernetes_service_name"=>"kubernetes",
164
+ "__metrics_path__"=>"/metrics",
165
+ "__scheme__"=>"https",
166
+ "job"=>"kubernetes-apiservers"},
167
+ "labels"=>{"instance"=>"10.35.19.248:8443", "job"=>"kubernetes-apiservers"},
168
+ "scrapeUrl"=>"https://10.35.19.248:8443/metrics",
169
+ "lastError"=>"",
170
+ "lastScrape"=>"2017-08-10T07:35:40.919376413Z",
171
+ "health"=>"up"},
172
+ ...
173
+
174
+ ```
120
175
 
121
176
  #### cAdvisor specialize client
122
177
 
178
+ A cAdvisor client is a client that add object specific labels to each REST call,
179
+ objects available are Node, Pod and Container.
180
+
181
+ ###### Node
182
+
183
+ Add the instance label, user must declare the instance on client creation.
184
+
185
+ ###### Pod
186
+
187
+ Add the pod_name label, user must declare the pod_name on client creation.
188
+
189
+ ###### Container
190
+
191
+ Add the container_name and pod_name labels, user must declare container_name and pod_name on client creation.
192
+
193
+ ###### Example
194
+
123
195
  ```ruby
124
196
 
125
197
  # create a client for cAdvisor metrics of a Node instance 'example.com'
126
198
  # connected to a Prometheus server listening on http://example.com:8080
127
199
  prometheus = Prometheus::ApiClient::Cadvisor::Node.new(
128
200
  instance: 'example.com',
129
- url: 'http://example.com:8080',
201
+ url: 'http://example.com:8080',
130
202
  )
131
203
 
132
204
  # send a query request to server
133
- prometheus.query(
134
- query: "sum(container_cpu_usage_seconds_total)",
135
- time: "2015-07-01T20:10:30.781Z",
136
- )
205
+ prometheus.query(query: 'sum(container_cpu_usage_seconds_total)')
206
+ ```
207
+ ```
208
+ # response from server:
209
+ {"resultType"=>"vector", "result"=>[{"metric"=>{}, "value"=>[1502350741.161, "6606.310387038"]}]}
137
210
  ```
138
211
 
139
212
  ## Tests
@@ -14,11 +14,10 @@ module Prometheus
14
14
  # @option options [String] :url Server base URL.
15
15
  # @option options [Hash] :credentials Authentication credentials.
16
16
  # @option options [Hash] :options Options used to define connection.
17
- # @option options [Hash] :params URI query unencoded key/value pairs.
18
17
  # @option options [Hash] :headers Unencoded HTTP header key/value pairs.
19
18
  # @option options [Hash] :request Request options.
20
19
  # @option options [Hash] :ssl SSL options.
21
- # @option options [Hash] :proxy Proxy options.
20
+ # @option options [String] :proxy Proxy url.
22
21
  #
23
22
  # A default client is created if options is omitted.
24
23
  def self.client(options = {})
@@ -24,14 +24,13 @@ module Prometheus
24
24
  # Create a Prometheus API client:
25
25
  #
26
26
  # @param [Hash] options
27
- # @option options [String] :url server base URL.
27
+ # @option options [String] :url Server base URL.
28
28
  # @option options [Hash] :credentials Authentication credentials.
29
29
  # @option options [Hash] :options Options used to define connection.
30
- # @option options [Hash] :params URI query unencoded key/value pairs.
31
30
  # @option options [Hash] :headers Unencoded HTTP header key/value pairs.
32
31
  # @option options [Hash] :request Request options.
33
32
  # @option options [Hash] :ssl SSL options.
34
- # @option options [Hash] :proxy Proxy options.
33
+ # @option options [String] :proxy Proxy url.
35
34
  #
36
35
  # A default client is created if options is omitted.
37
36
  def initialize(options = {})
@@ -109,39 +108,58 @@ module Prometheus
109
108
 
110
109
  # Helper function to evalueate the low level proxy option
111
110
  def faraday_proxy(options)
112
- options[:http_proxy_uri] if options[:http_proxy_uri]
111
+ return options[:proxy] if options[:proxy]
112
+
113
+ proxy = options[:options]
114
+ proxy[:http_proxy_uri] if proxy[:http_proxy_uri]
113
115
  end
114
116
 
115
117
  # Helper function to evalueate the low level ssl option
116
- def faraday_verify_ssl(options)
117
- return unless options[:verify_ssl]
118
-
119
- {
120
- verify: options[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
121
- cert_store: options[:ssl_cert_store],
122
- }
118
+ def faraday_ssl(options)
119
+ return options[:ssl] if options[:ssl]
120
+
121
+ ssl = options[:options]
122
+ if ssl[:verify_ssl] || ssl[:ssl_cert_store]
123
+ {
124
+ verify: ssl[:verify_ssl] != OpenSSL::SSL::VERIFY_NONE,
125
+ cert_store: ssl[:ssl_cert_store],
126
+ }
127
+ end
123
128
  end
124
129
 
125
130
  # Helper function to evalueate the low level headers option
126
- def faraday_headers(credentials)
127
- return unless credentials[:token]
131
+ def faraday_headers(options)
132
+ return options[:headers] if options[:headers]
133
+
134
+ headers = options[:credentials]
135
+ if headers[:token]
136
+ {
137
+ Authorization: 'Bearer ' + headers[:token].to_s,
138
+ }
139
+ end
140
+ end
128
141
 
129
- {
130
- Authorization: 'Bearer ' + credentials[:token].to_s,
131
- }
142
+ # Helper function to evalueate the low level headers option
143
+ def faraday_request(options)
144
+ return options[:request] if options[:request]
145
+
146
+ request = options[:options]
147
+ if request[:open_timeout] || request[:timeout]
148
+ {
149
+ open_timeout: request[:open_timeout],
150
+ timeout: request[:timeout],
151
+ }
152
+ end
132
153
  end
133
154
 
134
155
  # Helper function to create the args for the low level client
135
156
  def faraday_options(options)
136
157
  {
137
158
  url: options[:url] + options[:path],
138
- proxy: faraday_proxy(options[:options]),
139
- ssl: faraday_verify_ssl(options[:options]),
140
- headers: faraday_headers(options[:credentials]),
141
- request: {
142
- open_timeout: options[:options][:open_timeout],
143
- timeout: options[:options][:timeout],
144
- },
159
+ proxy: faraday_proxy(options),
160
+ ssl: faraday_ssl(options),
161
+ headers: faraday_headers(options),
162
+ request: faraday_request(options),
145
163
  }
146
164
  end
147
165
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Prometheus
4
4
  module ApiClient
5
- VERSION = '0.3.4'
5
+ VERSION = '0.4.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prometheus-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.1
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-08 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: quantile
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.2.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.2.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: faraday
29
15
  requirement: !ruby/object:Gem::Requirement