module SocialHubHttpUtility

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/socialHub_httpUtility.rb, line 29
def self.configure
  yield(configuration)
end
get_http_request(domain, path, params, headers, distination_service, trace_id) click to toggle source
# File lib/socialHub_httpUtility.rb, line 33
  def self.get_http_request(domain, path, params, headers, distination_service, trace_id)
    
    if(configuration.service_name)
      puts "service name from the ruby gem is"
      puts configuration.service_name
    end

    url = domain+path
    http_end_index = url.index(':')
    http_type = url[0 , http_end_index]
    uri = URI.parse(url)
    uri.query = URI.encode_www_form( params)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (http_type == 'https')
    request = Net::HTTP::Get.new(uri.request_uri)
    
    headers.each do |key,value|
      request.add_field(key, value)        
    end

    if(!headers.key?("request-id"))
      request.add_field("request-id",SecureRandom.uuid)
    end

    ZipkinTracer::TraceContainer.with_trace_id(trace_id) do
      b3_headers.each do |method, header|
        request.add_field(header, trace_id.send(method).to_s)
      end
    end


    log_params = { 
      "service_name" => params["service_name"],
      "http_url"     => url,
      "http_request_params" => params,
      "http_request_type"   => "GET"
    }

    response = {}
    metadata = {}
    begin
      res = http.request(request)
      case res
        when Net::HTTPSuccess then
          metadata["statusCode"] = 1  
          metadata["reason"] = "SUCCESS"
          response["metadata"] = metadata
          response["http_response"] = res

        when Net::HTTPMethodNotAllowed then 
           metadata["statusCode"] = 0
           metadata["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type "
           response["metadata"] = metadata
           response["http_response"] = {}

        when Net::HTTPNotFound then 
           metadata["statusCode"] = 0
           metadata["reason"] = "THERE IS NO SUCH A METHOD  >> Check That The Server Has This Method"
           response["metadata"] = metadata
           response["http_response"] = {}
        end
      
      begin
        log(log_params, distination_service, metadata)
      rescue 
      end

    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
     Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,Errno::ECONNREFUSED => e
       metadata["statusCode"] = -1
       metadata["reason"] = e.message
       response["metadata"] = metadata
       response["http_response"] = {}
       puts "Error during processing: #{$!}"
       puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
       begin
         log(log_params, distination_service, metadata)
       rescue 
       end
      
    end

    
    return response 
end
post_http_request(domain, path, params, headers, distination_service, trace_id) click to toggle source

End of Zipking header values

# File lib/socialHub_httpUtility.rb, line 131
def self.post_http_request(domain, path, params, headers, distination_service, trace_id)
    
    if(configuration.service_name)
      puts "service name from the ruby gem is"
      puts configuration.service_name
    end

    url = domain+path
    http_end_index = url.index(':')
    http_type = url[0 , http_end_index]
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = (http_type == 'https')
    post_data = URI.encode_www_form(params)
    if(!headers.key?("request-id"))
      headers["request-id"] = SecureRandom.uuid
    end
    
    ZipkinTracer::TraceContainer.with_trace_id(trace_id) do
      b3_headers.each do |method, header|
        headers[header] =  trace_id.send(method).to_s
      end
    end

     log_params = { 
      "service_name" => params["service_name"],
      "http_url"     => url,
      "http_request_params" => params,
      "http_request_type"   => "POST"
    }
    response = {}
    metadata = {}
    begin
      res = http.post(uri.path, post_data, headers)
      case res
        when Net::HTTPSuccess then
          metadata["statusCode"] = 1
          metadata["reason"] = "SUCCESS"
          response["metadata"] = metadata
          response["http_response"] = res

        when Net::HTTPMethodNotAllowed then 
           metadata["statusCode"] = 0
           metadata["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type "
           response["metadata"] = metadata
           response["http_response"] = {}

        when Net::HTTPNotFound then 
           metadata["statusCode"] = 0
           metadata["reason"] = "THERE IS NO SUCH A METHOD  >> Check That The Server Has This Method"
           response["metadata"] = metadata
           response["http_response"] = {}
        end
              
      begin
        log(log_params, distination_service, metadata)
      rescue 
      end      
    
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
      Net::HTTPBadResponse , Net::HTTPHeaderSyntaxError ,Net::ProtocolError,Errno::ECONNREFUSED => e
      
      metadata["statusCode"] = -1
      metadata["reason"] = e.message
      response["metadata"] = metadata
      response["http_response"] = {}
      puts "Error during processing: #{$!}"
      puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
      begin
         log(log_params, distination_service, metadata)
      rescue 
     end  
    end    
   
  return response
end
reset() click to toggle source
# File lib/socialHub_httpUtility.rb, line 25
def self.reset
  @configuration = Configuration.new
end

Public Instance Methods

b3_headers() click to toggle source

Zipkin Related Header Values

# File lib/socialHub_httpUtility.rb, line 120
def b3_headers
      {
        trace_id: 'X-B3-TraceId',
        parent_id: 'X-B3-ParentSpanId',
        span_id: 'X-B3-SpanId',
        sampled: 'X-B3-Sampled',
        flags: 'X-B3-Flags'
      }
end
log(log_params, distination_service, response_meta_data) click to toggle source
# File lib/socialHub_httpUtility.rb, line 208
def log (log_params, distination_service, response_meta_data)
    uri = URI.parse("http://localhost:8186/MonitoringService/storeLog")
    time = Time.now
    log_params["log_time"] =  time.to_s
    log_params["log_time_unix"] = time.to_i   

    if response_meta_data["statusCode"] == 1
      log_params["log_message"] = "Successed to get Http Response from " + distination_service
      log_params["http_response"] = "SUCCESS"
    elsif (response_meta_data["statusCode"] == -1 || response_meta_data["statusCode"] == 0 )
      log_params["log_message"] = "Failed to get Http Response from " + distination_service  
      log_params["http_response"] = "FAILED"                       
      log_params["http_response_reason"] = response_meta_data["reason"]   
    end
    
    http = Net::HTTP.new(uri.host, uri.port)
    post_data = URI.encode_www_form(log_params)
    res = http.post(uri.path, post_data)

end