Class: Protobuf::Rpc::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/rpc/stat.rb

Constant Summary collapse

MODES =
[:SERVER, :CLIENT].freeze
ERROR_TRANSLATIONS =
{
  ::Protobuf::Socketrpc::ErrorReason::BAD_REQUEST_DATA => "BAD_REQUEST_DATA",
  ::Protobuf::Socketrpc::ErrorReason::BAD_REQUEST_PROTO => "BAD_REQUEST_PROTO",
  ::Protobuf::Socketrpc::ErrorReason::SERVICE_NOT_FOUND => "SERVICE_NOT_FOUND",
  ::Protobuf::Socketrpc::ErrorReason::METHOD_NOT_FOUND => "METHOD_NOT_FOUND",
  ::Protobuf::Socketrpc::ErrorReason::RPC_ERROR => "RPC_ERROR",
  ::Protobuf::Socketrpc::ErrorReason::RPC_FAILED => "RPC_FAILED",
  ::Protobuf::Socketrpc::ErrorReason::INVALID_REQUEST_PROTO => "INVALID_REQUEST_PROTO",
  ::Protobuf::Socketrpc::ErrorReason::BAD_RESPONSE_PROTO => "BAD_RESPONSE_PROTO",
  ::Protobuf::Socketrpc::ErrorReason::UNKNOWN_HOST => "UNKNOWN_HOST",
  ::Protobuf::Socketrpc::ErrorReason::IO_ERROR => "IO_ERROR",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :SERVER) ⇒ Stat

Returns a new instance of Stat.


28
29
30
31
32
33
# File 'lib/protobuf/rpc/stat.rb', line 28

def initialize(mode = :SERVER)
  @mode = mode
  @request_size = 0
  @response_size = 0
  start
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.


10
11
12
# File 'lib/protobuf/rpc/stat.rb', line 10

def client
  @client
end

#dispatcherObject

Returns the value of attribute dispatcher.


9
10
11
# File 'lib/protobuf/rpc/stat.rb', line 9

def dispatcher
  @dispatcher
end

#end_timeObject

Returns the value of attribute end_time.


9
10
11
# File 'lib/protobuf/rpc/stat.rb', line 9

def end_time
  @end_time
end

#method_nameObject

Returns the value of attribute method_name.


10
11
12
# File 'lib/protobuf/rpc/stat.rb', line 10

def method_name
  @method_name
end

#modeObject

Returns the value of attribute mode.


9
10
11
# File 'lib/protobuf/rpc/stat.rb', line 9

def mode
  @mode
end

#request_sizeObject

Returns the value of attribute request_size.


9
10
11
# File 'lib/protobuf/rpc/stat.rb', line 9

def request_size
  @request_size
end

#response_sizeObject

Returns the value of attribute response_size.


10
11
12
# File 'lib/protobuf/rpc/stat.rb', line 10

def response_size
  @response_size
end

#serverObject

Returns the value of attribute server.


11
12
13
# File 'lib/protobuf/rpc/stat.rb', line 11

def server
  @server
end

#serviceObject

Returns the value of attribute service.


10
11
12
# File 'lib/protobuf/rpc/stat.rb', line 10

def service
  @service
end

#start_timeObject

Returns the value of attribute start_time.


9
10
11
# File 'lib/protobuf/rpc/stat.rb', line 9

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.


10
11
12
# File 'lib/protobuf/rpc/stat.rb', line 10

def status
  @status
end

Instance Method Details

#client?Boolean

Returns:

  • (Boolean)

91
92
93
# File 'lib/protobuf/rpc/stat.rb', line 91

def client?
  @mode == :CLIENT
end

#elapsed_timeObject


41
42
43
# File 'lib/protobuf/rpc/stat.rb', line 41

def elapsed_time
  (start_time && end_time ? "#{(end_time - start_time).round(4)}s" : nil)
end

#rpcObject


83
84
85
# File 'lib/protobuf/rpc/stat.rb', line 83

def rpc
  service && method_name ? "#{service}##{method_name}" : nil
end

#server?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/protobuf/rpc/stat.rb', line 87

def server?
  @mode == :SERVER
end

#sizesObject


62
63
64
65
66
67
68
# File 'lib/protobuf/rpc/stat.rb', line 62

def sizes
  if stopped?
    "#{@request_size}B/#{@response_size}B"
  else
    "#{@request_size}B/-"
  end
end

#startObject


70
71
72
# File 'lib/protobuf/rpc/stat.rb', line 70

def start
  @start_time ||= ::Time.now
end

#status_stringObject


95
96
97
98
99
# File 'lib/protobuf/rpc/stat.rb', line 95

def status_string
  return "OK" if status.nil?

  ERROR_TRANSLATIONS.fetch(status, "UNKNOWN_ERROR")
end

#stopObject


74
75
76
77
# File 'lib/protobuf/rpc/stat.rb', line 74

def stop
  start unless @start_time
  @end_time ||= ::Time.now
end

#stopped?Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/protobuf/rpc/stat.rb', line 79

def stopped?
  !end_time.nil?
end

#to_sObject


101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/protobuf/rpc/stat.rb', line 101

def to_s
  [
    server? ? "[SRV]" : "[CLT]",
    server? ? client : server,
    trace_id,
    rpc,
    sizes,
    elapsed_time,
    status_string,
    @end_time.try(:iso8601),
  ].compact.join(' - ')
end

#trace_idObject


114
115
116
# File 'lib/protobuf/rpc/stat.rb', line 114

def trace_id
  ::Thread.current.object_id.to_s(16)
end