Class: Protobuf::Rpc::Stat
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Stat
- Defined in:
- lib/protobuf/rpc/stat.rb
Constant Summary collapse
- MODES =
[:SERVER, :CLIENT].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#request_size ⇒ Object
Returns the value of attribute request_size.
-
#response_size ⇒ Object
Returns the value of attribute response_size.
-
#server ⇒ Object
Returns the value of attribute server.
-
#service ⇒ Object
Returns the value of attribute service.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
Class Method Summary collapse
-
.statsd_client ⇒ Object
The StatsD Client configured, if any.
-
.statsd_client=(statsd_client) ⇒ Object
Set the StatsD Client to send stats to.
Instance Method Summary collapse
- #client? ⇒ Boolean
- #elapsed_time ⇒ Object
- #failure(code) ⇒ Object
-
#initialize(mode = :SERVER) ⇒ Stat
constructor
A new instance of Stat.
- #rpc ⇒ Object
- #server? ⇒ Boolean
- #sizes ⇒ Object
- #start ⇒ Object
-
#statsd_base_path ⇒ Object
Return base path for StatsD metrics.
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #success ⇒ Object
- #to_s ⇒ Object
- #trace_id ⇒ Object
Constructor Details
#initialize(mode = :SERVER) ⇒ Stat
Returns a new instance of Stat.
25 26 27 28 29 30 31 32 |
# File 'lib/protobuf/rpc/stat.rb', line 25 def initialize(mode = :SERVER) @mode = mode @request_size = 0 @response_size = 0 @success = false @failure_code = nil start end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/protobuf/rpc/stat.rb', line 9 def client @client end |
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
8 9 10 |
# File 'lib/protobuf/rpc/stat.rb', line 8 def dispatcher @dispatcher end |
#end_time ⇒ Object
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_name ⇒ Object
Returns the value of attribute method_name.
9 10 11 |
# File 'lib/protobuf/rpc/stat.rb', line 9 def method_name @method_name end |
#mode ⇒ Object
Returns the value of attribute mode.
8 9 10 |
# File 'lib/protobuf/rpc/stat.rb', line 8 def mode @mode end |
#request_size ⇒ Object
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_size ⇒ Object
Returns the value of attribute response_size.
9 10 11 |
# File 'lib/protobuf/rpc/stat.rb', line 9 def response_size @response_size end |
#server ⇒ Object
Returns the value of attribute server.
9 10 11 |
# File 'lib/protobuf/rpc/stat.rb', line 9 def server @server end |
#service ⇒ Object
Returns the value of attribute service.
9 10 11 |
# File 'lib/protobuf/rpc/stat.rb', line 9 def service @service end |
#start_time ⇒ Object
Returns the value of attribute start_time.
8 9 10 |
# File 'lib/protobuf/rpc/stat.rb', line 8 def start_time @start_time end |
Class Method Details
.statsd_client ⇒ Object
The StatsD Client configured, if any.
21 22 23 |
# File 'lib/protobuf/rpc/stat.rb', line 21 def self.statsd_client @statsd_client end |
.statsd_client=(statsd_client) ⇒ Object
Set the StatsD Client to send stats to. The client must match the interface provided by lookout-statsd (https://github.com/lookout/statsd).
16 17 18 |
# File 'lib/protobuf/rpc/stat.rb', line 16 def self.statsd_client=(statsd_client) @statsd_client = statsd_client end |
Instance Method Details
#client? ⇒ Boolean
100 101 102 |
# File 'lib/protobuf/rpc/stat.rb', line 100 def client? @mode == :CLIENT end |
#elapsed_time ⇒ Object
42 43 44 |
# File 'lib/protobuf/rpc/stat.rb', line 42 def elapsed_time (start_time && end_time ? "#{(end_time - start_time).round(4)}s" : nil) end |
#failure(code) ⇒ Object
84 85 86 |
# File 'lib/protobuf/rpc/stat.rb', line 84 def failure(code) @failure_code = code end |
#rpc ⇒ Object
92 93 94 |
# File 'lib/protobuf/rpc/stat.rb', line 92 def rpc service && method_name ? "#{service}##{method_name}" : nil end |
#server? ⇒ Boolean
96 97 98 |
# File 'lib/protobuf/rpc/stat.rb', line 96 def server? @mode == :SERVER end |
#sizes ⇒ Object
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 |
#start ⇒ Object
70 71 72 |
# File 'lib/protobuf/rpc/stat.rb', line 70 def start @start_time ||= ::Time.now end |
#statsd_base_path ⇒ Object
Return base path for StatsD metrics
121 122 123 |
# File 'lib/protobuf/rpc/stat.rb', line 121 def statsd_base_path "rpc.#{service}.#{method_name}".gsub('::', '.').downcase end |
#stop ⇒ Object
74 75 76 77 78 |
# File 'lib/protobuf/rpc/stat.rb', line 74 def stop start unless @start_time @end_time ||= ::Time.now call_statsd_client end |
#stopped? ⇒ Boolean
88 89 90 |
# File 'lib/protobuf/rpc/stat.rb', line 88 def stopped? ! end_time.nil? end |
#success ⇒ Object
80 81 82 |
# File 'lib/protobuf/rpc/stat.rb', line 80 def success @success = true end |
#to_s ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/protobuf/rpc/stat.rb', line 104 def to_s [ server? ? "[SRV]" : "[CLT]", server? ? client : server, trace_id, rpc, sizes, elapsed_time, @end_time.try(:iso8601) ].compact.join(' - ') end |
#trace_id ⇒ Object
116 117 118 |
# File 'lib/protobuf/rpc/stat.rb', line 116 def trace_id ::Thread.current.object_id.to_s(16) end |