Skip to content
This repository was archived by the owner on Sep 29, 2021. It is now read-only.

Commit adf9725

Browse files
authored
Merge pull request #2 from zgalor/return_gen_id
Change .get() return value to entire http response body
2 parents 080e6b2 + c9ff867 commit adf9725

File tree

8 files changed

+19
-15
lines changed

8 files changed

+19
-15
lines changed

.rubocop.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AllCops:
22
Exclude:
3-
- lib/prometheus/api_client/version.rb
3+
- lib/prometheus/alert_buffer_client/version.rb
44

55
AlignHash:
66
EnforcedHashRocketStyle: table
@@ -22,6 +22,7 @@ Metrics/BlockLength:
2222
- 'spec/**/*.rb'
2323

2424
Metrics/LineLength:
25+
Max: 120
2526
Exclude:
2627
- 'spec/**/*.rb'
2728

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def ruby_version?(constraint)
77
end
88

99
gem 'faraday'
10+
gem 'faraday_middleware'
1011

1112
group :test do
1213
gem 'coveralls'

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ alerts = prometheus.get()
3737

3838
alerts = prometheus.get(generation_id: '12497ca8-b597-4590-ac5d-d55af7f3d185', from_index: 34)
3939

40+
Alerts will be returned in a Hash with following keys:
41+
* generationID
42+
* messages
43+
4044
#### Posting alerts
4145
\# post an alert JSON to server
4246

examples/get_alerts.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
prometheus = Prometheus::AlertBufferClient::Client.new(url: 'http://localhost:9099', path: '/topics/test')
55

66
alerts = prometheus.get(generation_id: '12497ca8-b597-4590-ac5d-d55af7f3d185', from_index: 1)
7-
puts alerts
7+
8+
puts "Generation Id: #{alerts['generationID']}"
9+
puts "Alerts: #{alerts['messages']}"

examples/post_alerts.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66
prometheus.post '{"posted":"alert"}'
77
prometheus.post '{"alertId":12}'
8-

lib/prometheus/alert_buffer_client.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
require 'prometheus/alert_buffer_client/client'
66

77
module Prometheus
8-
98
# Alert Client is a ruby implementation for a Prometheus-alert-buffer client.
109
module AlertBufferClient
11-
1210
def self.client(options = {})
1311
Client.new(options)
1412
end
15-
1613
end
1714
end

lib/prometheus/alert_buffer_client/client.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'json'
44
require 'faraday'
5+
require 'faraday_middleware'
56

67
module Prometheus
78
# Alert Client is a ruby implementation for a Prometheus-alert-buffer client.
@@ -20,7 +21,6 @@ class RequestError < StandardError; end
2021
},
2122
}.freeze
2223

23-
2424
# Create a Prometheus Alert client:
2525
#
2626
# @param [Hash] options
@@ -39,24 +39,27 @@ def initialize(options = {})
3939

4040
@client = Faraday.new(
4141
faraday_options(options),
42-
)
42+
) do |conn|
43+
conn.response(:json)
44+
conn.adapter(Faraday.default_adapter)
45+
end
4346
end
4447

45-
4648
# Get alerts:
4749
#
4850
# @param [Hash] options
4951
# @option options [String] :generation_id Database generation Id.
5052
# @option options [Integer] :from_index Minimal index of alerts to fetch.
5153
#
54+
# @return [Hash] response with keys: generationID, messages
5255
# All alerts will be fetched if options are omitted.
5356
def get(options = {})
5457
response = @client.get do |req|
5558
req.params['generationID'] = options[:generation_id]
5659
req.params['fromIndex'] = options[:from_index]
5760
end
5861

59-
JSON.parse(response.body)['messages']
62+
response.body
6063
end
6164

6265
# post alert:
@@ -65,8 +68,6 @@ def post(alert)
6568
@client.post do |req|
6669
req.body = alert
6770
end
68-
rescue
69-
raise RequestError, 'Bad response from server'
7071
end
7172

7273
# Helper function to evalueate the low level proxy option
@@ -98,7 +99,7 @@ def faraday_headers(options)
9899
return unless headers && headers[:token]
99100

100101
{
101-
Authorization: "Bearer #{headers[:token].to_s}",
102+
Authorization: "Bearer #{headers[:token]}",
102103
}
103104
end
104105

@@ -125,7 +126,6 @@ def faraday_options(options)
125126
request: faraday_request(options),
126127
}
127128
end
128-
129129
end
130130
end
131131
end

lib/prometheus/alert_buffer_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 AlertBufferClient
5-
VERSION = '0.1.0'
5+
VERSION = '0.2.0'
66
end
77
end

0 commit comments

Comments
 (0)