Class: NewRelic::Agent::OpenTelemetry::AttributeTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/opentelemetry/attribute_translator.rb

Constant Summary collapse

TRANSLATOR_REGISTRY =
{
  # Only use the instrumentation_scope category for scopes that
  # will not be correctly assigned using discriminating attributes
  # or span kind
  instrumentation_scope: {
    # pg instrumentation doesn't have db.system assigned when connect
    # spans start, so they would be incorrectly assigned
    # the HttpClientTranslator
    'opentelemetry-instrumentation-pg' => DatastoreTranslator
    # 'opentelemetry-instrumentation-redis' => RedisDatastoreTranslator,
  },
  discriminating_attribute: {
    'db.system' => DatastoreTranslator,
    'db.system.name' => DatastoreTranslator
    # 'rpc.system' => RpcTranslator,
  },
  span_kind: {
    client: HttpClientTranslator,
    server: HttpServerTranslator,
    # consumer: MessagingConsumerTranslator,
    # producer: MessagingProducerTranslator,
    internal: GenericTranslator
  }
}.freeze
DISCRIMINATING_ATTRIBUTE_KEYS =
TRANSLATOR_REGISTRY[:discriminating_attribute].keys.freeze

Class Method Summary collapse

Class Method Details

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

Identify the appropriate translator based on the provided arguments. Then, call that translator’s translate method. The translator can be identified by instrumentation scope, distinguising attributes, or span kind.

Returns:

  • (Hash)

    hash with translated attributes, created by the dispatched translator



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/agent/opentelemetry/attribute_translator.rb', line 54

def self.translate(span_kind: nil, attributes: nil, instrumentation_scope: nil, name: nil)
  attributes ||= NewRelic::EMPTY_HASH
  translator =
    if TRANSLATOR_REGISTRY[:instrumentation_scope][instrumentation_scope]
      TRANSLATOR_REGISTRY[:instrumentation_scope][instrumentation_scope]
    elsif k = DISCRIMINATING_ATTRIBUTE_KEYS.find { |key| attributes.key?(key) }
      TRANSLATOR_REGISTRY[:discriminating_attribute][k]
    elsif TRANSLATOR_REGISTRY[:span_kind][span_kind]
      TRANSLATOR_REGISTRY[:span_kind][span_kind]
    else
      GenericTranslator
    end

  translator.translate(attributes: attributes, name: name, instrumentation_scope: instrumentation_scope)
end