Class: Istox::Xray::GrpcClientXrayInterceptor

Inherits:
Gruf::Interceptors::ClientInterceptor
  • Object
show all
Defined in:
lib/istox/helpers/xray/grpc_client_xray_interceptor.rb

Instance Method Summary collapse

Instance Method Details

#call(request_context:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/istox/helpers/xray/grpc_client_xray_interceptor.rb', line 6

def call(request_context:)
  no_segment = false

  begin
    no_segment = XRay.recorder.current_segment.blank?
  rescue XRay::ContextMissingError
    no_segment = true
  end

  ## usually we have active segment for client interceptor
  ## but in some special case we might not have segment initiated
  ## eg. when running from rails console,
  ## in those case we will initiate the segment here manully
  XRay.recorder.begin_segment('grpc_request') if no_segment

  XRay.recorder.begin_subsegment("grpc_request.#{request_context.method}")

  request_context.[:xray_trace_id] = XRay.recorder.current_segment.trace_id
  request_context.[:xray_parent_id] = XRay.recorder.current_segment.id
  Thread.current[:tracer_id] = XRay.recorder.current_segment.trace_id

  result = yield

  XRay.recorder.end_subsegment

  XRay.recorder.end_segment if no_segment

  result
rescue StandardError => e
  log.error e
  raise e
end