Class: GrpcOpencensusInterceptor::ClientInterceptor

Inherits:
GRPC::ClientInterceptor
  • Object
show all
Defined in:
lib/grpc_opencensus_interceptor/client_interceptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(span_context: nil, sampler: nil) ⇒ ClientInterceptor

Returns a new instance of ClientInterceptor.

Parameters:

  • span_context (SpanContext) (defaults to: nil)

    The span context within which to create spans. Optional: If omitted, spans are created in the current thread-local span context.

  • sampler (#call) (defaults to: nil)

    The sampler to use when creating spans. Optional: If omitted, uses the sampler in the current config.



8
9
10
11
12
# File 'lib/grpc_opencensus_interceptor/client_interceptor.rb', line 8

def initialize(span_context: nil, sampler: nil)
  @span_context = span_context || OpenCensus::Trace
  @sampler      = sampler
  @serializer   = OpenCensus::Trace::Formatters::Binary.new
end

Instance Method Details

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

Intercept a unary request response call

Parameters:

  • request (Object)
  • call (GRPC::ActiveCall)
  • method (String)
  • metadata (Hash)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/grpc_opencensus_interceptor/client_interceptor.rb', line 22

def request_response(request:, call:, method:, metadata:)
  span_context = @span_context
  if span_context == OpenCensus::Trace && !span_context.span_context
    return yield
  end

  # NOTE: Use method as span name
  span_name = method

  span = span_context.start_span span_name, sampler: @sampler
  start_request span, method, 
  begin
    grpc_ex = GRPC::Ok.new
    yield
  rescue StandardError => e
    grpc_ex = Util.to_grpc_ex(e)
    raise e
  ensure
    finish_request span, grpc_ex
    span_context.end_span span
  end
end