Class: NewRelic::Agent::OpenTelemetry::RpcTranslator

Inherits:
BaseTranslator show all
Defined in:
lib/new_relic/agent/opentelemetry/translators/rpc_translator.rb

Constant Summary

Constants included from AttributeMappings

AttributeMappings::DATASTORE_MAPPINGS, AttributeMappings::DEFAULT_DESTINATIONS, AttributeMappings::HTTP_CLIENT_MAPPINGS, AttributeMappings::HTTP_SERVER_MAPPINGS, AttributeMappings::MESSAGING_CONSUMER_MAPPINGS, AttributeMappings::MESSAGING_PRODUCER_MAPPINGS, AttributeMappings::REDIS_MAPPINGS, AttributeMappings::RPC_CLIENT_MAPPINGS, AttributeMappings::RPC_SERVER_MAPPINGS

Class Method Summary collapse

Methods inherited from BaseTranslator

translate

Class Method Details

.add_specialized_attributes(result: {}, name: nil, attributes: nil, instrumentation_scope: nil, kind: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/opentelemetry/translators/rpc_translator.rb', line 16

def add_specialized_attributes(result: {}, name: nil, attributes: nil, instrumentation_scope: nil, kind: nil)
  case kind
  when :client
    uri = build_uri(attributes)
    result[:for_segment_api][:uri] = uri if uri
  when :server
    server_name = create_server_transaction_name(name: name, attributes: attributes, instrumentation_scope: instrumentation_scope) if name
    result[:for_segment_api][:name] = server_name if server_name

    uri = build_uri(attributes)
    if uri
      result[:agent]['request.uri'] = {value: uri, destinations: AttributeMappings::DEFAULT_DESTINATIONS}
    end
  end

  result
end

.build_uri(attributes) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/opentelemetry/translators/rpc_translator.rb', line 39

def build_uri(attributes)
  host = attributes['server.address'] || attributes['net.peer.name'] || attributes['net.sock.peer.addr']
  service = attributes['rpc.service']
  method = attributes['rpc.method']

  # return early if this method is called with an attributes
  # hash that doesn't have the required values
  return unless host || service || method

  # strip scheme prefix (e.g. dns:///, xds:///) used by gRPC name resolvers
  host = host&.sub(%r{\A\w+:///}, '')

  if host && service && method
    "grpc://#{host}/#{service}/#{method}"
  elsif host && method
    "grpc://#{host}/#{method}"
  elsif service && method
    "grpc://#{service}/#{method}"
  end
end

.create_server_transaction_name(name: nil, attributes: nil, instrumentation_scope: nil) ⇒ Object



34
35
36
37
# File 'lib/new_relic/agent/opentelemetry/translators/rpc_translator.rb', line 34

def create_server_transaction_name(name: nil, attributes: nil, instrumentation_scope: nil)
  txn_name = name&.delete_prefix(NewRelic::SLASH)
  Instrumentation::ControllerInstrumentation::TransactionNamer.name_for(nil, nil, :web, {class_name: instrumentation_scope, name: txn_name})
end

.mappings_hash(kind) ⇒ Object



12
13
14
# File 'lib/new_relic/agent/opentelemetry/translators/rpc_translator.rb', line 12

def mappings_hash(kind)
  kind == :client ? AttributeMappings::RPC_CLIENT_MAPPINGS : AttributeMappings::RPC_SERVER_MAPPINGS
end