Sha256: 5031638756bdd8f7ed8e9759e32911848c78ceca904e4a74f309ea15f88da69d

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 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, :type, :original_backtrace
    attr_reader :id, :context, :stacktrace, :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?
        @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

7 entries across 7 versions & 1 rubygems

Version Path
elastic-apm-1.1.0 lib/elastic_apm/span.rb
elastic-apm-1.0.2 lib/elastic_apm/span.rb
elastic-apm-1.0.1 lib/elastic_apm/span.rb
elastic-apm-1.0.0 lib/elastic_apm/span.rb
elastic-apm-1.0.0.beta2 lib/elastic_apm/span.rb
elastic-apm-1.0.0.beta1 lib/elastic_apm/span.rb
elastic-apm-0.8.0 lib/elastic_apm/span.rb