class Protobuf::Rpc::Stat

Constants

MODES

Attributes

client[RW]
dispatcher[RW]
end_time[RW]
method_name[RW]
mode[RW]
request_size[RW]
response_size[RW]
server[RW]
service[RW]
start_time[RW]

Public Class Methods

new(mode = :SERVER) click to toggle source
# 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

Public Instance Methods

client?() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 86
def client?
  @mode == :CLIENT
end
elapsed_time() click to toggle source
# 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) click to toggle source
# File lib/protobuf/rpc/stat.rb, line 70
def failure(code)
  @failure_code = code
end
rpc() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 78
def rpc
  service && method_name ? "#{service}##{method_name}" : nil
end
server=(peer) click to toggle source
# File lib/protobuf/rpc/stat.rb, line 36
def server=(peer)
  @server = { :port => peer[0], :ip => peer[1] }
end
server?() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 82
def server?
  @mode == :SERVER
end
sizes() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 48
def sizes
  if stopped?
    "#{@request_size}B/#{@response_size}B"
  else
    "#{@request_size}B/-"
  end
end
start() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 56
def start
  @start_time ||= ::Time.now
end
statsd_base_path() click to toggle source

Return base path for StatsD metrics

# File lib/protobuf/rpc/stat.rb, line 107
def statsd_base_path
  "rpc-client.#{service}.#{method_name}".gsub('::', '.').downcase
end
stop() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 60
def stop
  start unless @start_time
  @end_time ||= ::Time.now
  call_statsd_client
end
stopped?() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 74
def stopped?
  !end_time.nil?
end
success() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 66
def success
  @success = true
end
to_s() click to toggle source
# 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_id() click to toggle source
# File lib/protobuf/rpc/stat.rb, line 102
def trace_id
  ::Thread.current.object_id.to_s(16)
end

Private Instance Methods

call_statsd_client() click to toggle source

If a StatsD Client has been configured, send stats to it upon completion.

# File lib/protobuf/rpc/stat.rb, line 113
def call_statsd_client
  path = statsd_base_path
  statsd_client = Protobuf::Statsd.client
  return unless statsd_client

  if @success
    statsd_client.increment("#{path}.success")
  elsif @failure_code
    statsd_client.increment("#{path}.failure.total")
    statsd_client.increment("#{path}.failure.#{@failure_code}")
  end

  if start_time && end_time
    duration = end_time - start_time
    statsd_client.timing("#{path}.time", duration)
  end
end