class ThriftRack::Client

Constants

DEFAULT_REQUEST_ID

Attributes

app_name[W]
logger_tag[W]

Public Class Methods

app_name() click to toggle source
# 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
config(app_name, max_requests: 100, logger_tag: {}) click to toggle source
# 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() click to toggle source
# 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
logger_tag() click to toggle source
# File lib/thrift_rack/client.rb, line 80
def logger_tag
  @logger_tag || {}
end
new(url, client_klass, request = nil) click to toggle source
# 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

Public Instance Methods

method_missing(method, *params) click to toggle source
Calls superclass method
# 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
protocol_factory() click to toggle source
# File lib/thrift_rack/client.rb, line 18
def protocol_factory
  Thrift::CompactProtocolFactory.new
end
respond_to_missing?(method, _include_private = false) click to toggle source
# File lib/thrift_rack/client.rb, line 22
def respond_to_missing?(method, _include_private = false)
  @client.respond_to?(method)
end