class Gruf::Lightstep::ServerInterceptor

Intercepts inbound calls to provide LightStep tracing

Constants

DEFAULT_ERROR_CLASSES

Public Instance Methods

call() { || ... } click to toggle source

Handle the gruf around hook and trace sampled requests

# File lib/gruf/lightstep/server_interceptor.rb, line 29
def call(&_block)
  return yield if ignore_methods.include?(request.method_name)

  result = nil
  params = request_message_params
  tracer = ::Bigcommerce::Lightstep::Tracer.instance
  tracer.clear_active_span! # because we're always starting from the top on a gRPC boundary
  tracer.start_span(request.method_name, context: request_method.headers.to_h) do |span|
    span.set_tag('grpc.method', request.method_key)
    span.set_tag('grpc.request_class', request.request_class)
    span.set_tag('grpc.service', request.service_key)
    span.set_tag('span.kind', 'server')

    allowlist.each do |param|
      span.set_tag(param.to_s, params[param]) if params.key?(param)
    end

    begin
      result = yield
    rescue StandardError => e
      span.set_tag('error', error?(e))
      span.set_tag('grpc.error', true)
      span.set_tag('grpc.error_code', code_for(e))
      span.set_tag('grpc.error_class', e.class)
      raise # passthrough, we just want the annotations
    end
  end
  result
end

Private Instance Methods

allowlist() click to toggle source

@return [Array<Symbol>]

# File lib/gruf/lightstep/server_interceptor.rb, line 70
def allowlist
  @allowlist ||= (options.fetch(:allowlist, nil) || []).map(&:to_s).map(&:to_sym)
end
code_for(error) click to toggle source

@param [StandardError] @return [Number] that maps to one of the GRCP::Core::StatusCodes or Gruf::Lightstep.default_error_code

# File lib/gruf/lightstep/server_interceptor.rb, line 78
def code_for(error)
  error.respond_to?(:code) ? error.code : Gruf::Lightstep.default_error_code
end
error?(exception) click to toggle source

@return [Boolean]

# File lib/gruf/lightstep/server_interceptor.rb, line 101
def error?(exception)
  error_classes.include?(exception.class.to_s)
end
error_classes() click to toggle source

@return [Array]

# File lib/gruf/lightstep/server_interceptor.rb, line 108
def error_classes
  @error_classes ||= (options.fetch(:error_classes, nil) || DEFAULT_ERROR_CLASSES)
end
ignore_methods() click to toggle source

@return [Array<String>]

# File lib/gruf/lightstep/server_interceptor.rb, line 63
def ignore_methods
  @ignore_methods ||= options.fetch(:ignore_methods, nil) || []
end
request_message_params() click to toggle source

@return [Hash]

# File lib/gruf/lightstep/server_interceptor.rb, line 92
def request_message_params
  return {} if request.client_streamer? || !request.message.respond_to?(:to_h)

  request.message.to_h
end
request_method() click to toggle source

@return [Gruf::Lightstep::Method]

# File lib/gruf/lightstep/server_interceptor.rb, line 85
def request_method
  Gruf::Lightstep::Method.new(request.active_call, request.method_key, request.message)
end