Sha256: 2fef1a72e638803bffe21153a3487ecc384a04a4c40fa943e7551e82569ec895

Contents?: true

Size: 1.26 KB

Versions: 13

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'grpc_kit'

class LoggingInterceptor < GRPC::ClientInterceptor
  def request_response(request: nil, method: nil, **)
    now = Time.now.to_i
    GrpcKit.logger.info("Started request #{request}, method=#{method.name}, service_name=#{method.receiver.class.service_name}")
    yield.tap do
      GrpcKit.logger.info("Elapsed Time: #{Time.now.to_i - now}")
    end
  end

  def client_streamer(call: nil, method: nil, **)
    GrpcKit.logger.info("Started request method=#{method.name}, service_name=#{method.receiver.class.service_name}")
    yield(LoggingStream.new(call))
  end

  def server_streamer(call: nil, method: nil, **)
    GrpcKit.logger.info("Started request method=#{method.name}, service_name=#{method.receiver.class.service_name}")
    yield(LoggingStream.new(call))
  end

  def bidi_streamer(**)
    yield
  end

  class LoggingStream
    def initialize(stream)
      @stream = stream
    end

    def send_msg(msg, **opts)
      GrpcKit.logger.info("logging interceptor send #{msg}")
      @stream.send_msg(msg, opts)
    end

    def recv(**opt)
      @stream.recv(opt).tap do |v|
        GrpcKit.logger.info("logging interceptor recv #{v}")
      end
    end

    def close_and_recv
      @stream.close_and_recv
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
griffin-0.1.4 examples/interceptors/client_logging_interceptor.rb
griffin-0.1.3 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.9 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.8 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.7 examples/interceptors/client_logging_interceptor.rb
griffin-0.1.2 examples/interceptors/client_logging_interceptor.rb
griffin-0.1.1 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.6 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.5 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.4 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.3 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.2 examples/interceptors/client_logging_interceptor.rb
grpc_kit-0.1.1 examples/interceptors/client_logging_interceptor.rb