module NewRelic::Agent::Instrumentation::GRPC::Server

Constants

CATEGORY
DESTINATIONS
DT_KEYS
INSTANCE_VAR_HOST
INSTANCE_VAR_METHOD
INSTANCE_VAR_PORT
INSTRUMENTATION_NAME

Public Instance Methods

add_http2_port_with_tracing(*args) { || ... } click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 47
def add_http2_port_with_tracing(*args)
  set_host_and_port_on_server_instance(args.first)
  yield
end
handle_with_tracing(streamer_type, active_call, mth, _inter_ctx) { || ... } click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 25
def handle_with_tracing(streamer_type, active_call, mth, _inter_ctx)
  return yield unless trace_with_newrelic?

  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  metadata = metadata_for_call(active_call)
  txn = NewRelic::Agent::Transaction.start_new_transaction(NewRelic::Agent::Tracer.state,
    CATEGORY,
    trace_options)
  add_attributes(txn, metadata, streamer_type)
  process_distributed_tracing_headers(metadata)

  begin
    yield
  rescue => e
    NewRelic::Agent.notice_error(e)
    raise
  end
ensure
  txn&.finish
end
run_with_tracing(*args) { || ... } click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 52
def run_with_tracing(*args)
  set_host_and_port_and_method_info_on_desc
  yield
end

Private Instance Methods

add_attributes(txn, metadata, streamer_type) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 59
def add_attributes(txn, metadata, streamer_type)
  grpc_params(metadata, streamer_type).each do |attr, value|
    txn.add_agent_attribute(attr, value, DESTINATIONS)
    txn.current_segment&.add_agent_attribute(attr, value)
  end
end
grpc_headers(metadata) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 103
def grpc_headers(metadata)
  metadata.reject { |k, v| DT_KEYS.include?(k) }
end
grpc_params(metadata, streamer_type) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 107
def grpc_params(metadata, streamer_type)
  host = instance_variable_get(INSTANCE_VAR_HOST)
  port = instance_variable_get(INSTANCE_VAR_PORT)
  method = instance_variable_get(INSTANCE_VAR_METHOD)
  {'request.headers': grpc_headers(metadata),
   'request.uri': "grpc://#{host}:#{port}/#{method}",
   'request.method': method,
   'request.grpc_type': streamer_type}
end
host_and_port_from_host_string(host_string) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 78
def host_and_port_from_host_string(host_string)
  return unless host_string

  info = host_string.split(':').freeze
  return unless info.size == 2

  info
end
metadata_for_call(active_call) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 66
def metadata_for_call(active_call)
  return NewRelic::EMPTY_HASH unless active_call&.metadata

  active_call.metadata
end
process_distributed_tracing_headers(metadata) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 72
def process_distributed_tracing_headers(metadata)
  return unless metadata && !metadata.empty?

  ::NewRelic::Agent::DistributedTracing::accept_distributed_trace_headers(metadata, 'Other')
end
set_host_and_port_and_method_info_on_desc() click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 95
def set_host_and_port_and_method_info_on_desc
  rpc_descs.each do |method, desc|
    desc.instance_variable_set(INSTANCE_VAR_HOST, instance_variable_get(INSTANCE_VAR_HOST))
    desc.instance_variable_set(INSTANCE_VAR_PORT, instance_variable_get(INSTANCE_VAR_PORT))
    desc.instance_variable_set(INSTANCE_VAR_METHOD, cleaned_method(method))
  end
end
set_host_and_port_on_server_instance(host_string) click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 87
def set_host_and_port_on_server_instance(host_string)
  info = host_and_port_from_host_string(host_string)
  return unless info

  instance_variable_set(INSTANCE_VAR_HOST, info.first)
  instance_variable_set(INSTANCE_VAR_PORT, info.last)
end
trace_options() click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 117
def trace_options
  method = instance_variable_get(INSTANCE_VAR_METHOD)
  {category: CATEGORY, transaction_name: "Controller/#{method}"}
end
trace_with_newrelic?() click to toggle source
# File lib/new_relic/agent/instrumentation/grpc/server/instrumentation.rb, line 122
def trace_with_newrelic?
  do_trace = instance_variable_get(:@trace_with_newrelic)
  return do_trace unless do_trace.nil? # check for nil, not falsey

  host = instance_variable_get(INSTANCE_VAR_HOST)
  return true unless host

  do_trace = !host_denylisted?(host)
  instance_variable_set(:@trace_with_newrelic, do_trace)

  do_trace
end