mackerel-client 0.0.3 → 0.0.5

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: 91ab71f2c21720288ec7c6c1d44079f4d98326d5
4
- data.tar.gz: 3e6e4f6bc08ad6c72949538141eacf94d16407d6
3
+ metadata.gz: 50fe10ec4bbe6d4c05ba4ded9dc8dc3890986d62
4
+ data.tar.gz: abce163f8af5de40bb2dddb16ac1a107f611e5d1
5
5
  SHA512:
6
- metadata.gz: cd2297ae218d4c491c51c0a82e5985aa7f7674b0bed2d22ed0246f919155d0e96a8a6fca22b31fc4f81bf49be10057c992988e27543521a1a765f2e874e0292c
7
- data.tar.gz: 98363c727e49f46c2250dc30ab730fb53d852eb150c5342cf590697cf91709f4daab37360fa0427cf2e8f725ebfe5db5b2e6878c7ad6adf5d2c6f7e589d5e926
6
+ metadata.gz: 3f15c1177dd45f34b7a0dae2ace107a92505732557f31310a6da47965bd47182b1cd2612e2f930c6d7579c5439a2c25d7b39e78f72b0789d60035c458822b22b
7
+ data.tar.gz: a43f37ae3bc1777d55aa4fa544bff81646bdb865291376062e8d669dc2914c5cefa87e79e9ebfca524bd2bc8e9cccd1aaa6859166c13fc27a7972e2d665f1c52
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
@@ -8,9 +8,11 @@ module Mackerel
8
8
 
9
9
  class Client
10
10
 
11
+ ERROR_MESSAGE_FOR_API_KEY_ABSENCE = "API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY."
12
+
11
13
  def initialize(args = {})
12
14
  @origin = args[:mackerel_origin] || 'https://mackerel.io'
13
- @api_key = args[:mackerel_api_key]
15
+ @api_key = args[:mackerel_api_key] || raise(ERROR_MESSAGE_FOR_API_KEY_ABSENCE)
14
16
  end
15
17
 
16
18
  def get_host(host_id)
@@ -80,6 +82,23 @@ module Mackerel
80
82
  data = JSON.parse(response.body)
81
83
  end
82
84
 
85
+ def get_latest_metrics(hostIds, names)
86
+ query = (hostIds.map{ |hostId| "hostId=#{hostId}" } +
87
+ names.map{ |name| "name=#{name}" }).join('&')
88
+
89
+ client = http_client
90
+ response = client.get "/api/v0/tsdb/latest?#{query}" do |req|
91
+ req.headers['X-Api-Key'] = @api_key
92
+ end
93
+
94
+ unless response.success?
95
+ raise "/api/v0/tsdb/latest?#{query} faild: #{response.status}"
96
+ end
97
+
98
+ data = JSON.parse(response.body)
99
+ data["tsdbLatest"]
100
+ end
101
+
83
102
  def post_service_metrics(service_name, metrics)
84
103
  client = http_client
85
104
 
@@ -6,6 +6,13 @@ describe Mackerel::Client do
6
6
  let(:api_key) { 'xxxxxxxx' }
7
7
  let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
8
 
9
+ describe 'initialization' do
10
+ it 'display an error message when api_key is absent' do
11
+ expected_message = "API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY."
12
+ expect { Mackerel::Client.new() }.to raise_error(expected_message)
13
+ end
14
+ end
15
+
9
16
  describe '#get_host' do
10
17
  let(:stubbed_response) {
11
18
  [
@@ -168,6 +175,48 @@ describe Mackerel::Client do
168
175
  end
169
176
  end
170
177
 
178
+ describe "#get_latest_metrics" do
179
+ let(:stubbed_response) {
180
+ [
181
+ 200,
182
+ {},
183
+ JSON.dump(response_object)
184
+ ]
185
+ }
186
+
187
+ let(:test_client) {
188
+ Faraday.new do |builder|
189
+ builder.adapter :test do |stubs|
190
+ stubs.get(api_path) { stubbed_response }
191
+ end
192
+ end
193
+ }
194
+
195
+ let(:hostId) { '21obeF4PhZN' }
196
+
197
+ let(:metric_name) { "loadavg5" }
198
+
199
+ let(:api_path) { "/api/v0/tsdb/latest" }
200
+
201
+ let(:response_object) {
202
+ {
203
+ "tsdbLatest" => {
204
+ hostId => {
205
+ metric_name => {"time"=>1407898200, "value"=>0.03666666666666667},
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ before do
212
+ allow(client).to receive(:http_client).and_return(test_client)
213
+ end
214
+
215
+ it "successfully post metrics" do
216
+ expect(client.get_latest_metrics([hostId], [metric_name])).to eq(response_object["tsdbLatest"])
217
+ end
218
+ end
219
+
171
220
  describe '#post_service_metrics' do
172
221
  let(:stubbed_response) {
173
222
  [
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mackerel-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mackerel developer team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-03 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.9'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.9'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2'
69
69
  description: Mackerel client is a library to access Mackerel (https://mackerel.io/).
@@ -75,9 +75,9 @@ executables:
75
75
  extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
- - ".gitignore"
79
- - ".rspec"
80
- - ".travis.yml"
78
+ - .gitignore
79
+ - .rspec
80
+ - .travis.yml
81
81
  - Gemfile
82
82
  - LICENSE.txt
83
83
  - README.md
@@ -101,17 +101,17 @@ require_paths:
101
101
  - lib
102
102
  required_ruby_version: !ruby/object:Gem::Requirement
103
103
  requirements:
104
- - - ">="
104
+ - - '>='
105
105
  - !ruby/object:Gem::Version
106
106
  version: '0'
107
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ">="
109
+ - - '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 2.2.2
114
+ rubygems_version: 2.0.14
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: Mackerel client implemented by Ruby.