Sha256: d8f871522353f8959aaa04cdc012e703fffd5363a2ecbc4c1932c18455104677

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'securerandom'

require 'elastic_apm/span/context'

module ElasticAPM
  # @api private
  class Span
    DEFAULT_TYPE = 'custom'.freeze

    # rubocop:disable Metrics/ParameterLists
    def initialize(
      transaction,
      id,
      name,
      type = nil,
      parent: nil,
      context: nil
    )
      @transaction = transaction
      @id = id
      @name = name
      @type = type || DEFAULT_TYPE
      @parent = parent
      @context = context

      @stacktrace = nil
      @original_backtrace = nil
    end
    # rubocop:enable Metrics/ParameterLists

    attr_accessor :name, :context, :type, :stacktrace, :original_backtrace
    attr_reader :id, :duration, :parent, :relative_start

    def start
      @relative_start = Util.micros - @transaction.timestamp

      self
    end

    def done
      @duration = Util.micros - @transaction.timestamp - relative_start

      if original_backtrace && long_enough_for_stacktrace?
        self.stacktrace =
          @transaction.instrumenter.agent.stacktrace_builder.build(
            original_backtrace, type: :span
          )
      end

      self.original_backtrace = nil # release it

      self
    end

    def done?
      !!duration
    end

    def running?
      relative_start && !done?
    end

    def inspect
      "<ElasticAPM::Span id:#{id}" \
        " name:#{name.inspect}" \
        " type:#{type.inspect}" \
        '>'
    end

    private

    def long_enough_for_stacktrace?
      min_duration = @transaction.instrumenter.config.span_frames_min_duration

      case min_duration
      when -1 then true
      when 0 then false
      else duration / 1000 >= min_duration
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elastic-apm-0.7.4 lib/elastic_apm/span.rb
elastic-apm-0.7.3 lib/elastic_apm/span.rb
elastic-apm-0.7.2 lib/elastic_apm/span.rb