Class: GrpcAccessLoggingInterceptor::ServerInterceptor

Inherits:
GRPC::ServerInterceptor
  • Object
show all
Defined in:
lib/grpc_access_logging_interceptor/server_interceptor.rb

Constant Summary collapse

USER_AGENT_KEY =
"user-agent"

Instance Method Summary collapse

Constructor Details

#initialize(logger: DefaultLogger.new, params_filter: nil, custom_data_provider: nil) ⇒ ServerInterceptor

Returns a new instance of ServerInterceptor.

Parameters:

  • logger (#log) (defaults to: DefaultLogger.new)
  • params_filter (#filter, nil) (defaults to: nil)
  • custom_data_provider (#call, #execute, nil) (defaults to: nil)


13
14
15
16
17
# File 'lib/grpc_access_logging_interceptor/server_interceptor.rb', line 13

def initialize(logger: DefaultLogger.new, params_filter: nil, custom_data_provider: nil)
  @logger               = logger
  @params_filter        = params_filter
  @custom_data_provider = custom_data_provider
end

Instance Method Details

#request_response(request:, call:, method:) ⇒ Object

Intercept a unary request response call.

Parameters:

  • request (Object)
  • call (GRPC::ActiveCall::SingleReqView)
  • method (Method)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/grpc_access_logging_interceptor/server_interceptor.rb', line 26

def request_response(request:, call:, method:)
  data = {} # Initialize at first to avoid nil

  accessed_at = Time.now

  data.merge!({
    remote_addr:   remote_addr(call.peer),
    accessed_at:   accessed_at.utc.strftime('%Y-%m-%d %H:%M:%S.%6N'),
    params:        filter(request.to_h).to_json,
    user_agent:    call.[USER_AGENT_KEY],
    grpc_method:   grpc_method(method),
    grpc_metadata: GMSC.safe_convert(call.).to_json,
  })
  data.merge!(custom_data(request: request, call: call, method: method))

  yield

  data[:grpc_status_code] = 0  # OK
rescue StandardError => e
  data[:grpc_status_code] = grpc_status_code(e)
  raise e
ensure
  data[:response_time_ms] = response_time_ms(accessed_at)
  log(data)
end