11require 'date'
22require 'time'
33require 'protobuf/logging'
4+ require 'protobuf/rpc/rpc.pb'
45
56module Protobuf
67 module Rpc
78 class Stat
89 attr_accessor :mode , :start_time , :end_time , :request_size , :dispatcher
9- attr_accessor :response_size , :client , :service , :method_name
10+ attr_accessor :response_size , :client , :service , :method_name , :status
1011 attr_reader :server
1112
1213 MODES = [ :SERVER , :CLIENT ] . freeze
1314
15+ ERROR_TRANSLATIONS = {
16+ ::Protobuf ::Socketrpc ::ErrorReason ::BAD_REQUEST_DATA => "BAD_REQUEST_DATA" ,
17+ ::Protobuf ::Socketrpc ::ErrorReason ::BAD_REQUEST_PROTO => "BAD_REQUEST_PROTO" ,
18+ ::Protobuf ::Socketrpc ::ErrorReason ::SERVICE_NOT_FOUND => "SERVICE_NOT_FOUND" ,
19+ ::Protobuf ::Socketrpc ::ErrorReason ::METHOD_NOT_FOUND => "METHOD_NOT_FOUND" ,
20+ ::Protobuf ::Socketrpc ::ErrorReason ::RPC_ERROR => "RPC_ERROR" ,
21+ ::Protobuf ::Socketrpc ::ErrorReason ::RPC_FAILED => "RPC_FAILED" ,
22+ ::Protobuf ::Socketrpc ::ErrorReason ::INVALID_REQUEST_PROTO => "INVALID_REQUEST_PROTO" ,
23+ ::Protobuf ::Socketrpc ::ErrorReason ::BAD_RESPONSE_PROTO => "BAD_RESPONSE_PROTO" ,
24+ ::Protobuf ::Socketrpc ::ErrorReason ::UNKNOWN_HOST => "UNKNOWN_HOST" ,
25+ ::Protobuf ::Socketrpc ::ErrorReason ::IO_ERROR => "IO_ERROR" ,
26+ } . freeze
27+
1428 def initialize ( mode = :SERVER )
1529 @mode = mode
1630 @request_size = 0
@@ -78,6 +92,12 @@ def client?
7892 @mode == :CLIENT
7993 end
8094
95+ def status_string
96+ return "OK" if status . nil?
97+
98+ ERROR_TRANSLATIONS . fetch ( status , "UNKNOWN_ERROR" )
99+ end
100+
81101 def to_s
82102 [
83103 server? ? "[SRV]" : "[CLT]" ,
@@ -86,14 +106,14 @@ def to_s
86106 rpc ,
87107 sizes ,
88108 elapsed_time ,
109+ status_string ,
89110 @end_time . try ( :iso8601 ) ,
90111 ] . compact . join ( ' - ' )
91112 end
92113
93114 def trace_id
94115 ::Thread . current . object_id . to_s ( 16 )
95116 end
96-
97117 end
98118 end
99119end
0 commit comments