Class: ThriftRack::Client
- Inherits:
-
Object
- Object
- ThriftRack::Client
- Defined in:
- lib/thrift_rack/client.rb
Constant Summary collapse
- DEFAULT_REQUEST_ID =
"no-request".freeze
Class Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(url, client_klass, request = nil) ⇒ Client
constructor
A new instance of Client.
- #method_missing(method, *params) ⇒ Object
- #protocol_factory ⇒ Object
- #respond_to_missing?(method, _include_private = false) ⇒ Boolean
Constructor Details
#initialize(url, client_klass, request = nil) ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/thrift_rack/client.rb', line 5 def initialize(url, client_klass, request = nil) if request.is_a?(ActionDispatch::Request) @request = request @request_id = request.request_id else @request_id = request || DEFAULT_REQUEST_ID end @url = url @transport = ThriftRack::HttpClientTransport.new(url) protocol = protocol_factory.get_protocol(@transport) @client = client_klass.new(protocol) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *params) ⇒ Object
26 27 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 65 66 67 68 69 70 |
# File 'lib/thrift_rack/client.rb', line 26 def method_missing(method, *params) return super unless @client.respond_to?(method) self.class_eval do define_method method.to_sym do |*args| begin rpc_id = SecureRandom.uuid request_at = Time.now if Thread.current["RPC_FULL_TRACE"].to_s == "true" full_trace = true else full_trace = @request_id == DEFAULT_REQUEST_ID || @request_id.hash % 8 == 0 end @transport.add_headers("X-Request-ID" => @request_id, "X-Rpc-ID" => rpc_id, "X-Rpc-Func" => method.to_s, "X-From" => ThriftRack::Client.app_name || "unknown", "X-Full-Trace" => full_trace.to_s) @client.send(method, *args) ensure end_time = Time.now duration = (end_time - request_at) * 1000 process_duration = @transport.response_headers["x-server-process-duration"]&.to_f if full_trace || duration >= 100 ThriftRack::Client.logger.info( JSON.dump( request_at: request_at.iso8601(6), request_id: @request_id, rpc_id: rpc_id, duration: duration.round(4), path: URI(@url).path, func: method, tag: ThriftRack::Client.logger_tag, full_trace: full_trace, extra_context: @request ? { context: "action_controller", controller: @request.params["controller"], action: @request.params["action"] } : {}, server: { id: @transport.response_headers["x-server-id"], private_ip: @transport.response_headers["x-server-private-ip"], process_duration: process_duration ? process_duration.round(4) : nil, network_duration: process_duration ? (duration - process_duration).round(4) : nil, }, ), ) end end end end self.public_send(method, *params) end |
Class Attribute Details
.app_name ⇒ Object
75 76 77 78 |
# File 'lib/thrift_rack/client.rb', line 75 def app_name @app_name ||= Rails.application.class.parent.name.underscore if defined? Rails @app_name end |
.logger_tag ⇒ Object
80 81 82 |
# File 'lib/thrift_rack/client.rb', line 80 def logger_tag @logger_tag || {} end |
Class Method Details
.config(app_name, max_requests: 100, logger_tag: {}) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/thrift_rack/client.rb', line 92 def config(app_name, max_requests: 100, logger_tag: {}) self.app_name = app_name self.logger_tag = logger_tag HttpClientTransport.default = HttpClientTransport.new_http(app_name, max_requests: max_requests) at_exit do ThriftRack::Client.logger.close end end |
.logger ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/thrift_rack/client.rb', line 84 def logger @logger ||= if defined? Rails ActiveSupport::Logger.new(File.open("#{Rails.root}/log/rpc_client.log", File::WRONLY | File::APPEND | File::CREAT)) else ::Logger.new(STDOUT) end end |
Instance Method Details
#protocol_factory ⇒ Object
18 19 20 |
# File 'lib/thrift_rack/client.rb', line 18 def protocol_factory Thrift::CompactProtocolFactory.new end |
#respond_to_missing?(method, _include_private = false) ⇒ Boolean
22 23 24 |
# File 'lib/thrift_rack/client.rb', line 22 def respond_to_missing?(method, _include_private = false) @client.respond_to?(method) end |