diff --git a/README.md b/README.md index f6033ce..6435d07 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,8 @@ alerts = prometheus.get() \# get all the alerts with generation_id='12497ca8-b597-4590-ac5d-d55af7f3d185' and index >= 34 alerts = prometheus.get(generation_id: '12497ca8-b597-4590-ac5d-d55af7f3d185', from_index: 34) + +#### Posting alerts +\# post an alert JSON to server + +alerts = prometheus.post('{"alertId":12}') diff --git a/examples/get_alerts.rb b/examples/get_alerts.rb index 5e4cb95..31c1fbd 100644 --- a/examples/get_alerts.rb +++ b/examples/get_alerts.rb @@ -1,7 +1,7 @@ require 'prometheus/alert_buffer_client' # returns a client -prometheus = Prometheus::AlertBufferClient::Client.new(url: 'http://localhost:9099', path: '/topics/zgalor') +prometheus = Prometheus::AlertBufferClient::Client.new(url: 'http://localhost:9099', path: '/topics/test') -alerts = prometheus.get(generation_id: '12497ca8-b597-4590-ac5d-d55af7f3d185', from_index: 5) +alerts = prometheus.get(generation_id: '12497ca8-b597-4590-ac5d-d55af7f3d185', from_index: 1) puts alerts diff --git a/examples/post_alerts.rb b/examples/post_alerts.rb new file mode 100644 index 0000000..fd4a48a --- /dev/null +++ b/examples/post_alerts.rb @@ -0,0 +1,8 @@ +require 'prometheus/alert_buffer_client' + +# returns a client +prometheus = Prometheus::AlertBufferClient::Client.new(url: 'http://localhost:9099', path: '/topics/test') + +prometheus.post '{"posted":"alert"}' +prometheus.post '{"alertId":12}' + diff --git a/lib/prometheus/alert_buffer_client/client.rb b/lib/prometheus/alert_buffer_client/client.rb index 47ca9d7..48b6bda 100644 --- a/lib/prometheus/alert_buffer_client/client.rb +++ b/lib/prometheus/alert_buffer_client/client.rb @@ -59,6 +59,16 @@ def get(options = {}) JSON.parse(response.body)['messages'] end + # post alert: + # @param [String] alert Alert to post + def post(alert) + @client.post do |req| + req.body = alert + end + rescue + raise RequestError, 'Bad response from server' + end + # Helper function to evalueate the low level proxy option def faraday_proxy(options) return options[:proxy] if options[:proxy]