Class: NewRelic::Agent::OpenTelemetry::Trace::Span

Inherits:
OpenTelemetry::Trace::Span
  • Object
show all
Defined in:
lib/new_relic/agent/opentelemetry/trace/span.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finishableObject

Returns the value of attribute finishable.



10
11
12
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 10

def finishable
  @finishable
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 12

def status
  @status
end

#translator=(value) ⇒ Object (writeonly)

Sets the attribute translator

Parameters:

  • value

    the value to set the attribute translator to.



11
12
13
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 11

def translator=(value)
  @translator = value
end

Instance Method Details

#add_attributes(attributes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 22

def add_attributes(attributes)
  return if attributes.nil? || attributes.empty?

  if @translator
    translated = @translator.translate(attributes: attributes)
    apply_translated_attributes(translated)
  else
    # fallback to adding all attributes to the span as custom
    NewRelic::Agent.add_custom_span_attributes(attributes)
  end
end

#add_instrumentation_scope(name, version) ⇒ Object



90
91
92
93
94
95
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 90

def add_instrumentation_scope(name, version)
  return unless transaction

  transaction.add_agent_attribute('otel.scope.name', name, AttributeFilter::DST_SPAN_EVENTS)
  transaction.add_agent_attribute('otel.scope.version', version, AttributeFilter::DST_SPAN_EVENTS)
end

#apply_translated_attributes(translated) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 34

def apply_translated_attributes(translated)
  translated[:instance_variable].each do |key, value|
    sym_key = "@#{key}".to_sym
    finishable.instance_variable_set(sym_key, value)
  end

  translated[:intrinsic].each do |key, value|
    finishable.attributes.add_intrinsic_attribute(key, value)
  end

  translated[:agent].each do |key, attr_data|
    finishable.attributes.add_agent_attribute(key, attr_data[:value], attr_data[:destinations])
  end

  NewRelic::Agent.add_custom_span_attributes(translated[:custom]) unless translated[:custom].empty?
end

#finish(end_timestamp: nil) ⇒ Object



14
15
16
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 14

def finish(end_timestamp: nil)
  finishable&.finish
end

#name=(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 65

def name=(name)
  if recording?
    # overridden_name has slightly higher precedence than
    # set_transaction_name, but still has a small chance of being
    # overruled by other transaction naming operations if a
    # @frozen_name has already been set. See Transaction#best_name.
    if finishable.is_a?(NewRelic::Agent::Transaction)
      finishable.overridden_name = name
    # New Relic doesn't allow customers to rename segments
    # so this method is just to deal with the OTel APIs that may
    # try to rename a span after it's created.
    elsif finishable.is_a?(NewRelic::Agent::Transaction::Segment)
      finishable.instance_variable_set(:@name, name)
    end
  else
    NewRelic::Agent.logger.warn('Calling name= on a finished OpenTelemetry Span')
  end
end

#record_exception(exception, attributes: nil) ⇒ Object

TODO: do we need to add a condition for NewRelic::Agent::Tracer.capture_segment_error(segment) if finishable is a segment?



52
53
54
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 52

def record_exception(exception, attributes: nil)
  NewRelic::Agent.notice_error(exception, attributes: attributes)
end

#recording?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 57

def recording?
  # in OTel, the recording? method checks for the end time on a span
  # The closest method we have to this is finished? which exists on
  # both transactions and segments.
  !finishable&.finished?
end

#set_attribute(key, value) ⇒ Object



18
19
20
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 18

def set_attribute(key, value)
  add_attributes(key => value)
end

#transactionObject



84
85
86
87
88
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 84

def transaction
  return nil unless finishable

  @transaction ||= finishable.is_a?(Transaction) ? finishable : finishable.transaction
end