Class: Coralogix::CoralogixHTTPSender
- Inherits:
-
Object
- Object
- Coralogix::CoralogixHTTPSender
- Defined in:
- lib/httpsender.rb
Constant Summary collapse
- TICKS_IN_SECOND =
10**7
Instance Method Summary collapse
- #disable_proxy(value) ⇒ Object
- #disable_proxy=(value) ⇒ Object
- #force_compression=(value) ⇒ Object
-
#get_time_sync ⇒ float
A helper method to get coralogix server current time and calculate the time difference.
-
#initialize(proxy = {}) ⇒ CoralogixHTTPSender
constructor
A new instance of CoralogixHTTPSender.
-
#json2zip(json) ⇒ String
Compress json.
-
#send_request(bulk) ⇒ Object
A helper method to post http request.
Constructor Details
#initialize(proxy = {}) ⇒ CoralogixHTTPSender
Returns a new instance of CoralogixHTTPSender.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/httpsender.rb', line 28 def initialize proxy={} begin @initialized = false @mutex = Mutex.new @uri = URI(CORALOGIX_LOG_URL) if(@disable_proxy) @http = Net::HTTP.new(@uri.host, @uri.port, p_addr=nil, p_port=nil) else if proxy.empty? or proxy.fetch("host", nil).nil? or proxy.fetch("port", nil).nil? @http = Net::HTTP.new(@uri.host, @uri.port) else @http = Net::HTTP.new( @uri.host, @uri.port, p_addr=proxy["host"], p_port=proxy["port"], p_user=proxy.fetch("user", nil), p_pass=proxy.fetch("password", nil) ) end end @http.use_ssl = true #@http.keep_alive_timeout = 10 @http.verify_mode = OpenSSL::SSL::VERIFY_PEER @http.read_timeout = HTTP_TIMEOUT # seconds @http.open_timeout = HTTP_TIMEOUT # seconds headers = { 'Content-Type' => 'application/json', 'Content-Encoding' => 'deflate' } @req = Net::HTTP::Post.new(@uri.path, headers) rescue Exception => e DebugLogger.error e. DebugLogger.error e.backtrace.inspect end end |
Instance Method Details
#disable_proxy(value) ⇒ Object
16 17 18 |
# File 'lib/httpsender.rb', line 16 def disable_proxy value @disable_proxy = value end |
#disable_proxy=(value) ⇒ Object
20 21 22 |
# File 'lib/httpsender.rb', line 20 def disable_proxy=(value) @disable_proxy = value end |
#force_compression=(value) ⇒ Object
24 25 26 |
# File 'lib/httpsender.rb', line 24 def force_compression=(value) @force_compression = value end |
#get_time_sync ⇒ float
A helper method to get coralogix server current time and calculate the time difference
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/httpsender.rb', line 127 def get_time_sync @mutex.synchronize do begin DebugLogger.trace "Syncing time with coralogix server" res = @http.get(CORALOGIX_TIME_DELTA_URL) if res.is_a?(Net::HTTPSuccess) && !res.body.to_s.empty? #Get server ticks from 1970 server_ticks = res.body.to_i.to_s # Relative to 1970 #Take the first 13 digits server_ticks = server_ticks[0..12] #Convert the ticks to utc time server_time = Time.parse(Time.at(server_ticks.to_i / 1000.to_f).strftime('%H:%M:%S.%L')).utc local_time = Time.now.utc time_delta = (server_time - local_time) * 1000.0 DebugLogger.trace "Updating time delta to: #{time_delta}" return true, time_delta end return false, 0 rescue Exception => e DebugLogger.error e. DebugLogger.error e.backtrace.inspect return false, 0 end end end |
#json2zip(json) ⇒ String
Compress json
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/httpsender.rb', line 70 def json2zip(json) begin compressed_data = Zlib::Deflate.deflate(json) return compressed_data rescue Exception => e DebugLogger.error e. DebugLogger.error e.backtrace.inspect return nil end end |
#send_request(bulk) ⇒ Object
A helper method to post http request
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/httpsender.rb', line 84 def send_request bulk @mutex.synchronize do attempt = 0 while attempt < HTTP_SEND_RETRY_COUNT begin DebugLogger.debug "About to send bulk to Coralogix server. Attempt number: #{attempt+1}" DebugLogger.trace "Compressing bulk..." zippedBulk = json2zip(bulk.to_json) if zippedBulk.nil? && !@force_compression DebugLogger.debug "Failed to compress bulk, sending raw data instead" @req = Net::HTTP::Post.new(@uri.path, 'Content-Type' => 'application/json') @req.body = bulk.to_json else if zippedBulk.nil? DebugLogger.error "Failed to compress bulk" DebugLogger.error "Compressed bulk is nil" end DebugLogger.trace "Sending compressed bulk" @req.body = zippedBulk end DebugLogger.trace Benchmark.measure { res = @http.request(@req) DebugLogger.debug "Successfully sent bulk to Coralogix server. Result is: #{res.code}" }.to_s return true rescue Exception => e DebugLogger.error e. DebugLogger.error e.backtrace.inspect end attempt+=1; DebugLogger.error "Failed to send bulk. Will retry in: #{HTTP_SEND_RETRY_INTERVAL} seconds..." sleep HTTP_SEND_RETRY_INTERVAL end end end |