Class: NewRelic::Agent::OpenTelemetry::Trace::Tracer

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

Constant Summary collapse

VALID_KINDS =
[:server, :client, :consumer, :producer, :internal, nil].freeze
KINDS_THAT_START_TRANSACTIONS =
%i[server consumer].freeze
KINDS_THAT_DO_NOT_START_TXNS_WITHOUT_REMOTE_PARENT =
[:client, :producer, :internal, nil].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, version = nil) ⇒ Tracer

Returns a new instance of Tracer.



16
17
18
19
# File 'lib/new_relic/agent/opentelemetry/trace/tracer.rb', line 16

def initialize(name = nil, version = nil)
  @name = name || ''
  @version = version || ''
end

Instance Method Details

#in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/new_relic/agent/opentelemetry/trace/tracer.rb', line 46

def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
  span = nil
  span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind)

  ::OpenTelemetry::Trace.with_span(span) { |s, c| yield(s, c) }
rescue => e
  # TODO: Update for segment errors if finishable is a segment
  NewRelic::Agent.notice_error(e)
  raise
ensure
  span&.finish
end

#start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/new_relic/agent/opentelemetry/trace/tracer.rb', line 21

def start_span(name, with_parent: nil, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
  parent_otel_context = ::OpenTelemetry::Trace.current_span(with_parent).context

  return ::OpenTelemetry::Trace::Span::INVALID if should_not_create_telemetry?(parent_otel_context, kind)

  translated = AttributeTranslator.translate(span_kind: kind, attributes: attributes, instrumentation_scope: @name, name: name)

  finishable = if can_start_transaction?(parent_otel_context, kind)
    start_transaction_from_otel(name, parent_otel_context, kind, translated: translated)
  else
    start_segment_from_otel(name: name, translated: translated, start_timestamp: start_timestamp, kind: kind)
  end

  otel_span = get_otel_span_from_finishable(finishable)

  otel_span.finishable = finishable
  otel_span.status = ::OpenTelemetry::Trace::Status.unset
  otel_span.translator = translated[:translator]
  add_remote_context_to_otel_span(otel_span, parent_otel_context)
  otel_span.add_instrumentation_scope(@name, @version)
  otel_span.apply_translated_attributes(translated)

  otel_span
end