module GRPC::Core::CallWithTrace

Stackdriver Trace instrumentation of GRPC by patching GRPC::Core::Call class. Add more RPC information into the trace span created by upstream patch from GRPC::ActiveCallWithTrace

Public Class Methods

add_request_labels(call, message, labels) click to toggle source

@private Add request labels from the Call object and message.

# File lib/google/cloud/trace/patches/call_with_trace.rb, line 27
def self.add_request_labels call, message, labels
  label_keys = Google::Cloud::Trace::LabelKey

  message_size = message.to_s.bytesize.to_s

  set_label labels, label_keys::RPC_REQUEST_SIZE, message_size
  set_label labels, label_keys::RPC_HOST, call.peer.to_s
end
add_response_labels(response, labels) click to toggle source

@private Add response labels from gRPC response

# File lib/google/cloud/trace/patches/call_with_trace.rb, line 38
def self.add_response_labels response, labels
  label_keys = Google::Cloud::Trace::LabelKey

  response_size = response.message.to_s.bytesize.to_s
  status = status_code_to_label response.status.code

  set_label labels, label_keys::RPC_RESPONSE_SIZE, response_size
  set_label labels, label_keys::RPC_STATUS_CODE, status
end
set_label(labels, key, value) click to toggle source

@private Helper method to set label

# File lib/google/cloud/trace/patches/call_with_trace.rb, line 50
def self.set_label labels, key, value
  labels[key] = value if value.is_a? ::String
end
status_code_to_label(code) click to toggle source

@private Reverse lookup from numeric status code to readable string.

# File lib/google/cloud/trace/patches/call_with_trace.rb, line 56
def self.status_code_to_label code
  @lookup ||= Hash[GRPC::Core::StatusCodes.constants.map do |c|
    [GRPC::Core::StatusCodes.const_get(c), c.to_s]
  end]

  @lookup[code]
end

Public Instance Methods

run_batch(*args) click to toggle source

Override GRPC::Core::Call#run_batch method. Reuse the “gRPC request” span created in ActiveCallWithTrace patch to add more information from the request.

Calls superclass method
# File lib/google/cloud/trace/patches/call_with_trace.rb, line 68
def run_batch *args
  span = Google::Cloud::Trace.get
  # Make sure we're in a "gRPC request" span
  span = nil if !span.respond_to?(:name) || span.name != GRPC::ActiveCallWithTrace::SPAN_NAME

  if span && !args.empty?
    message = args[0]
    CallWithTrace.add_request_labels self, message, span.labels
  end

  response = super

  CallWithTrace.add_response_labels response, span.labels if span

  response
end