Class: Protobuf::Rpc::Stat

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

Constant Summary collapse

MODES =
[:SERVER, :CLIENT].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = :SERVER) ⇒ Stat



13
14
15
16
17
18
19
20
# File 'lib/protobuf/rpc/stat.rb', line 13

def initialize(mode = :SERVER)
  @mode = mode
  @request_size = 0
  @response_size = 0
  @success = false
  @failure_code = nil
  start
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#dispatcherObject

Returns the value of attribute dispatcher.



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

def dispatcher
  @dispatcher
end

#end_timeObject

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#method_nameObject

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#request_sizeObject

Returns the value of attribute request_size.



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

def request_size
  @request_size
end

#response_sizeObject

Returns the value of attribute response_size.



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

def response_size
  @response_size
end

#serverObject

Returns the value of attribute server.



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

def server
  @server
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Instance Method Details

#client?Boolean



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

def client?
  @mode == :CLIENT
end

#elapsed_timeObject



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

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

#failure(code) ⇒ Object



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

def failure(code)
  @failure_code = code
end

#rpcObject



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

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

#server?Boolean



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

def server?
  @mode == :SERVER
end

#sizesObject



48
49
50
51
52
53
54
# File 'lib/protobuf/rpc/stat.rb', line 48

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

#startObject



56
57
58
# File 'lib/protobuf/rpc/stat.rb', line 56

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

#statsd_base_pathObject

Return base path for StatsD metrics



107
108
109
# File 'lib/protobuf/rpc/stat.rb', line 107

def statsd_base_path
  "rpc-client.#{service}.#{method_name}".gsub('::', '.').downcase
end

#stopObject



60
61
62
63
64
# File 'lib/protobuf/rpc/stat.rb', line 60

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

#stopped?Boolean



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

def stopped?
  !end_time.nil?
end

#successObject



66
67
68
# File 'lib/protobuf/rpc/stat.rb', line 66

def success
  @success = true
end

#to_sObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/protobuf/rpc/stat.rb', line 90

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

#trace_idObject



102
103
104
# File 'lib/protobuf/rpc/stat.rb', line 102

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