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

#kindObject

Returns the value of attribute kind.



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

def kind
  @kind
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#translator=(value) ⇒ Object (writeonly)

Sets the attribute translator

Parameters:

  • value

    the value to set the attribute translator to.



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

def translator=(value)
  @translator = value
end

Instance Method Details

#add_attributes(attributes) ⇒ Object



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

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

  if @translator
    translated = @translator.translate(attributes: attributes, kind: @kind)
    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_event(name, attributes: nil, timestamp: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 44

def add_event(name, attributes: nil, timestamp: nil)
  # This method adds SpanEvent events to a Span event.
  # A SpanEvent is used to denote a meaningful, singular point in a
  # Span's duration.
  return self unless recording?

  # add_span_event_event lives in OpenTelemetry::AbstractSegmentPatch
  # and in OpenTelemetry::TransactionPatch
  finishable&.add_span_event_event(name, attributes: attributes, timestamp: timestamp)
  self
end

#add_instrumentation_scope(name, version) ⇒ Object



112
113
114
115
116
117
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 112

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


35
36
37
38
39
40
41
42
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 35

def add_link(link)
  return self unless recording?

  # add_span_link lives in OpenTelemetry::AbstractSegmentPatch
  # and in OpenTelemetry::TransactionPatch
  finishable&.add_span_link(link)
  self
end

#apply_translated_attributes(translated) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 56

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



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

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 87

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?



74
75
76
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 74

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)


79
80
81
82
83
84
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 79

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



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

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

#transactionObject



106
107
108
109
110
# File 'lib/new_relic/agent/opentelemetry/trace/span.rb', line 106

def transaction
  return nil unless finishable

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