Module: SocialHubHttpUtility
- Defined in:
- lib/socialHub_httpUtility.rb,
lib/socialHub_httpUtility/version.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"1.1.0"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
- .configure {|configuration| ... } ⇒ Object
- .get_http_request(domain, path, params, headers, distination_service, trace_id) ⇒ Object
-
.post_http_request(domain, path, params, headers, distination_service, trace_id) ⇒ Object
End of Zipking header values.
- .reset ⇒ Object
Instance Method Summary collapse
-
#b3_headers ⇒ Object
Zipkin Related Header Values.
- #log(log_params, distination_service, response_meta_data) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
18 19 20 |
# File 'lib/socialHub_httpUtility.rb', line 18 def configuration @configuration end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
29 30 31 |
# File 'lib/socialHub_httpUtility.rb', line 29 def self.configure yield(configuration) end |
.get_http_request(domain, path, params, headers, distination_service, trace_id) ⇒ Object
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 |
# 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 = {} = {} begin res = http.request(request) case res when Net::HTTPSuccess then ["statusCode"] = 1 ["reason"] = "SUCCESS" response["metadata"] = response["http_response"] = res when Net::HTTPMethodNotAllowed then ["statusCode"] = 0 ["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type " response["metadata"] = response["http_response"] = {} when Net::HTTPNotFound then ["statusCode"] = 0 ["reason"] = "THERE IS NO SUCH A METHOD >> Check That The Server Has This Method" response["metadata"] = response["http_response"] = {} end begin log(log_params, distination_service, ) rescue end rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,Errno::ECONNREFUSED => e ["statusCode"] = -1 ["reason"] = e. response["metadata"] = response["http_response"] = {} puts "Error during processing: #{$!}" puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}" begin log(log_params, distination_service, ) rescue end end return response end |
.post_http_request(domain, path, params, headers, distination_service, trace_id) ⇒ Object
End of Zipking header values
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# 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 = {} = {} begin res = http.post(uri.path, post_data, headers) case res when Net::HTTPSuccess then ["statusCode"] = 1 ["reason"] = "SUCCESS" response["metadata"] = response["http_response"] = res when Net::HTTPMethodNotAllowed then ["statusCode"] = 0 ["reason"] = "Method NOT ALLOWED FOR THIS URL >> Check The Method Type " response["metadata"] = response["http_response"] = {} when Net::HTTPNotFound then ["statusCode"] = 0 ["reason"] = "THERE IS NO SUCH A METHOD >> Check That The Server Has This Method" response["metadata"] = response["http_response"] = {} end begin log(log_params, distination_service, ) rescue end rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse , Net::HTTPHeaderSyntaxError ,Net::ProtocolError,Errno::ECONNREFUSED => e ["statusCode"] = -1 ["reason"] = e. response["metadata"] = response["http_response"] = {} puts "Error during processing: #{$!}" puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}" begin log(log_params, distination_service, ) rescue end end return response end |
.reset ⇒ Object
25 26 27 |
# File 'lib/socialHub_httpUtility.rb', line 25 def self.reset @configuration = Configuration.new end |
Instance Method Details
#b3_headers ⇒ Object
Zipkin Related Header Values
120 121 122 123 124 125 126 127 128 |
# 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) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/socialHub_httpUtility.rb', line 208 def log (log_params, distination_service, ) 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 ["statusCode"] == 1 log_params["log_message"] = "Successed to get Http Response from " + distination_service log_params["http_response"] = "SUCCESS" elsif (["statusCode"] == -1 || ["statusCode"] == 0 ) log_params["log_message"] = "Failed to get Http Response from " + distination_service log_params["http_response"] = "FAILED" log_params["http_response_reason"] = ["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 |