Class: NewRelic::Agent::OpenTelemetry::BaseTranslator

Inherits:
Object
  • Object
show all
Includes:
AttributeMappings
Defined in:
lib/new_relic/agent/opentelemetry/translators/base_translator.rb

Constant Summary

Constants included from AttributeMappings

AttributeMappings::DATASTORE_MAPPINGS, AttributeMappings::DEFAULT_DESTINATIONS, AttributeMappings::HTTP_CLIENT_MAPPINGS, AttributeMappings::HTTP_SERVER_MAPPINGS

Class Method Summary collapse

Class Method Details

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

Method defined by child classes that calls any extra, unique operations to craft attributes. Ex: parse_operation in DatastoreTranslator.

Parameters:

  • result (optional, Hash) (defaults to: {})

    The result hash built by the translate method

  • name (optional, String) (defaults to: nil)

    String The name provided to the translate method

  • attributes (optional, Hash) (defaults to: {})

    The attributes provided to the translate method

  • instrumentation_scope (optional, String) (defaults to: nil)

    The instrumentation scope provided to the translate method

Returns:

  • (Hash)

    The augmented result hash



74
75
76
77
# File 'lib/new_relic/agent/opentelemetry/translators/base_translator.rb', line 74

def add_specialized_attributes(result: {}, name: nil, attributes: {}, instrumentation_scope: nil)
  # no-op
  {}
end

.mappings_hashObject

This method should be redefined in child classes. The body of the method should be the mappings constant defined in the

AttributesMappings module for the attribute type being translated


17
18
19
20
# File 'lib/new_relic/agent/opentelemetry/translators/base_translator.rb', line 17

def mappings_hash
  # no-op
  NewRelic::EMPTY_HASH
end

.translate(attributes: {}, name: nil, instrumentation_scope: nil) ⇒ Hash

The translate method iterates through the mappings_hash to assign the provided attributes into the correct categories

transaction or segment.

Parameters:

  • attributes (optional Hash) (defaults to: {})

    The attributes provided to the Tracer or Span API

  • name (optional String) (defaults to: nil)

    The span name provided to the Tracer#start_span API

  • instrumentation_scope (optional Strin) (defaults to: nil)

    The tracer name provided to the Tracer#start_span API

Returns:

  • (Hash)

    A hash with attributes divided into various categories for assignment on a New Relic



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/new_relic/agent/opentelemetry/translators/base_translator.rb', line 31

def translate(attributes: {}, name: nil, instrumentation_scope: nil)
  working_attrs = attributes.dup # shallow copy
  result = {intrinsic: {}, agent: {}, custom: {}, for_segment_api: {}, instance_variable: {}, translator: self}

  mappings_hash.each do |nr_key, mapping|
    value = extract_first_present(working_attrs, mapping[:otel_keys])
    next unless value

    case mapping[:category]
    when :intrinsic
      result[:intrinsic][nr_key] = value
    when :agent
      result[:agent][nr_key] = {value: value, destinations: mapping[:destinations]}
    when :instance_variable
      result[:instance_variable][nr_key] = value
    end

    if mapping[:segment_field]
      result[:for_segment_api][mapping[:segment_field]] = value
    end
  end

  # Call any methods unique to the category being translated to create
  # specialized attributes.
  # This method will augment the working result hash, so it does not
  # need to be merged.
  add_specialized_attributes(result: result, name: name, attributes: attributes, instrumentation_scope: instrumentation_scope)

  # Assign any remaining attributes as custom attributes
  result[:custom] = working_attrs

  result
end