22
33require 'json'
44require 'faraday'
5+ require 'faraday_middleware'
56
67module 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
131131end
0 commit comments