class Coralogix::CoralogixHTTPSender

@private

Constants

TICKS_IN_SECOND

Public Class Methods

new(proxy={}) click to toggle source
# 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_NONE
        @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.message  
        DebugLogger.error e.backtrace.inspect  
    end
end

Public Instance Methods

disable_proxy(value) click to toggle source
# File lib/httpsender.rb, line 16
def disable_proxy value
    @disable_proxy = value
end
disable_proxy=(value) click to toggle source
# File lib/httpsender.rb, line 20
def disable_proxy=(value)
    @disable_proxy = value
end
force_compression=(value) click to toggle source
# File lib/httpsender.rb, line 24
def force_compression=(value)
    @force_compression = value
end
get_time_sync() click to toggle source

A helper method to get coralogix server current time and calculate the time difference

@return [float] - time delta

# 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.message  
            DebugLogger.error e.backtrace.inspect  
            return false, 0
        end
    end
end
json2zip(json) click to toggle source

Compress json @param json - json to compress

@return [String] return base 64 string that represents the compresed log message binary data

# 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.message
        DebugLogger.error e.backtrace.inspect
        return nil
    end
end
send_request(bulk) click to toggle source

A helper method to post http request

@param bulk - JSON bulk containing the log entries

# 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.message
                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