Sha256: e000e946d87f2ae4cf2275a16f4730414208834e6e5827b29ea25043d00c4721

Contents?: true

Size: 1.85 KB

Versions: 29

Compression:

Stored size: 1.85 KB

Contents

require 'date'
require 'time'
require 'protobuf/logging'

module Protobuf
  module Rpc
    class Stat
      attr_accessor :mode, :start_time, :end_time, :request_size, :dispatcher
      attr_accessor :response_size, :client, :server, :service, :method_name

      MODES = [:SERVER, :CLIENT].freeze

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

      attr_writer :client

      def client
        @client || nil
      end

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

      def method_name
        @method_name ||= @dispatcher.try(:service).try(:method_name)
      end

      def server=(peer)
        @server = { :port => peer[0], :ip => peer[1] }
      end

      def server
        @server ? "#{@server[:ip]}:#{@server[:port]}" : nil
      end

      def service
        @service ||= @dispatcher.try(:service).class.name
      end

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

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

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

      def stopped?
        !end_time.nil?
      end

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

      def server?
        @mode == :SERVER
      end

      def client?
        @mode == :CLIENT
      end

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

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

    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
protobuf-3.7.2.pre1 lib/protobuf/rpc/stat.rb
protobuf-3.7.1 lib/protobuf/rpc/stat.rb
protobuf-3.7.0 lib/protobuf/rpc/stat.rb
protobuf-3.7.0.pre3 lib/protobuf/rpc/stat.rb
protobuf-3.6.12 lib/protobuf/rpc/stat.rb
protobuf-3.6.11 lib/protobuf/rpc/stat.rb
protobuf-3.6.10 lib/protobuf/rpc/stat.rb
protobuf-3.7.0.pre2 lib/protobuf/rpc/stat.rb
protobuf-3.6.9 lib/protobuf/rpc/stat.rb
protobuf-3.7.0.pre1 lib/protobuf/rpc/stat.rb
protobuf-3.7.0.pre0 lib/protobuf/rpc/stat.rb
protobuf-3.6.7 lib/protobuf/rpc/stat.rb
protobuf-3.6.6 lib/protobuf/rpc/stat.rb
protobuf-3.6.2 lib/protobuf/rpc/stat.rb
protobuf-3.6.1 lib/protobuf/rpc/stat.rb
protobuf-3.6.0 lib/protobuf/rpc/stat.rb
protobuf-3.5.5 lib/protobuf/rpc/stat.rb
protobuf-3.5.4 lib/protobuf/rpc/stat.rb
protobuf-3.5.3 lib/protobuf/rpc/stat.rb
protobuf-3.5.2 lib/protobuf/rpc/stat.rb