class ElasticAPM::Span

@api private

Constants

DEFAULT_TYPE

Attributes

action[RW]
context[R]
duration[R]
name[RW]
original_backtrace[RW]
outcome[RW]
parent[R]
sample_rate[R]
self_time[R]
stacktrace[R]
subtype[RW]
timestamp[R]
trace_context[RW]
transaction[R]
type[RW]

Public Class Methods

new( name:, transaction:, trace_context:, parent:, type: nil, subtype: nil, action: nil, context: nil, stacktrace_builder: nil, sync: nil ) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/elastic_apm/span.rb, line 42
def initialize(
  name:,
  transaction:,
  trace_context:,
  parent:,
  type: nil,
  subtype: nil,
  action: nil,
  context: nil,
  stacktrace_builder: nil,
  sync: nil
)
  @name = name

  if subtype.nil? && type&.include?('.')
    @type, @subtype, @action = type.split('.')
  else
    @type = type || DEFAULT_TYPE
    @subtype = subtype
    @action = action
  end

  @transaction = transaction
  @parent = parent
  @trace_context = trace_context || parent.trace_context.child
  @sample_rate = transaction.sample_rate

  @context = context || Span::Context.new(sync: sync)
  @stacktrace_builder = stacktrace_builder
end

Public Instance Methods

done(clock_end: Util.monotonic_micros) click to toggle source
# File lib/elastic_apm/span.rb, line 124
def done(clock_end: Util.monotonic_micros)
  stop clock_end
  self
end
inspect() click to toggle source
# File lib/elastic_apm/span.rb, line 155
def inspect
  "<ElasticAPM::Span id:#{trace_context&.id}" \
    " name:#{name.inspect}" \
    " type:#{type.inspect}" \
    " subtype:#{subtype.inspect}" \
    " action:#{action.inspect}" \
    '>'
end
prepare_for_serialization!() click to toggle source
# File lib/elastic_apm/span.rb, line 129
def prepare_for_serialization!
  build_stacktrace! if should_build_stacktrace?
  self.original_backtrace = nil # release original
end
running?() click to toggle source
# File lib/elastic_apm/span.rb, line 142
def running?
  started? && !stopped?
end
set_destination(address: nil, port: nil, service: nil, cloud: nil) click to toggle source
# File lib/elastic_apm/span.rb, line 146
def set_destination(address: nil, port: nil, service: nil, cloud: nil)
  context.destination = Span::Context::Destination.new(
    address: address,
    port: port,
    service: service,
    cloud: cloud
  )
end
start(clock_start = Util.monotonic_micros) click to toggle source

life cycle

# File lib/elastic_apm/span.rb, line 98
def start(clock_start = Util.monotonic_micros)
  @timestamp = Util.micros
  @clock_start = clock_start
  @parent.child_started
  self
end
started?() click to toggle source
# File lib/elastic_apm/span.rb, line 138
def started?
  !!timestamp
end
stop(clock_end = Util.monotonic_micros) click to toggle source
# File lib/elastic_apm/span.rb, line 105
def stop(clock_end = Util.monotonic_micros)
  @duration ||= (clock_end - @clock_start)
  @parent.child_stopped
  @self_time = @duration - child_durations.duration

  if exit_span?
    context.destination ||= Context::Destination.new
    context.destination.service ||= Context::Destination::Service.new
    context.destination.service.resource ||= (subtype || type)

    # Deprecated fields but required by some versions of APM Server, so
    # we auto-infer them from existing fields
    context.destination.service.name ||= (subtype || type)
    context.destination.service.type ||= type
  end

  self
end
stopped?() click to toggle source
# File lib/elastic_apm/span.rb, line 134
def stopped?
  !!duration
end

Private Instance Methods

build_stacktrace!() click to toggle source
# File lib/elastic_apm/span.rb, line 166
def build_stacktrace!
  @stacktrace = @stacktrace_builder.build(original_backtrace, type: :span)
end
exit_span?() click to toggle source
# File lib/elastic_apm/span.rb, line 184
def exit_span?
  context.destination || context.db || context.message || context.http
end
long_enough_for_stacktrace?() click to toggle source
# File lib/elastic_apm/span.rb, line 174
def long_enough_for_stacktrace?
  min_duration =
    @stacktrace_builder.config.span_frames_min_duration_us

  return true if min_duration < 0
  return false if min_duration == 0

  duration >= min_duration
end
should_build_stacktrace?() click to toggle source
# File lib/elastic_apm/span.rb, line 170
def should_build_stacktrace?
  @stacktrace_builder && original_backtrace && long_enough_for_stacktrace?
end