class ElasticAPM::Instrumenter

@api private

def current_transaction

def current_transaction
  @transaction_info.current
end

def current_transaction=(transaction)

def current_transaction=(transaction)
  @transaction_info.current = transaction
end

def initialize(agent)

def initialize(agent)
  @agent = agent
  @config = agent.config
  @transaction_info = TransactionInfo.new
end

def inspect

def inspect
  '<ElasticAPM::Instrumenter ' \
    "current_transaction=#{current_transaction.inspect}" \
    '>'
end

def random_sample?

def random_sample?
  rand <= config.transaction_sample_rate
end

def set_custom_context(context)

def set_custom_context(context)
  return unless current_transaction
  current_transaction.context.custom.merge!(context)
end

def set_tag(key, value)

def set_tag(key, value)
  return unless current_transaction
  current_transaction.context.tags[key] = value.to_s
end

def set_user(user)

def set_user(user)
  return unless current_transaction
  current_transaction.context.user = Context::User.new(config, user)
end

def span(name, type = nil, backtrace: nil, context: nil, &block)

rubocop:disable Metrics/MethodLength
def span(name, type = nil, backtrace: nil, context: nil, &block)
  unless current_transaction
    return yield if block_given?
    return
  end
  current_transaction.span(
    name,
    type,
    backtrace: backtrace,
    context: context,
    &block
  )
end

def start

def start
end

def stop

def stop
  current_transaction.release if current_transaction
  @subscriber.unregister! if @subscriber
end

def submit_transaction(transaction)

def submit_transaction(transaction)
  agent.enqueue_transaction transaction
  return unless config.debug_transactions
  debug('Submitted transaction:') { Util.inspect_transaction transaction }
end

def subscriber=(subscriber)

def subscriber=(subscriber)
  @subscriber = subscriber
  @subscriber.register!
end

def transaction(name = nil, type = nil, context: nil, sampled: nil)

rubocop:disable Metrics/CyclomaticComplexity
rubocop:disable Metrics/MethodLength
def transaction(name = nil, type = nil, context: nil, sampled: nil)
  unless config.instrument
    yield if block_given?
    return
  end
  if (transaction = current_transaction)
    yield transaction if block_given?
    return transaction
  end
  sampled = random_sample? if sampled.nil?
  transaction =
    Transaction.new self, name, type, context: context, sampled: sampled
  self.current_transaction = transaction
  return transaction unless block_given?
  begin
    yield transaction
  ensure
    self.current_transaction = nil
    transaction.done
  end
  transaction
end